답안 #784876

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
784876 2023-07-16T16:24:38 Z TheSahib 커다란 상품 (IOI17_prize) C++14
100 / 100
87 ms 16072 KB
#include "prize.h"
#include <bits/stdc++.h>
 
using namespace std;
 
const int BLOCK = 500;
const int MAX = 2e5 + 5;
 
int tree[MAX];
 
void update(int pos, int val){
    while(pos < MAX){
        tree[pos] += val;
        pos += pos & (-pos);
    }
}
 
int ask(int l, int r){
    if(r < l) return 0;
    if(l != 1) return ask(1, r) - ask(1, l - 1);
    int ans = 0;
    while(r > 0){
        ans += tree[r];
        r -= r & (-r);
    }
    return ans;
}
 
vector<int> mp[MAX];
 
vector<int> query(int i){
    if(!mp[i].empty()) return mp[i];
    return mp[i] = ask(i - 1);
}
 
 
int find_best(int n) {
    vector<int> order(n);
    srand(time(0));
    iota(order.begin(), order.end(), 1);
    random_shuffle(order.begin(), order.end());
    random_shuffle(order.begin(), order.end());

    vector<int> a;
    int cnt = 0;
    
    set<int> st;
    for (int i = 0; i < n; i++)
    {
        st.insert(i + 1);
    }
    for (int j = 0; j < min(1, n); ++j)
    {
        int i = order[j];
        a = query(i);
        if(a[0] + a[1] == 0) return i - 1;
        if(a[0] + a[1] < cnt){
            update(i, 1);
            st.erase(i);
        }
        else{
            cnt = a[0] + a[1];
        }
    }
    vector<int> blocks;
    int c = 1;
    while(true){
        auto itr = st.lower_bound(c);
        bool end = false;
        if(itr == st.end()){
            --itr;
            end = true;
        }
        a = query(*itr);
        if(a[0] + a[1] == 0) return (*itr) - 1;
        if(a[0] + a[1] < cnt){
            update(*itr, 1);
            st.erase(itr);
            continue;
        }
        c = *itr;
        blocks.push_back(c);
        c += MAX;
        if(end){
            break;
        }
    }

    for(int i = 0; i < blocks.size() - 1; ++i){
        int b1 = blocks[i], b2 = blocks[i + 1];
        while(query(b1)[1] - query(b2)[1] - ask(b1, b2) > 0){
            int l = b1, r = b2;
            while(l <= r){
                int x = (l + r) / 2;
                auto itr = st.lower_bound(x);
                if(itr == st.end() || (*itr) > r){
                    --itr;
                }
                int mid = *itr;

                a = query(mid);

                if(a[0] + a[1] == 0){
                    return mid - 1;
                }
                if(a[0] + a[1] < cnt){
                    update(mid, 1);
                    st.erase(mid);
                    break;
                }
                a[0] -= query(b1)[0] + ask(b1, mid);
                a[1] -= query(b2)[1] + ask(mid, b2);
    
                if(a[1] > 0){
                    l = mid + 1;
                }
                else{
                    r = mid - 1;
                }
            }
        }
    }
}

Compilation message

