제출 #408654

#제출 시각아이디문제언어결과실행 시간메모리
408654Kevin_Zhang_TW커다란 상품 (IOI17_prize)C++17
97.23 / 100
76 ms7600 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
 
#include "prize.h"
 
const int MAX_N = 300010;
 
vector<int> qry(int id) {
	static vector<int> mem[MAX_N];
	if (mem[id].size()) return mem[id];
	static int cnt;
	assert(++cnt <= 9000);
	return mem[id] = ask(id);
}
 
int sum(int id) { return qry(id)[0] + qry(id)[1]; }

int solve(int L, int R, int mxsum, int fr_small, int cnt_small) {
	if (L > R || cnt_small == 0) return 0;

	int M = L + R >> 1;

	if (sum(M) == mxsum) {
		int cnt = qry(M)[0], res = 0;
		return solve(L, M-1, mxsum, fr_small, cnt - fr_small) +
			solve(M+1, R, mxsum, cnt, cnt_small - (cnt - fr_small));
	}

	if (sum(M) == 0) return M;


	int NR = M, res = 0;
	while (NR >= L && sum(NR) < mxsum) {
		if (sum(NR) == 0) return NR;
		--NR;
	}
	if (NR >= L)
		res += solve(L, NR-1, mxsum, fr_small, qry(NR)[0] - fr_small);

	int NL = M;
	while (NL <= R && sum(NL) < mxsum) {
		if (sum(NL) == 0) return NL;
		++NL;
	}
	if (NL <= R)
		res += solve(NL+1, R, mxsum, qry(NL)[0], cnt_small - (qry(NL)[0] - fr_small));

	return res;
}
 
int find_best(int n) {
	int mxsum = 0, pos = -1, m = min(n, 500);
	for (int i = 0;i < m;++i) {
		chmax(mxsum, sum(i));
		if (mxsum == sum(i))
			pos = i;
		if (sum(i) == 0) return i;
	}
	return solve(m, n-1, mxsum, qry(pos)[0] + (m - pos - 1), mxsum - pos);
}

컴파일 시 표준 에러 (stderr) 메시지

prize.cpp: In function 'int solve(int, int, int, int, int)':
prize.cpp:35:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |  int M = L + R >> 1;
      |          ~~^~~
prize.cpp:38:24: warning: unused variable 'res' [-Wunused-variable]
   38 |   int cnt = qry(M)[0], res = 0;
      |                        ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...