#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 500001
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
typedef long double ld;
typedef long long ll;
typedef short int si;
vector <int> mid[N], midr[N];
int an, L = 1, R = 1, pr[N], pos[N], a[N];
bool cmp(int x, int y) {return abs(pos[x] - x) < abs(pos[y] - y);}
int main()
{
// freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {cin >> a[i]; pos[a[i]] = i;}
for (int i = 1; i <= n; i++)
{
pr[i] = 0;
if (i != 1) pr[i] = pr[i - 1];
if (a[i] == i) pr[i]++;
}
for (int i = 1; i <= n; i++)
{
int l = i, r = pos[i];
if (l > r) swap(l, r);
if (l == r) continue;
if ((r - l) % 2 == 0) mid[(r + l) / 2].pb(i); else midr[(r + l) / 2].pb(i);
}
for (int i = 1; i <= n; i++)
{
sort(mid[i].begin(), mid[i].end(), cmp);
sort(midr[i].begin(), midr[i].end(), cmp);
int j = 0;
while (j < sz(mid[i]))
{
int len = abs(pos[mid[i][j]] - i);
int l = i - len, r = i + len;
int sum = pr[r] - pr[l - 1] + j + 1 - (a[i] == i ? 1 : 0);
if (sum > an)
{
an = sum;
L = l; R = r;
}
j++;
}
j = 0;
while (j < sz(midr[i]))
{
int len = abs(pos[midr[i][j]] - i);
int l = i - len, r = i + len + 1;
int sum = pr[r] - pr[l - 1] + j + 1;
if (sum > an)
{
an = sum;
L = l; R = r;
}
j++;
}
}
cout << a[L] << " " << a[R] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
23808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23936 KB |
Output is correct |
2 |
Correct |
19 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
24064 KB |
Output is correct |
2 |
Correct |
19 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23936 KB |
Output is correct |
2 |
Correct |
18 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24320 KB |
Output is correct |
2 |
Correct |
128 ms |
29552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
27008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
248 ms |
40824 KB |
Output is correct |
2 |
Incorrect |
203 ms |
31932 KB |
Integer 0 violates the range [1, 500000] |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
33848 KB |
Output is correct |
2 |
Correct |
89 ms |
29560 KB |
Output is correct |