제출 #152448

#제출 시각아이디문제언어결과실행 시간메모리
152448qkxwsmThe Big Prize (IOI17_prize)C++14
97.58 / 100
69 ms2180 KiB
/*
PROG: template
LANG: C++11
    _____
  .'     '.
 /  0   0  \
|     ^     |
|  \     /  |
 \  '---'  /
  '._____.'
 */
#include <bits/stdc++.h>
#include "prize.h"

using namespace std;

template<class T>
void readi(T &x)
{
	T input = 0;
	bool negative = false;
	char c = ' ';
	while (c < '-')
	{
		c = getchar();
	}
	if (c == '-')
	{
		negative = true;
		c = getchar();
	}
	while (c >= '0')
	{
		input = input * 10 + (c - '0');
		c = getchar();
	}
	if (negative)
	{
		input = -input;
	}
	x = input;
}
template<class T>
void printi(T output)
{
	if (output == 0)
	{
		putchar('0');
		return;
	}
	if (output < 0)
	{
		putchar('-');
		output = -output;
	}
	int aout[20];
	int ilen = 0;
	while(output)
	{
		aout[ilen] = ((output % 10));
		output /= 10;
		ilen++;
	}
	for (int i = ilen - 1; i >= 0; i--)
	{
		putchar(aout[i] + '0');
	}
	return;
}
template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
long long randomize(long long mod)
{
	return ((1ll << 30) * rand() + (1ll << 15) * rand() + rand()) % mod;
}

#define MP make_pair
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-20;

#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 200013

long long normalize(long long x, long long mod = INF)
{
	return (((x % mod) + mod) % mod);
}

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N, M;
bool vis[MAXN];
pii quer[MAXN];

void get(int idx)
{
	if (vis[idx])
	{
		return;
	}
	vis[idx] = true;
	vector<int> res = ask(idx);
	quer[idx] = MP(res[0], res[1]);
//	cerr << idx << ' ' << quer[idx].fi << ' ' << quer[idx].se << endl;
//	if (quer[idx].fi + quer[idx].se != M)
//	{
//		special[idx] = true;
////		cerr << "special " << idx << endl;
//	}
	return;
}
void solve(int L, int R)
{
	//guarantee that neither L, R are special and both are queried
	if (L > R) return;
	if (quer[L].fi == quer[R].fi) return;
//	cerr << "solve " << L << ' ' << R << endl;
	int mid;
	mid = (L + R)/2;
	for (; mid <= R; mid++)
	{
		get(mid);
		if (quer[mid].fi + quer[mid].se == M)
		{
			break;
		}
	}
	solve(mid, R);
	mid = (L + R)/2;
	for (; mid >= L; mid--)
	{
		get(mid);
		if (quer[mid].fi + quer[mid].se == M)
		{
			break;
		}
	}
	solve(L, mid);
	//at least one special guy in L...lmid - 1
}
int find_best(int n)
{
	//at most 6 distinct prizes
	//711 guys are not 6
	//query 712 times, then you get to know the location of a max
	//1, 2, 5, 26, 677, whatever
	//1, 3, 8, 34, 711, 200000
	N = n;
	if (N == 1) return 0;
	//question is: you want to query at most 5000 times and find the 1
	//find the locations of all the not1's in O(fast)
	//there are 711 flags in an array of 200000, can you find the locations of all of them, given how many are <= x
	for (int i = 0; i < min(240, N); i++)
	{
		get(i);
		ckmax(M, quer[i].fi + quer[i].se);
	}
	for (int i = 0; i < min(240, N); i++)
	{
		get(N - 1 - i);
		ckmax(M, quer[N - 1 - i].fi + quer[N - 1 - i].se);
	}
//	cerr << M << endl;
	int lt = 0, rt = N - 1;
	for (; lt < N; lt++)
	{
		get(lt);
		if (quer[lt].fi + quer[lt].se == M)
		{
			break;
		}
	}
	for (; rt >= 0; rt--)
	{
		get(rt);
		if (quer[rt].fi + quer[rt].se == M)
		{
			break;
		}
	}
//	cerr << lt << ' ' << rt << endl;
	solve(lt, rt);
	for (int i = 0; i < N; i++)
	{
		if (vis[i] && (quer[i].fi + quer[i].se != M))
		{
			get(i);
			if (quer[i].fi + quer[i].se == 0)
			{
				return i;
			}
		}
	}
	assert(false);
	//M is the # of special guys
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...