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>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define sh cin.tie(0); cin.sync_with_stdio(0); cout.tie(0);
#define FILE freopen("test.in", "r", stdin);
#define vprint(v) for (int ii = 0; ii < v.size(); ii++){cout << v[ii] << " ";}
#define debugv(v) if (v.size() != 0) {cout << "[ "; for (int __ = 0; __ < (int)(v.size()) - 1; __++){cout << v[__] << ", ";} cout << v[(int)(v.size()) - 1] << " ]" << endl;} else {cout << "[]" << endl;}
#define debug cout << "-----------------------------------------------" << endl;
#define print1(a) cout << "{ " << a << " }" << endl;
#define print2(a, b) cout << "{ " << a << ", " << b << " }" << endl;
#define print3(a, b, c) cout << "{ " << a << ", " << b << ", " << c << " }" << endl;
#define print4(a, b, c, d) cout << "{ " << a << ", " << b << ", " << c << ", " << d << " }" << endl;
using namespace std;
//#define int long long
const int INF = 1e9 + 228;
const int MAXN = 5e5 + 228;
vector<int> rot[MAXN][2];
int pos[MAXN];
signed main()
{
#ifdef LOCAL
FILE;
#endif
sh;
int n;
cin >> n;
//assert(n != 2);
vector<int> v;
for (int i = 0; i < n; i++) {
int aa;
cin >> aa;
v.pb(aa - 1);
}
for (int i = 0; i < n; i++) {
int nw = v[i];
pos[nw] = i;
int len = abs(nw - i) + 1;
int mid = min(nw, i) + len / 2;
mid--;
//print4(i, nw, mid, len);
rot[mid + (len % 2)][len % 2].pb(min(nw, i));
}
for (int i = 0; i < n; i++) {
sort(all(rot[i][0]));sort(all(rot[i][1]));reverse(all(rot[i][1]));reverse(all(rot[i][0]));
}
vector<int> p;
p.pb(0);
for (int i = 0; i < n; i++) {
p.pb(p[i] + (v[i] == i));
}
int mx = 0, l = 0, r = 0;
for (int mid = 0; mid < n; mid++) {
int now = 0;
int re = 0;
for (auto to : rot[mid][0]) {
now++;
re = now - (p[mid + mid - to + 2] - p[to]);
if (re > mx) {
mx = re;
l = to;
r = mid + mid - to + 1;
//print4(mid, re, l, r);
}
}
//print1(mx);
now = 0;
re = 0;
for (auto to : rot[mid][1]) {
now++;
re = now - (p[mid + mid - to + 1] - p[to]);
if (re > mx) {
mx = re;
l = to;
r = mid + mid - to;
}
}
//print1(mx);
}
cout << v[l] + 1 << ' ' << v[r] + 1 << endl;
return 0;
}
# | 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... |