답안 #166436

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
166436 2019-12-02T12:28:45 Z dolphingarlic 코알라 (APIO17_koala) C++14
100 / 100
69 ms 564 KB
#include "koala.h"
#include <bits/stdc++.h>
#pragma GCC Optimize("unroll-loops")
#pragma GCC Optimize("O3")
#pragma GCC target("sse4,avx2,fma,avx")
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;
 
int B[100], R[100];
 
/*
HELPER FUNCTIONS
*/
 
bool compare(int a, int b, int N, int W) {
    fill(B, B + N, 0);
    B[a] = B[b] = W;
    playRound(B, R);
    return (R[b] > W);
}
vector<int> mergesort(vector<int> v, int N, int W) {
    if (v.size() == 1) return v;
 
    vector<int> a, b;
    a.insert(a.begin(), v.begin(), v.begin() + (v.size() + 1) / 2);
    b.insert(b.begin(), v.begin() + (v.size() + 1) / 2, v.end());
    a = mergesort(a, N, W), b = mergesort(b, N, W);
 
    vector<int> sorted;
    int aptr = 0, bptr = 0;
    while (aptr < a.size() && bptr < b.size()) {
        if (compare(a[aptr], b[bptr], N, W)) sorted.push_back(a[aptr++]);
        else sorted.push_back(b[bptr++]);
    }
    sorted.insert(sorted.end(), a.begin() + aptr, a.end());
    sorted.insert(sorted.end(), b.begin() + bptr, b.end());
 
    return sorted;
}
 
void subtask5(vector<int> v, int N, int W, int* P, int l = 1, int r = 100) {
    if (l == r) P[v[0]] = l;
    else {
        int x = min((int)sqrt(2 * l), W / (r - l + 1));
 
        fill(B, B + N, 0);
        for (int i : v) B[i] = x;
 
        playRound(B, R);
        vector<int> less, greater;
        for (int i : v) if (R[i] > x) greater.push_back(i);
        else less.push_back(i);
 
        subtask5(less, N, W, P, l, l + less.size() - 1);
        subtask5(greater, N, W, P, r - greater.size() + 1, r);
    }
}
 
/*
BEGIN ACTUAL FUNCTIONS
*/
 
// Assign a single stone to the first cup
// If Koala picks that cup, it is not the smallest and some other cup has 0
// Otherwise, cup 0 is the smallest cup
// Uses 1 turn
int minValue(int N, int W) {
    fill(B, B + N, 0);
    fill(R, R + N, 0);
    B[0] = 1;
    playRound(B, R);
    if (R[0] < 2) return 0;
    else FOR(i, 1, N) if (!R[i]) return i;
    return -1;
}
 
// Binary search for the max value
// Uses like 4 turns
int maxValue(int N, int W) {
    vector<int> v;
    FOR(i, 0, N) v.push_back(i);
    while (v.size() != 1) {
        int k = W / v.size();
        fill(B, B + N, 0);
        for (int i : v) B[i] = k;
        playRound(B, R);
        v.clear();
        FOR(i, 0, N)
        if (R[i] > k) v.push_back(i);
    }
    return v[0];
}
 
// Assign positions 0 and 1 x stones until Koala treats them differently
// Binary search for x:
// If both 0 and 1 get > x stones, increase x
// If both 0 and 1 get < x stones, decrease x
// Notice how x <= 9 because sum(9..17) > 100
// Uses like 3 turns
int greaterValue(int N, int W) {
    int l = 1, r = 9;
    while (l != r) {
        int mid = (l + r) / 2;
        B[0] = B[1] = mid;
        playRound(B, R);
 
        if (R[0] > mid && R[1] > mid) l = mid + 1;
        else if (R[0] <= mid && R[1] <= mid) r = mid - 1;
        else return (R[0] < R[1]);
    }
    B[0] = B[1] = l;
    playRound(B, R);
    return (R[0] < R[1]);
}
 
