fork download
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main() {
  7. ios::sync_with_stdio(false);
  8. cin.tie(nullptr);
  9.  
  10. int n;
  11. cin >> n;
  12.  
  13. vector<long long> dist(n - 1);
  14. for (int i = 0; i < n - 1; i++) {
  15. cin >> dist[i];
  16. }
  17.  
  18. // Oblicz pozycje miast
  19. vector<long long> pos(n);
  20. pos[0] = 0;
  21. for (int i = 1; i < n; i++) {
  22. pos[i] = pos[i - 1] + dist[i - 1];
  23. }
  24.  
  25. long long total_length = pos[n - 1];
  26. long long min_max_dist = total_length;
  27.  
  28. vector<long long> candidates;
  29.  
  30. // Dla miasta i: max_dist = max(pos[i], total_length - pos[i])
  31. for (int i = 0; i < n; i++) {
  32. long long max_dist = max(pos[i], total_length - pos[i]);
  33. if (max_dist < min_max_dist) {
  34. min_max_dist = max_dist;
  35. candidates.clear();
  36. candidates.push_back(i+1);
  37. }
  38. else if(max_dist == min_max_dist){
  39. candidates.push_back(i+1);
  40. }
  41. }
  42.  
  43. for(long long x : candidates)
  44. {
  45. cout<<x<<' ';
  46. }
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5300KB
stdin
7
2 3 2 2 4 1 
stdout
4