Submission #1054159

# Submission time Handle Problem Language Result Execution time Memory
1054159 2024-08-12T07:07:25 Z 정민찬(#11058) Koala Game (APIO17_koala) C++14
100 / 100
30 ms 596 KB
#include "koala.h"
#include <bits/stdc++.h>

using namespace std;

void playRound(int *B, int *R);

int minValue(int N, int W) {
    vector<int> B(N, 0), R(N, 0);
    B[0] = 1;
    playRound(&B[0], &R[0]);
    for (int i=0; i<N; i++) {
        if (R[i] == 0) {
            return i;
        }
    }
}

int maxValue(int N, int W) {
    vector<int> B(N, 0), R(N, 0);
    vector<int> cand(N);
    iota(cand.begin(), cand.end(), 0);
    while (true) {
        int cnt = min(13, W / (int)cand.size());
        fill(B.begin(), B.end(), 0);
        for (auto &x : cand) {
            B[x] = cnt;
        }
        playRound(&B[0], &R[0]);
        vector<int> nxt;
        for (int i=0; i<N; i++) {
            if (B[i] && R[i]) {
                nxt.push_back(i);
            }
        }
        cand = nxt;
        if (cand.size() == 1) return cand[0];
    }
}

int greaterValue(int N, int W) {
    vector<int> B(N, 0), R(N, 0);
    int lo = 1, hi = 14;
    while (lo <= hi) {
        int mid = lo + hi >> 1;
        B[0] = mid; B[1] = mid;
        playRound(&B[0], &R[0]);
        if (R[0] > mid && R[1] <= mid) return 0;
        if (R[1] > mid && R[0] <= mid) return 1;
        if (R[0] <= mid && R[1] <= mid) hi = mid - 1;
        else lo = mid + 1;
    }
}

int greaterValue2(int N, int W, int x, int y) {
    vector<int> B(N, 0), R(N, 0);
    int lo = 1, hi = 14;
    while (lo <= hi) {
        int mid = lo + hi >> 1;
        B[x] = mid; B[y] = mid;
        playRound(&B[0], &R[0]);
        if (R[x] > mid && R[y] <= mid) return 0;
        if (R[y] > mid && R[x] <= mid) return 1;
        if (R[x] <= mid && R[y] <= mid) hi = mid - 1;
        else lo = mid + 1;
    }
}

void cal(int l, int r, int N, int W, vector<int> &ids, int *P) {
    if (l == r) {
        assert(ids.size() == 1);
        P[ids[0]] = l;
        return;
    }
    int cnt = W / (r-l+1);
    if (W == N && cnt + 1 > r-l+1) {
        while (true) {
            if (cnt + 1 <= r-l+1) break;
            int sum = 0;
            int t = 0;
            for (int i=1; i<=N; i++) {
                if (l <= i && i <= r) continue;
                sum += i;
                t ++;
                if (t == cnt + 1 - (r-l+1)) break;
            }
            if (sum < r) break;
            cnt --;
        }
    }
    vector<int> B(N, 0), R(N, 0);
    for (auto &x : ids) {
        B[x] = cnt;
    }
    playRound(&B[0], &R[0]);
    vector<int> lids, rids;
    for (auto &x : ids) {
        if (R[x]) rids.push_back(x);
        else lids.push_back(x);
    }
    cal(l, l+lids.size()-1, N, W, lids, P);
    cal(l+lids.size(), r, N, W, rids, P);
}

void allValues(int N, int W, int *P) {
    vector<int> ids(N);
    iota(ids.begin(), ids.end(), 0);
    cal(1, N, N, W, ids, P);
}
/*
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:45:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |         int mid = lo + hi >> 1;
      |                   ~~~^~~~
koala.cpp: In function 'int greaterValue2(int, int, int, int)':
koala.cpp:59:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |         int mid = lo + hi >> 1;
      |                   ~~~^~~~
koala.cpp: In function 'int minValue(int, int)':
koala.cpp:9:23: warning: control reaches end of non-void function [-Wreturn-type]
    9 |     vector<int> B(N, 0), R(N, 0);
      |                       ^
koala.cpp: In function 'int greaterValue(int, int)':
koala.cpp:42:23: warning: control reaches end of non-void function [-Wreturn-type]
   42 |     vector<int> B(N, 0), R(N, 0);
      |                       ^
koala.cpp: In function 'int greaterValue2(int, int, int, int)':
koala.cpp:56:23: warning: control reaches end of non-void function [-Wreturn-type]
   56 |     vector<int> B(N, 0), R(N, 0);
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 4 ms 344 KB Output is correct
3 Correct 3 ms 344 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 10 ms 452 KB Output is correct
3 Correct 9 ms 344 KB Output is correct
4 Correct 8 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 344 KB Output is correct
2 Correct 30 ms 344 KB Output is correct
3 Correct 26 ms 472 KB Output is correct
4 Correct 27 ms 344 KB Output is correct
5 Correct 26 ms 460 KB Output is correct
6 Correct 30 ms 344 KB Output is correct
7 Correct 26 ms 344 KB Output is correct
8 Correct 26 ms 344 KB Output is correct
9 Correct 27 ms 344 KB Output is correct
10 Correct 26 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 596 KB Output is correct
2 Correct 4 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 4 ms 456 KB Output is correct
6 Correct 4 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 4 ms 456 KB Output is correct
12 Correct 5 ms 340 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 4 ms 456 KB Output is correct
15 Correct 4 ms 344 KB Output is correct
16 Correct 4 ms 344 KB Output is correct
17 Correct 4 ms 344 KB Output is correct
18 Correct 5 ms 344 KB Output is correct
19 Correct 5 ms 344 KB Output is correct
20 Correct 4 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Correct 2 ms 344 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 344 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 2 ms 476 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 2 ms 344 KB Output is correct
11 Correct 2 ms 344 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 2 ms 344 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 2 ms 344 KB Output is correct
20 Correct 2 ms 344 KB Output is correct
21 Correct 2 ms 344 KB Output is correct
22 Correct 2 ms 344 KB Output is correct
23 Correct 2 ms 344 KB Output is correct
24 Correct 2 ms 344 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 344 KB Output is correct
27 Correct 2 ms 344 KB Output is correct
28 Correct 2 ms 344 KB Output is correct
29 Correct 2 ms 344 KB Output is correct
30 Correct 2 ms 344 KB Output is correct
31 Correct 2 ms 344 KB Output is correct
32 Correct 2 ms 344 KB Output is correct
33 Correct 2 ms 344 KB Output is correct
34 Correct 2 ms 344 KB Output is correct
35 Correct 2 ms 344 KB Output is correct
36 Correct 2 ms 344 KB Output is correct
37 Correct 2 ms 344 KB Output is correct
38 Correct 2 ms 344 KB Output is correct
39 Correct 2 ms 344 KB Output is correct
40 Correct 2 ms 464 KB Output is correct