#include "koala.h"
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 1; (i) <= (n); ++i)
#define forn(i, l, r) for(int i = (l); i <= (r); ++i)
#define ford(i, r, l) for(int i = (r); i >= (l); --i)
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define FORD(i, n) for(int i = ((n) - 1); i >= 0; --i)
mt19937 rng(192929);
const int MaxN = 2e5 + 3;
int b[MaxN], r[MaxN];
int minValue(int N, int W) {
int x = rng() % N;
FOR(i, N) b[i] = 0;
b[x] = 1;
playRound(b, r);
int pos_x = 0;
while(r[pos_x]) ++pos_x;
if(r[x] == 2) return pos_x;
int y = rng() % N ;
b[x] = 0;
b[y] = 1;
playRound(b, r);
int pos_y;
while(r[pos_y]) ++pos_y;
if(r[y] == 2) return pos_y;
if(pos_x == pos_y) return pos_x;
return (rng() & 1) ? pos_x : pos_y;
}
int potential[MaxN];
int maxValue(int N, int W) {
FOR(i, N) potential[i] = 1;
while(true)
{
int cnt = 0;
FOR(i, N)
cnt += potential[i];
if(cnt == 1)
FOR(i, N) if(potential[i]) return i;
if(cnt == 0) assert(0);
FOR(i, N)
if(potential[i])
b[i] = W / cnt;
else b[i] = 0;
playRound(b, r);
FOR(i, N ) if(r[i] && potential[i]) potential[i] = 1;
else potential[i] = 0;
}
return 0;
}
int greaterValue(int N, int W) {
int lb = 1, rb = min(W / 2, 9);
FOR(i, N) b[i] = 0;
while(lb <= rb)
{
int mid = lb + rb >> 1;
b[0] = b[1] = mid;
//FOR(i, N) cout << b[i] << " "; cout << endl;
playRound(b, r);
int c1 = r[0] > b[0], c2 = r[1] > b[1];
if(c1 ^ c2) return c1 < c2;
if(c1) lb = mid + 1;
else rb = mid - 1;
}
return 0;
}
void dnc(vector<int> &cur, int lb, int rb, int *P, int N, int W, bool ok)
{
if(lb > rb) return;
if(lb == rb) P[cur[0]] = lb;
else
{
FOR(i, N) b[i] = 0;
for(int x : cur) b[x] = W / (rb - lb + 1);
playRound(b, r);
vector<int> L;
vector<int> R;
for(int x : cur) if(r[x] > b[x]) R.push_back(x);
else L.push_back(x);
dnc(L, lb, lb + (L.size()) - 1, P, N, W, ok);
dnc(R, rb - (R.size()) + 1, rb, P, N, W, ok);
}
}
void allValues(int N, int W, int *P) {
vector<int> cur;
FOR(i, N) cur.push_back(i);
dnc(cur, 1, N , P, N, W, W == N);
}
static int N, W;
static int P[105];
static int maxQueries = 3200;
static int numQueries;
static void runGame(int F);
static void grader();
int main() {
grader();
return 0;
}
void playRound(int *B, int *R) {
int i, j;
int S = 0;
for (i=0;i<N;++i) {
if ( !(B[i] >= 0 && B[i] <= W) ) {
printf("Invalid query.\n");
exit(0);
}
S += B[i];
}
if (S > W) {
printf("Invalid query.\n");
exit(0);
}
numQueries++;
if (numQueries > maxQueries) {
printf("Too many queries.\n");
exit(0);
}
int cache[2][205];
int num[2][205];
char taken[105][205];
for (i=0;i<205;++i) {
cache[1][i] = 0;
num[1][i] = 0;
}
for (i=0;i<N;++i) {
int v = B[i]+1;
int ii = i&1;
int o = ii^1;
for (j=0;j<=W;++j) {
cache[ii][j] = cache[o][j];
num[ii][j] = num[o][j];
taken[i][j] = 0;
}
for (j=W;j>=v;--j) {
int h = cache[o][j-v] + P[i];
int hn = num[o][j-v] + 1;
if (h > cache[ii][j] || (h == cache[ii][j] && hn > num[ii][j])) {
cache[ii][j] = h;
num[ii][j] = hn;
taken[i][j] = 1;
} else {
taken[i][j] = 0;
}
}
}
int cur = W;
for (i=N-1;i>=0;--i) {
R[i] = taken[i][cur] ? (B[i] + 1) : 0;
cur -= R[i];
}
}
static void runGame(int F) {
int i;
scanf("%d %d",&N,&W);
for (i=0;i<N;++i) {
scanf("%d",&P[i]);
}
numQueries = 0;
if (F == 1) {
printf("%d\n", minValue(N, W));
} else if (F == 2) {
printf("%d\n", maxValue(N, W));
} else if (F == 3) {
printf("%d\n", greaterValue(N, W));
} else if (F == 4) {
int userP[105];
allValues(N, W, userP);
for (i=0;i<N;i++) {
printf("%d ",userP[i]);
}
printf("\n");
}
printf("Made %d calls to playRound.\n", numQueries);
}
static void grader() {
int i;
int F, G;
scanf("%d %d",&F,&G);
for (i=0;i<G;i++) {
runGame(F);
}
}
Compilation message
koala.cpp: In function 'int greaterValue(int, int)':
koala.cpp:76:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
76 | int mid = lb + rb >> 1;
| ~~~^~~~
koala.cpp: In function 'void grader()':
koala.cpp:217:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
217 | scanf("%d %d",&F,&G);
| ~~~~~^~~~~~~~~~~~~~~
koala.cpp: In function 'void runGame(int)':
koala.cpp:190:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
190 | scanf("%d %d",&N,&W);
| ~~~~~^~~~~~~~~~~~~~~
koala.cpp:192:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
192 | scanf("%d",&P[i]);
| ~~~~~^~~~~~~~~~~~
koala.cpp: In function 'int minValue(int, int)':
koala.cpp:39:18: warning: 'pos_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
39 | while(r[pos_y]) ++pos_y;
| ~~~~~~~^
/usr/bin/ld: /tmp/ccGrkwlf.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccDEvQnc.o:koala.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccGrkwlf.o: in function `playRound(int*, int*)':
grader.cpp:(.text+0x280): multiple definition of `playRound(int*, int*)'; /tmp/ccDEvQnc.o:koala.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status