Submission #743990

# Submission time Handle Problem Language Result Execution time Memory
743990 2023-05-18T07:01:50 Z becaido Koala Game (APIO17_koala) C++17
62 / 100
55 ms 372 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "koala.h"
#endif // WAIMAI

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

#ifdef WAIMAI
void playRound(int *B, int *R);
#endif // WAIMAI

const int SIZE = 105;

int a[SIZE], b[SIZE];

int minValue(int N, int W) {
    a[0] = 1;
    fill(a + 1, a + N, 0);
    playRound(a, b);
    FOR (i, 0, N - 1) if (b[i] == 0) return i;
}

int maxValue(int N, int W) {
    vector<int> big(N);
    iota(big.begin(), big.end(), 0);
    while (big.size() > 1) {
        int w = W / big.size();
        fill(a, a + N, 0);
        for (int p : big) a[p] = w;
        playRound(a, b);
        vector<int> tmp;
        for (int p : big) if (b[p] > a[p]) tmp.pb(p);
        big = tmp;
    }
    if (big.size() == 0) return -1;
    return big[0];
}

unordered_map<int, int> mp[SIZE];
int greaterValue(int N, int W, int p1, int p2) {
    if (mp[p1].count(p2)) return mp[p1][p2];
    int l = 1, r = W == 6 ? W / 2 : W / 8;
    while (l < r) {
        int mid = (l + r) / 2;
        fill(a, a + N, 0);
        a[p1] = a[p2] = mid;
        playRound(a, b);
        bool big1 = b[p1] > a[p1], big2 = b[p2] > a[p2];
        if (big1 && big2) l = mid + 1;
        else if (!big1 && !big2) r = mid - 1;
        else return mp[p1][p2] = mp[p2][p1] = (big1 ? p1 : p2);
    }
}
int greaterValue(int N, int W) {
    FOR (i, 0, N - 1) mp[i].clear();
    return greaterValue(N, W, 0, 1);
}

void allValues(int N, int W, int *P) {
    vector<int> pos(N);
    iota(pos.begin(), pos.end(), 0);
    FOR (i, 0, N - 1) mp[i].clear();
    if (W == 2 * N) {
        sort(pos.begin(), pos.end(), [&](int lhs, int rhs) {
            if (mp[lhs].count(rhs)) return mp[lhs][rhs] == rhs;
            fill(a, a + N, 0);
            a[lhs] = a[rhs] = N;
            playRound(a, b);
            if (b[rhs] > a[rhs]) mp[lhs][rhs] = mp[rhs][lhs] = rhs;
            else mp[lhs][rhs] = mp[rhs][lhs] = lhs;
            return b[rhs] > a[rhs];
        });
    } else {
        sort(pos.begin(), pos.end(), [&](int lhs, int rhs) {
            return greaterValue(N, W, lhs, rhs) == rhs;
        });
    }
    FOR (i, 0, N - 1) P[pos[i]] = i + 1;
}

/*
4 1
100 100 1
in1
1 1
6 6 5 3 2 1 6 4
out1
3
in2
2 1
6 6 5 3 2 1 6 4
out2
4
in3
3 1
6 6
5 3 2 1 6 4
out3
0
in4
4 1
6 12 5 3 2 1 6 4
in5
4 1
6 6 5 3 2 1 6 4
*/

#ifdef WAIMAI
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];
    }
}

int maxq;
mt19937 rng(time(NULL));
static void runGame(int F) {
    int i;

    scanf("%d %d",&N,&W);
    //N = W = 100;

    //printf("random?\n");
    int x = 1;
    //scanf("%d", &x);
    if (x == 1) {
        //printf("random array = ");
        iota(P, P + N, 1);
        shuffle(P, P + N, rng);
        //for (int i = 0; i < N; i++) printf("%d ", P[i]);
        //printf("\n");
    } else {
        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);
    maxq = max(maxq, numQueries);
}

static void grader() {
    int i;

    int F, G;
    scanf("%d %d",&F,&G);

    for (i=0;i<G;i++) {
        runGame(F);
    }
    printf("maxQ = %d\n", maxq);
}
#endif // WAIMAI

