#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];
#define cerr while(0) cerr
#define INF 1000000000
signed main() {
#ifndef cerr
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
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] = -INF; else wl[i] = -INF, wr[i] = -score;
}
for(int i = 1; i <= N; i++) cerr << "w " << wl[i] << ' ' << wr[i] << '\n';
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;
for(int i = 1; i <= N; i++) cerr << dpl[i] << ' ' << dpr[i] << '\n';
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;
int first = bg - (i - bg);
assert(first >= 1);
for(int j = first; j <= i; j++) if(ar[j] == j) old++;
glob -= old;
reverse(ar+first, ar+i+1);
int next = 0;
for(int j = first; j <= i; j++) if(ar[j] == j) next++;
if(glob + next > res) res = glob+next, a = first, b = i;
glob += old;
bg = i+1;
reverse(ar+first, ar+i+1);
}
}
int en = N;
for(int i = N; i >= 1; i--) {
if(dpr[i] >= dpr[i-1]) {
int old = 0;
int last = en+(en-i);
assert(last <= N);
for(int j = i; j <= last; j++) if(ar[j] == j) old++;
glob -= old;
reverse(ar+i, ar+last+1);
int next = 0;
for(int j = i; j <= last; j++) if(ar[j] == j) next++;
if(glob + next > res) res = glob+next, a = i, b = last;
glob += old;
en = i-1;
reverse(ar+i, ar+last+1);
}
}
cerr << glob << '\n';
cout << ar[a] << ' ' << ar[b] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Integer 0 violates the range [1, 402] |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Integer 0 violates the range [1, 4500] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
3028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
94 ms |
15008 KB |
Integer 0 violates the range [1, 490000] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
51 ms |
8936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |