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>
//#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;
}
# | 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... |