prize.cpp: In function 'int find_best(int)':
prize.cpp:89:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for(int i = 0; i < blocks.size() - 1; ++i){
      |                    ~~^~~~~~~~~~~~~~~~~~~
prize.cpp:38:24: warning: control reaches end of non-void function [-Wreturn-type]
   38 |     vector<int> order(n);
      |                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 15176 KB Output is correct
2 Correct 43 ms 15068 KB Output is correct
3 Correct 45 ms 15116 KB Output is correct
4 Correct 39 ms 15164 KB Output is correct
5 Correct 49 ms 15140 KB Output is correct
6 Correct 48 ms 15120 KB Output is correct
7 Correct 40 ms 15088 KB Output is correct
8 Correct 42 ms 15088 KB Output is correct
9 Correct 39 ms 15072 KB Output is correct
10 Correct 40 ms 15156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 15076 KB Output is correct
2 Correct 38 ms 15100 KB Output is correct
3 Correct 39 ms 15104 KB Output is correct
4 Correct 40 ms 15080 KB Output is correct
5 Correct 39 ms 15096 KB Output is correct
6 Correct 45 ms 15164 KB Output is correct
7 Correct 41 ms 15048 KB Output is correct
8 Correct 42 ms 15084 KB Output is correct
9 Correct 39 ms 15112 KB Output is correct
10 Correct 40 ms 15100 KB Output is correct
11 Correct 47 ms 15352 KB Output is correct
12 Correct 41 ms 15532 KB Output is correct
13 Correct 41 ms 15104 KB Output is correct
14 Correct 13 ms 6144 KB Output is correct
15 Correct 60 ms 15948 KB Output is correct
16 Correct 46 ms 15272 KB Output is correct
17 Correct 76 ms 16008 KB Output is correct
18 Correct 39 ms 15048 KB Output is correct
19 Correct 82 ms 16000 KB Output is correct
20 Correct 29 ms 10364 KB Output is correct
21 Correct 71 ms 15648 KB Output is correct
22 Correct 54 ms 16028 KB Output is correct
23 Correct 41 ms 15232 KB Output is correct
24 Correct 53 ms 15464 KB Output is correct
25 Correct 52 ms 15448 KB Output is correct
26 Correct 58 ms 15480 KB Output is correct
27 Correct 41 ms 15168 KB Output is correct
28 Correct 40 ms 15196 KB Output is correct
29 Correct 43 ms 15248 KB Output is correct
30 Correct 39 ms 15100 KB Output is correct
31 Correct 81 ms 15984 KB Output is correct
32 Correct 41 ms 15196 KB Output is correct
33 Correct 2 ms 4944 KB Output is correct
34 Correct 64 ms 15692 KB Output is correct
35 Correct 39 ms 15180 KB Output is correct
36 Correct 73 ms 15964 KB Output is correct
37 Correct 41 ms 15376 KB Output is correct
38 Correct 40 ms 15184 KB Output is correct
39 Correct 56 ms 15668 KB Output is correct
40 Correct 39 ms 15048 KB Output is correct
41 Correct 56 ms 15436 KB Output is correct
42 Correct 51 ms 15548 KB Output is correct
43 Correct 49 ms 15500 KB Output is correct
44 Correct 53 ms 15692 KB Output is correct
45 Correct 69 ms 15820 KB Output is correct
46 Correct 79 ms 15980 KB Output is correct
47 Correct 48 ms 15684 KB Output is correct
48 Correct 50 ms 15416 KB Output is correct
49 Correct 56 ms 16060 KB Output is correct
50 Correct 39 ms 15120 KB Output is correct
51 Correct 56 ms 15620 KB Output is correct
52 Correct 80 ms 16056 KB Output is correct
53 Correct 39 ms 15088 KB Output is correct
54 Correct 70 ms 15616 KB Output is correct
55 Correct 79 ms 16056 KB Output is correct
56 Correct 39 ms 15108 KB Output is correct
57 Correct 51 ms 15428 KB Output is correct
58 Correct 51 ms 15368 KB Output is correct
59 Correct 52 ms 15540 KB Output is correct
60 Correct 54 ms 15532 KB Output is correct
61 Correct 41 ms 15112 KB Output is correct
62 Correct 43 ms 15224 KB Output is correct
63 Correct 38 ms 15112 KB Output is correct
64 Correct 39 ms 15284 KB Output is correct
65 Correct 48 ms 15180 KB Output is correct
66 Correct 39 ms 15132 KB Output is correct
67 Correct 45 ms 15124 KB Output is correct
68 Correct 50 ms 15176 KB Output is correct
69 Correct 39 ms 15056 KB Output is correct
70 Correct 40 ms 15200 KB Output is correct
71 Correct 87 ms 16072 KB Output is correct
72 Correct 43 ms 15484 KB Output is correct
73 Correct 83 ms 16052 KB Output is correct
74 Correct 82 ms 16044 KB Output is correct
75 Correct 40 ms 15188 KB Output is correct
76 Correct 84 ms 16000 KB Output is correct
77 Correct 82 ms 16060 KB Output is correct
78 Correct 44 ms 15564 KB Output is correct
79 Correct 58 ms 15900 KB Output is correct
80 Correct 80 ms 16016 KB Output is correct
81 Correct 80 ms 15944 KB Output is correct
82 Correct 72 ms 16044 KB Output is correct
83 Correct 42 ms 15312 KB Output is correct
84 Correct 60 ms 15936 KB Output is correct
85 Correct 71 ms 16064 KB Output is correct
86 Correct 40 ms 15432 KB Output is correct
87 Correct 40 ms 15176 KB Output is correct
88 Correct 42 ms 15432 KB Output is correct
89 Correct 40 ms 15368 KB Output is correct
90 Correct 42 ms 15120 KB Output is correct
91 Correct 40 ms 15284 KB Output is correct
92 Correct 40 ms 15048 KB Output is correct
93 Correct 44 ms 15368 KB Output is correct
94 Correct 41 ms 15312 KB Output is correct
95 Correct 41 ms 15320 KB Output is correct
96 Correct 41 ms 15412 KB Output is correct
97 Correct 38 ms 15048 KB Output is correct