#include <iostream>
#include <vector>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
// use 1-indexed vector for convenience.
vector<int> p(n+1);
for (int i = 1; i <= n; i++){
cin >> p[i];
}
// Option 1: Look for a pair that gives +2 improvement.
for (int i = 1; i <= n; i++){
int j = p[i];
// Check valid index and that it is a mutual pair and not fixed originally.
if(i != j && j >= 1 && j <= n && p[j] == i) {
// We want to output the contiguous subarray from min(i, j) to max(i, j).
int l = min(i, j);
int r = max(i, j);
// Output the card numbers at positions l and r.
cout << p[l] << " " << p[r] << "\n";
return 0;
}
}
// Option 2: Look for an adjacent pair that gives +1 improvement.
for (int i = 1; i < n; i++){
// Consider subarray [i, i+1]
// After reversal, position i gets p[i+1] and becomes fixed if p[i+1]== i,
// and position i+1 gets p[i] and becomes fixed if p[i]== i+1.
if(p[i+1] == i && p[i] != i){ // ensure that the improvement is not cancelling an already fixed point
cout << p[i] << " " << p[i+1] << "\n";
return 0;
}
if(p[i] == i+1 && p[i+1] != i+1){
cout << p[i] << " " << p[i+1] << "\n";
return 0;
}
}
// Option 3: If no improvement can be made, choose a single element reversal.
// We can pick the first card.
cout << p[1] << " " << p[1] << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |