Submission #82231

# Submission time Handle Problem Language Result Execution time Memory
82231 2018-10-29T14:48:20 Z luciocf Doktor (COCI17_doktor) C++14
100 / 100
326 ms 43260 KB
#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
1 Correct 24 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 24052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 24052 KB Output is correct
2 Correct 24 ms 24052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24052 KB Output is correct
2 Correct 36 ms 24148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 24148 KB Output is correct
2 Correct 23 ms 24148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24148 KB Output is correct
2 Correct 24 ms 24148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 24384 KB Output is correct
2 Correct 103 ms 28344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 28344 KB Output is correct
2 Correct 51 ms 28344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 326 ms 39480 KB Output is correct
2 Correct 160 ms 39480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 39480 KB Output is correct
2 Correct 128 ms 43260 KB Output is correct