// Subtask 4:
// Assign indices i and j 100 stones each to check whether P[i] > P[j]
// Use merge sort to get O(Nlog(N)) <= 700 turns
// Subtask 5:
// TODO
void allValues(int N, int W, int *P) {
    if (W == 2 * N) {
        vector<int> v;
        FOR(i, 0, N) v.push_back(i);
        vector<int> sorted = mergesort(v, N, W / 2);
        FOR(i, 0, N) P[sorted[i]] = i + 1;
    } else {
        vector<int> v;
        FOR(i, 0, N) v.push_back(i);
        subtask5(v, N, W, P);
    }
}

Compilation message

koala.cpp:3:0: warning: ignoring #pragma GCC Optimize [-Wunknown-pragmas]
 #pragma GCC Optimize("unroll-loops")
 
koala.cpp:4:0: warning: ignoring #pragma GCC Optimize [-Wunknown-pragmas]
 #pragma GCC Optimize("O3")
 
koala.cpp: In function 'std::vector<int> mergesort(std::vector<int>, int, int)':
koala.cpp:31:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (aptr < a.size() && bptr < b.size()) {
            ~~~~~^~~~~~~~~~
koala.cpp:31:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (aptr < a.size() && bptr < b.size()) {
                               ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 6 ms 376 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 376 KB Output is correct
2 Correct 16 ms 376 KB Output is correct
3 Correct 16 ms 376 KB Output is correct
4 Correct 16 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 376 KB Output is correct
2 Correct 69 ms 376 KB Output is correct
3 Correct 60 ms 388 KB Output is correct
4 Correct 60 ms 504 KB Output is correct
5 Correct 60 ms 376 KB Output is correct
6 Correct 61 ms 504 KB Output is correct
7 Correct 62 ms 504 KB Output is correct
8 Correct 61 ms 440 KB Output is correct
9 Correct 60 ms 376 KB Output is correct
10 Correct 60 ms 564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 376 KB Output is correct
2 Correct 35 ms 376 KB Output is correct
3 Correct 35 ms 416 KB Output is correct
4 Correct 34 ms 376 KB Output is correct
5 Correct 33 ms 376 KB Output is correct
6 Correct 34 ms 412 KB Output is correct
7 Correct 34 ms 376 KB Output is correct
8 Correct 33 ms 376 KB Output is correct
9 Correct 33 ms 376 KB Output is correct
10 Correct 32 ms 376 KB Output is correct
11 Correct 34 ms 504 KB Output is correct
12 Correct 20 ms 376 KB Output is correct
13 Correct 34 ms 376 KB Output is correct
14 Correct 31 ms 376 KB Output is correct
15 Correct 31 ms 376 KB Output is correct
16 Correct 30 ms 376 KB Output is correct
17 Correct 32 ms 376 KB Output is correct
18 Correct 31 ms 376 KB Output is correct
19 Correct 31 ms 376 KB Output is correct
20 Correct 31 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 380 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 380 KB Output is correct
9 Correct 5 ms 380 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 380 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 6 ms 376 KB Output is correct
16 Correct 9 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 504 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 5 ms 376 KB Output is correct
27 Correct 5 ms 376 KB Output is correct
28 Correct 6 ms 380 KB Output is correct
29 Correct 5 ms 376 KB Output is correct
30 Correct 5 ms 376 KB Output is correct
31 Correct 5 ms 376 KB Output is correct
32 Correct 5 ms 376 KB Output is correct
33 Correct 5 ms 376 KB Output is correct
34 Correct 5 ms 376 KB Output is correct
35 Correct 5 ms 376 KB Output is correct
36 Correct 5 ms 376 KB Output is correct
37 Correct 5 ms 376 KB Output is correct
38 Correct 5 ms 376 KB Output is correct
39 Correct 5 ms 376 KB Output is correct
40 Correct 5 ms 424 KB Output is correct