Compilation message

koala.cpp: In function 'int minValue(int, int)':
koala.cpp:39:1: warning: control reaches end of non-void function [-Wreturn-type]
   39 | }
      | ^
koala.cpp: In function 'int greaterValue(int, int, int, int)':
koala.cpp:71:1: warning: control reaches end of non-void function [-Wreturn-type]
   71 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 208 KB Output is correct
2 Correct 4 ms 208 KB Output is correct
3 Correct 4 ms 208 KB Output is correct
4 Correct 4 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 320 KB Output is correct
2 Correct 12 ms 208 KB Output is correct
3 Correct 16 ms 320 KB Output is correct
4 Correct 12 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 340 KB Output is correct
2 Correct 51 ms 332 KB Output is correct
3 Correct 42 ms 344 KB Output is correct
4 Correct 47 ms 340 KB Output is correct
5 Correct 51 ms 324 KB Output is correct
6 Correct 55 ms 324 KB Output is correct
7 Correct 51 ms 332 KB Output is correct
8 Correct 50 ms 328 KB Output is correct
9 Correct 53 ms 336 KB Output is correct
10 Correct 48 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 336 KB Output is correct
2 Correct 44 ms 368 KB Output is correct
3 Correct 33 ms 356 KB Output is correct
4 Correct 33 ms 352 KB Output is correct
5 Correct 33 ms 336 KB Output is correct
6 Incorrect 36 ms 336 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Partially correct 20 ms 360 KB Output is partially correct
2 Partially correct 27 ms 348 KB Output is partially correct
3 Partially correct 25 ms 356 KB Output is partially correct
4 Partially correct 23 ms 352 KB Output is partially correct
5 Partially correct 29 ms 348 KB Output is partially correct
6 Partially correct 26 ms 344 KB Output is partially correct
7 Partially correct 23 ms 336 KB Output is partially correct
8 Partially correct 23 ms 336 KB Output is partially correct
9 Partially correct 24 ms 344 KB Output is partially correct
10 Partially correct 22 ms 336 KB Output is partially correct
11 Partially correct 24 ms 336 KB Output is partially correct
12 Partially correct 15 ms 336 KB Output is partially correct
13 Partially correct 24 ms 356 KB Output is partially correct
14 Partially correct 24 ms 344 KB Output is partially correct
15 Partially correct 25 ms 364 KB Output is partially correct
16 Partially correct 23 ms 348 KB Output is partially correct
17 Partially correct 27 ms 356 KB Output is partially correct
18 Partially correct 25 ms 356 KB Output is partially correct
19 Partially correct 22 ms 336 KB Output is partially correct
20 Partially correct 22 ms 372 KB Output is partially correct
21 Partially correct 21 ms 372 KB Output is partially correct
22 Partially correct 24 ms 336 KB Output is partially correct
23 Partially correct 22 ms 348 KB Output is partially correct
24 Partially correct 25 ms 364 KB Output is partially correct
25 Partially correct 23 ms 336 KB Output is partially correct
26 Partially correct 27 ms 336 KB Output is partially correct
27 Partially correct 24 ms 364 KB Output is partially correct
28 Partially correct 26 ms 336 KB Output is partially correct
29 Partially correct 24 ms 360 KB Output is partially correct
30 Partially correct 28 ms 340 KB Output is partially correct
31 Partially correct 30 ms 356 KB Output is partially correct
32 Partially correct 26 ms 344 KB Output is partially correct
33 Partially correct 31 ms 360 KB Output is partially correct
34 Partially correct 22 ms 348 KB Output is partially correct
35 Partially correct 24 ms 336 KB Output is partially correct
36 Partially correct 24 ms 336 KB Output is partially correct
37 Partially correct 24 ms 352 KB Output is partially correct
38 Partially correct 24 ms 356 KB Output is partially correct
39 Partially correct 25 ms 372 KB Output is partially correct
40 Partially correct 27 ms 352 KB Output is partially correct