#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 500005;
vector<int> B[MAXN];
int a[MAXN], sum[MAXN], cnt[4 * MAXN], nArr;
void brute(void) {
int res(0), l(0), r(0);
for (int i = 1; i <= nArr; ++i) {
for (int j = i; j <= nArr; ++j) {
reverse(a + i, a + j + 1);
int cnt(0);
for (int k = 1; k <= nArr; ++k)
cnt += (a[k] == k);
reverse(a + i, a + j + 1);
if(cnt > res) {
res = cnt;
l = i, r = j;
}
}
}
cout << res << '\n' << a[l] << ' ' << a[r] << '\n';
}
void process() {
cin >> nArr;
int cnto(0);
for (int i = 1; i <= nArr; ++i) {
cin >> a[i];
//if(i == 1) { for (int i = 1; i <= nArr; ++i) a[i] = i; shuffle(a + 1, a + nArr + 1, rng); for (int i = 1; i <= nArr; ++i) cout << a[i] << " \n"[i == nArr]; }
B[max(i, a[i])].push_back(min(i, a[i]));
cnto += (a[i] == i);
}
int maxcnt(cnto), l(1), r(1);
for (int i = 1; i <= nArr; ++i) {
sum[i] = sum[i - 1];
for (int it = 0; it < sz(B[i]); ++it) {
int j(B[i][it]);
if(i != j) {
++cnt[i + j];
int midc = ((i - j + 1) & 1) ? (a[j + (i - j + 1) / 2] == (j + (i - j + 1) / 2)) : 0;
//cout << j << ' ' << i << ' ' << i + j << ' ' << cnt[i + j] << ' ' << sum[i] - sum[j - 1] - midc << '\n';
if(maxcnt < cnt[i + j] + cnto - sum[i] + sum[j - 1] + midc) {
maxcnt = cnt[i + j] + cnto - sum[i] + sum[j - 1] + midc;
l = j, r = i;
}
} else {
++sum[i];
}
}
}
//brute();
//cout << maxcnt << '\n';
cout << a[l] << ' ' << a[r] << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
process();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
12048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12244 KB |
Output is correct |
2 |
Correct |
6 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12244 KB |
Output is correct |
2 |
Correct |
6 ms |
12076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12404 KB |
Output is correct |
2 |
Correct |
37 ms |
20984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
15316 KB |
Output is correct |
2 |
Correct |
18 ms |
14988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
29960 KB |
Output is correct |
2 |
Correct |
62 ms |
27052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
22456 KB |
Output is correct |
2 |
Correct |
58 ms |
34124 KB |
Output is correct |