Submission #82216

# Submission time Handle Problem Language Result Execution time Memory
82216 2018-10-29T14:01:24 Z luciocf Doktor (COCI17_doktor) C++14
60 / 100
29 ms 11104 KB
#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, 2*n);	

	for (int i = 1; i <= n; i++)
	{
		version[i] = new Node();
		upd(version[i-1], version[i], 1, 2*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, 2*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, 2*n, num[i]+i);

		if (aux > ans)
		{
			ans = aux;
			ind1 = num[num[i]], ind2 = num[i];
		}
	}

	cout << ind1 << " " << ind2 << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 552 KB Output is correct
2 Correct 3 ms 696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1668 KB Output is correct
2 Correct 4 ms 1668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 3152 KB Output is correct
2 Correct 8 ms 3152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3536 KB Output is correct
2 Correct 2 ms 3536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 11104 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 11104 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 11104 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 11104 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -