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>
using namespace std;
const int maxn = 5e5+10;
int n, num[maxn], soma[maxn];
vector<int> pos[2*maxn];
int main(void)
{
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> num[i];
if (num[i] == i) soma[i] = soma[i-1]+1;
else soma[i] = soma[i-1];
pos[num[i]+i].push_back(i);
}
int anss = 0, ind1 = num[1], ind2 = num[2];
for (int i = 1; i <= n; i++)
{
if (num[i] < i) continue;
int aux = soma[i-1]+soma[n]-soma[num[i]]+pos[num[i]+i].size();
int ini = 0, fim = pos[num[i]+i].size()-1, ans = -1;
while (ini <= fim)
{
int mid = (ini+fim)>>1;
if (pos[num[i]+i][mid] >= i) fim = mid-1;
else ans = mid, ini = mid+1;
}
aux -= (ans+1);
ini = 0, fim = pos[num[i]+i].size()-1, ans = pos[num[i]+i].size();
while (ini <= fim)
{
int mid = (ini+fim)>>1;
if (pos[num[i]+i][mid] <= num[i]) ini = mid+1;
else ans = mid, fim = mid-1;
}
aux -= (pos[num[i]+i].size()-ans);
if (aux > anss)
{
anss = aux;
ind1 = num[i], ind2 = num[num[i]];
}
}
for (int i = 1; i <= n; i++)
{
if (num[i] > i) continue;
int aux = soma[n]-soma[i]+soma[num[i]-1]+pos[num[i]+i].size();
int ini = 0, fim = pos[num[i]+i].size()-1, ans = -1;
while (ini <= fim)
{
int mid = (ini+fim)>>1;
if (pos[num[i]+i][mid] >= num[i]) fim = mid-1;
else ans = mid, ini = mid+1;
}
aux -= (ans+1);
ini = 0, fim = pos[num[i]+i].size()-1, ans = pos[num[i]+i].size();
while (ini <= fim)
{
int mid = (ini+fim)>>1;
if (pos[num[i]+i][mid] <= i) ini = mid+1;
else ans = mid, fim = mid-1;
}
aux -= (pos[num[i]+i].size()-ans);
if (aux > anss)
{
anss = aux;
ind1 = num[num[i]], ind2 = num[i];
}
}
cout << ind1 << " " << ind2 << "\n";
}
# | 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... |