Submission #1080251

# Submission time Handle Problem Language Result Execution time Memory
1080251 2024-08-29T08:21:20 Z Elias The Big Prize (IOI17_prize) C++17
Compilation error
0 ms 0 KB
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long

#ifdef _DEBUG

int find_best(int n);
std::vector<int> ask(int i);
#else
#include "prize.h"
#endif

vector<int> a;
vector<int> b;

int N;

int segSet(int l, int r, int i, int k, int x)
{
	if (l > k || r <= k)
		return b[i];
	if (l + 1 == r)
		return a[l] = b[i] = x;
	int m = (l + r) / 2;
	return b[i] = segSet(l, m, i * 2 + 1, k, x) + segSet(m, r, i * 2 + 2, k, x);
}

int segGet(int l, int r, int i, int ql, int qr)
{
	if (ql <= l && qr >= r)
		return b[i];
	if (ql >= r || qr <= l)
		return 0;
	int m = (l + r) / 2;
	return segGet(l, m, i * 2 + 1, ql, qr) + segGet(m, r, i * 2 + 2, ql, qr);
}

int asks = 0;

int getRecursive(int l, int r, int cnt_here, int cnt_l, int cnt_r)
{
	if (l >= r)
		return -1;

	if (cnt_here <= 0)
		return -1;

	int m = (l + r) / 2;
	int i = m;

	vector<int> answer;
	bool is_special = false;
	int diff_l = 0;
	int diff_r = 0;

	do
	{
		if (i == r)
			break;

		is_special = false;
		asks++;
		assert(asks < 10000);
		assert(i >= 0 && i < n);
		answer = ask(i);

		if (answer[0] + answer[1] == 0)
			return i;

		answer[0] -= cnt_l;
		answer[1] -= cnt_r;

		if (answer[0] + answer[1] != cnt_here)
		{
			is_special = true;
			i++;
		}
		else
		{
			answer[0] -= i - m;
			diff_l += i - m;
			diff_r += i - m;
		}
	} while (is_special);

	int left = -1;
	if (i == r)
		left = getRecursive(l, m, cnt_here - (i - m), cnt_l, cnt_r + (i - m));
	else
		left = getRecursive(l, m, answer[0], cnt_l, cnt_r + answer[1] + diff_r);

	if (left != -1)
		return left;

	return getRecursive(i + 1, r, answer[1], cnt_l + answer[0] + diff_l, cnt_r);
}

int find_best(int n)
{

	vector<int> cnt_here;

	for (int k = 0; k < 20; k++)
	{
		int i = rand() % n;

		vector<int> answer = ask(i);

		cnt_here.push_back(answer[0] + answer[1]);
	}

	sort(cnt_here.begin(), cnt_here.end());

	int result = getRecursive(0, n, cnt_here.back(), 0, 0);
	

	assert(result != -1);

	return result;
}

#ifdef _DEBUG

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

static const int max_q = 10000;
static int n;
static int query_count = 0;
static vector<int> g;
static vector<vector<int>> rank_count;

vector<int> ask(int i)
{
	query_count++;
	if (query_count > max_q)
	{
		cerr << "Query limit exceeded" << endl;
		exit(0);
	}

	if (i < 0 || i >= n)
	{
		cerr << "Bad index: " << i << endl;
		exit(0);
	}

	vector<int> res(2);
	res[0] = rank_count[g[i] - 1][i + 1];
	res[1] = rank_count[g[i] - 1][n] - res[0];
	return res;
}

int main()
{
	cin >> n;

	g.resize(n);
	for (int i = 0; i < n; i++)
	{
		cin >> g[i];
		if (g[i] < 1)
		{
			cerr << "Invalid rank " << g[i] << " at index " << i << endl;
			exit(0);
		}
	}

	int max_rank = *max_element(g.begin(), g.end());

	rank_count.resize(max_rank + 1, vector<int>(n + 1, 0));
	for (int r = 0; r <= max_rank; r++)
	{
		for (int i = 1; i <= n; i++)
		{
			rank_count[r][i] = rank_count[r][i - 1];
			if (g[i - 1] == r)
				rank_count[r][i]++;
		}
	}

	for (int i = 0; i <= n; i++)
		for (int r = 1; r <= max_rank; r++)
			rank_count[r][i] += rank_count[r - 1][i];

	int res = find_best(n);
	assert(g[res] == 1);
	cout << res << endl
		 << "Query count: " << query_count << endl;

	return 0;
}

#endif

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from prize.cpp:4:
prize.cpp: In function 'int getRecursive(int, int, int, int, int)':
prize.cpp:69:24: error: 'n' was not declared in this scope
   69 |   assert(i >= 0 && i < n);
      |                        ^