This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+5;
int ar[maxn], lk[maxn];
int wl[maxn], wr[maxn];
int dpl[maxn], dpr[maxn];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for(int i = 1; i <= N; i++) cin >> ar[i], lk[ar[i]] = i;
for(int i = 1; i <= N; i++) {
int score = i - ar[i];
if(score >= 0) wl[i] = score, wr[i] = -1; else wl[i] = -1, wr[i] = -score;
}
int glob = 0;
for(int i = 1; i <= N; i++) if(ar[i] == i) glob++;
for(int i = 1; i <= N; i++) dpl[i] = (wl[i-1] + 2 == wl[i]) ? dpl[i-1]+1 : 1;
for(int i = N; i >= 1; i--) dpr[i] = (wr[i+1] + 2 == wl[i]) ? dpr[i+1]+1 : 1;
int bg = 1;
int res = 0;
int a = -1, b = -1;
for(int i = 1; i <= N; i++) {
if(dpl[i] >= dpl[i+1]) {
int old = 0;
for(int j = bg; j <= i; j++) if(ar[j] == j) old++;
glob -= old;
reverse(ar+bg, ar+i+1);
int next = 0;
for(int j = bg; j <= i; j++) if(ar[j] == j) next++;
if(glob + next > res) res = glob+next, a = ar[bg], b = ar[i];
glob += old;
bg = i+1;
}
}
int en = N;
for(int i = N; i >= 1; i--) {
if(dpr[i] >= dpr[i-1]) {
int old = 0;
for(int j = i; j <= en; j++) if(ar[j] == j) old++;
glob -= old;
reverse(ar+i, ar+en+1);
int next = 0;
for(int j = i; j <= en; j++) if(ar[j] == j) next++;
if(glob + next > res) res = glob+next, a = ar[i], b = ar[en];
glob += old;
en = i-1;
}
}
cout << a << ' ' << b << '\n';
}
# | 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... |