fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. int N; cin >> N;
  8.  
  9. vector<int> A(N + 1);
  10. for (int i = 1; i <= N; ++i)
  11. cin >> A[i];
  12.  
  13. // let's solve the problem
  14. vector<int> decreasing;
  15.  
  16. pair<int, int> answer;
  17.  
  18. // build the decreasing sequence
  19. decreasing.push_back(1);
  20. for (int i = 1; i <= N; ++i)
  21. if (A[i] < A[decreasing.back()])
  22. decreasing.push_back(i); // we work with indexes because we might have equal values
  23.  
  24. for (int i = N; i > 0; --i) {
  25. while (decreasing.size() and A[decreasing.back()] < A[i]) { // while we can pair these 2
  26. pair<int, int> current_pair(decreasing.back(), i);
  27. if (current_pair.second - current_pair.first > answer.second - answer.first)
  28. answer = current_pair;
  29. decreasing.pop_back();
  30. }
  31. }
  32.  
  33. cout << "Best pair found: (" << answer.first << ", " << answer.second << ") with values (" << A[answer.first] << ", " << A[answer.second] << ")\n";
  34. }
Success #stdin #stdout 0s 4360KB
stdin
Standard input is empty
stdout
Best pair found: (0, 0) with values (0, 0)