#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e3+10;
struct Node
{
Node *l, *r;
int v;
Node()
{
l = r = NULL;
v = 0;
}
};
Node *version[maxn];
int num[maxn], soma[maxn], n;
void build(Node *node, int l, int r)
{
if (l == r) return;
int mid = (l+r)>>1;
node->l = new Node(), node->r = new Node();
build(node->l, l, mid); build(node->r, mid+1, r);
}
void upd(Node *prev, Node *node, int l, int r, int pos)
{
if (l == r)
{
node->v = prev->v+1;
return;
}
int mid = (l+r)>>1;
if (pos <= mid)
{
node->r = prev->r;
if (node->l == NULL) node->l = new Node();
upd(prev->l, node->l, l, mid, pos);
}
else
{
node->l = prev->l;
if (node->r == NULL) node->r = new Node();
upd(prev->r, node->r, mid+1, r, pos);
}
node->v = node->l->v+node->r->v;
}
int query(Node *prev, Node *node, int l, int r, int pos)
{
if (l == r) return node->v-prev->v;
int mid = (l+r)>>1;
if (pos <= mid) return query(prev->l, node->l, l, mid, pos);
return query(prev->r, node->r, mid+1, r, pos);
}
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];
}
version[0] = new Node();
build(version[0], 1, n);
for (int i = 1; i <= n; i++)
{
version[i] = new Node();
upd(version[i-1], version[i], 1, n, num[i]+i);
}
int ans = 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]];
aux += query(version[i-1], version[num[i]], 1, n, num[i]+i);
if (aux > ans)
{
ans = 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];
aux += query(version[num[i]-1], version[i], 1, n, num[i]+i);
if (aux > ans)
{
ans = aux;
ind1 = num[num[i]], ind2 = num[i];
}
}
cout << ind1 << " " << ind2 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
380 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
584 KB |
Output is correct |
2 |
Incorrect |
2 ms |
760 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
1444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
2764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2896 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2896 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
19 ms |
9284 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
9284 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
9284 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
9284 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |