fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int N; cin >> N;
  6. int stands[N];
  7. for (int i=0; i<N; i++) cin >> stands[i];
  8.  
  9. for (int i = 0; i < N; i++) {
  10. int dist = 0, currIdx = i;
  11. vector<int> reStands;
  12. for (int i=0; i<N; i++) reStands.push_back(stands[i]);
  13. while (reStands.size() > 0) {
  14. cout << reStands.size() << " " << currIdx << endl;
  15. if (currIdx == 0 || currIdx == reStands.size()-1 || reStands.size() == 2) {
  16. dist += reStands[reStands.size()-1] - reStands[0];
  17. break;
  18. }
  19. else {
  20. if (reStands[i]-reStands[i-1] < reStands[i+1] - reStands[i]) {
  21. cout << "greater: " << reStands[i] << "-" << reStands[i-1] << endl;
  22. currIdx = i-1;
  23. dist += reStands[i]-reStands[i-1];
  24. reStands.erase(reStands.begin()+i);
  25. }
  26. else {
  27. cout << "less: " << reStands[i+1] << "-" << reStands[i] << endl;
  28. currIdx = i;
  29. dist += reStands[i+1] - reStands[i];
  30. reStands.erase(reStands.begin()+i);
  31. }
  32. }
  33. cout << dist << endl;
  34. }
  35. cout << dist << endl;
  36. }
  37. }
Success #stdin #stdout 0.01s 5360KB
stdin
7
2 5 6 10 12 20 22
stdout
7 0
20
7 1
less: 6-5
1
6 1
less: 10-6
5
5 1
less: 12-10
7
4 1
less: 20-12
15
3 1
less: 22-20
17
2 1
37
7 2
greater: 6-5
1
6 1
less: 12-10
3
5 2
greater: 12-5
10
4 1
less: 22-20
12
3 2
32
7 3
less: 12-10
2
6 3
greater: 12-6
8
5 2
less: 22-20
10
4 3
30
7 4
greater: 12-10
2
6 3
less: 22-20
4
5 4
24
7 5
less: 22-20
2
6 5
22
7 6
20