#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], ans[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;
vector <int> gn; gn.clear();
for (int i = 1; i <= n; i++) {gn.pb(i); cin >> a[i]; pos[a[i]] = i; ans[i] = -1;}
for (int i = 1; i <= n; i++)
{
pr[i] = 0;
if (i != 1) pr[i] = pr[i - 1];
if (a[i] == i) pr[i]++;
}
sort(gn.begin(), gn.end(), cmp);
for (int i = 1; i <= n; i++)
{
int l = i, r = pos[i];
if (l > r) swap(l, r);
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;
if (sum > an)
{
an = sum;
L = l; R = r;
}
j++;
}
j = 0;
while (j < sz(midr[i]))
{
int len = abs(pos[midr[i][j]] - midr[i][j]) / 2;
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 |
20 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23808 KB |
Output is correct |
2 |
Correct |
20 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23936 KB |
Output is correct |
2 |
Correct |
21 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 |
21 ms |
24064 KB |
Output is correct |
2 |
Correct |
18 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
24320 KB |
Output is correct |
2 |
Correct |
194 ms |
34792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
28188 KB |
Output is correct |
2 |
Correct |
75 ms |
27204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
48008 KB |
Output is correct |
2 |
Correct |
317 ms |
40060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
37992 KB |
Output is correct |
2 |
Correct |
134 ms |
51692 KB |
Output is correct |