답안 #1041945

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1041945 2024-08-02T10:38:53 Z Zero_OP Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
427 ms 43508 KB
#include<bits/stdc++.h>

using namespace std;

#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template<typename T>
    bool minimize(T& a, const T& b){
        if(a > b){
            return a = b, true;
        }  return false;
    }

template<typename T>
    bool maximize(T& a, const T& b){
        if(a < b){
            return a = b, true;
        } return false;
    }

int f(int n, int k){
    if(n == 1) return 1;
    if(k <= (n + 1) / 2){
        if(2 * k > n) return (2 * k) % n;
        else return 2 * k;
    }
    int rec = f(n / 2, k - (n + 1) / 2);
    if(n & 1) return 2 * rec + 1;
    else return 2 * rec - 1;
}


void testcase(){
    int L, q; string s;
    cin >> L >> q >> s;

    int n = 1 << L;
    vector<int> a(n), sos_submask(n), sos_supermask(n);
    for(int i = 0; i < n; ++i){
        a[i] = s[i] - '0';
        sos_submask[i] = a[i];
        sos_supermask[i] = a[i];
    }

    for(int i = 0; i < L; ++i){
        for(int mask = 0; mask < n; ++mask){
            if(mask >> i & 1) sos_submask[mask] += sos_submask[mask ^ (1 << i)];
            else sos_supermask[mask] += sos_supermask[mask ^ (1 << i)];
        }
    }

    int cnt[3], mask[3], BIG = (1 << L) - 1;
    while(q--){
        string t;
        cin >> t;
        reverse(t.begin(), t.end());

        memset(cnt, 0, sizeof(cnt));
        memset(mask, 0, sizeof(mask));
        for(int i = 0; i < L; ++i){
            int type = (t[i] == '0' ? 0 : (t[i] == '1' ? 1 : 2));
            ++cnt[type];
            mask[type] |= (1 << i);
        }

        int min_cnt = *min_element(cnt, cnt + 3);
        if(cnt[0] == min_cnt){
            int ans = 0;
            for(int s = mask[0];; s = (s - 1) & mask[0]){
                if(__builtin_popcount(s) & 1) ans -= sos_supermask[s | mask[1]];
                else ans += sos_supermask[s | mask[1]];
                if(!s) break;
            }
            cout << ans << '\n';
        } else if(cnt[1] == min_cnt){
            int ans = 0;
            for(int s = mask[1];; s = (s - 1) & mask[1]){
                if(__builtin_popcount(s) & 1) ans -= sos_submask[(mask[1] ^ s) | mask[2]];
                else ans += sos_submask[(mask[1] ^ s) | mask[2]];
                if(!s) break;
            }
            cout << ans << '\n';
        } else{
            int ans = 0;
            for(int s = mask[2];; s = (s - 1) & mask[2]){
                ans += a[mask[1] | s];
                if(!s) break;
            }
            cout << ans << '\n';
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    file("task");

    int T = 1;
    //cin >> T;
    while(T--){
        testcase();
    }

    return 0;
}

Compilation message

snake_escaping.cpp: In function 'void testcase()':
snake_escaping.cpp:56:26: warning: unused variable 'BIG' [-Wunused-variable]
   56 |     int cnt[3], mask[3], BIG = (1 << L) - 1;
      |                          ^~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:9:55: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:102:5: note: in expansion of macro 'file'
  102 |     file("task");
      |     ^~~~
snake_escaping.cpp:9:88: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:102:5: note: in expansion of macro 'file'
  102 |     file("task");
      |     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 464 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 464 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 97 ms 15028 KB Output is correct
12 Correct 131 ms 14672 KB Output is correct
13 Correct 135 ms 14080 KB Output is correct
14 Correct 154 ms 14160 KB Output is correct
15 Correct 126 ms 15188 KB Output is correct
16 Correct 135 ms 14272 KB Output is correct
17 Correct 141 ms 14148 KB Output is correct
18 Correct 99 ms 16208 KB Output is correct
19 Correct 114 ms 13388 KB Output is correct
20 Correct 99 ms 14676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 464 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 97 ms 15028 KB Output is correct
12 Correct 131 ms 14672 KB Output is correct
13 Correct 135 ms 14080 KB Output is correct
14 Correct 154 ms 14160 KB Output is correct
15 Correct 126 ms 15188 KB Output is correct
16 Correct 135 ms 14272 KB Output is correct
17 Correct 141 ms 14148 KB Output is correct
18 Correct 99 ms 16208 KB Output is correct
19 Correct 114 ms 13388 KB Output is correct
20 Correct 99 ms 14676 KB Output is correct
21 Correct 105 ms 18224 KB Output is correct
22 Correct 163 ms 18468 KB Output is correct
23 Correct 174 ms 17324 KB Output is correct
24 Correct 168 ms 17084 KB Output is correct
25 Correct 131 ms 19092 KB Output is correct
26 Correct 165 ms 17540 KB Output is correct
27 Correct 165 ms 17492 KB Output is correct
28 Correct 110 ms 20108 KB Output is correct
29 Correct 127 ms 16284 KB Output is correct
30 Correct 113 ms 18260 KB Output is correct
31 Correct 156 ms 18132 KB Output is correct
32 Correct 174 ms 18260 KB Output is correct
33 Correct 129 ms 17052 KB Output is correct
34 Correct 164 ms 17232 KB Output is correct
35 Correct 169 ms 17492 KB Output is correct
36 Correct 111 ms 16204 KB Output is correct
37 Correct 142 ms 18256 KB Output is correct
38 Correct 156 ms 16212 KB Output is correct
39 Correct 139 ms 17264 KB Output is correct
40 Correct 166 ms 17236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 464 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 27 ms 16128 KB Output is correct
12 Correct 31 ms 16116 KB Output is correct
13 Correct 31 ms 15932 KB Output is correct
14 Correct 40 ms 16172 KB Output is correct
15 Correct 29 ms 16220 KB Output is correct
16 Correct 36 ms 15976 KB Output is correct
17 Correct 35 ms 15972 KB Output is correct
18 Correct 26 ms 16384 KB Output is correct
19 Correct 30 ms 16164 KB Output is correct
20 Correct 27 ms 16116 KB Output is correct
21 Correct 32 ms 16120 KB Output is correct
22 Correct 39 ms 16112 KB Output is correct
23 Correct 29 ms 15944 KB Output is correct
24 Correct 35 ms 16112 KB Output is correct
25 Correct 33 ms 16116 KB Output is correct
26 Correct 26 ms 15976 KB Output is correct
27 Correct 29 ms 16116 KB Output is correct
28 Correct 33 ms 15880 KB Output is correct
29 Correct 31 ms 16112 KB Output is correct
30 Correct 38 ms 16112 KB Output is correct
31 Correct 29 ms 16116 KB Output is correct
32 Correct 41 ms 16116 KB Output is correct
33 Correct 33 ms 15972 KB Output is correct
34 Correct 26 ms 16064 KB Output is correct
35 Correct 31 ms 15988 KB Output is correct
36 Correct 31 ms 15980 KB Output is correct
37 Correct 32 ms 16124 KB Output is correct
38 Correct 32 ms 15988 KB Output is correct
39 Correct 33 ms 16128 KB Output is correct
40 Correct 33 ms 16124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 464 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 97 ms 15028 KB Output is correct
12 Correct 131 ms 14672 KB Output is correct
13 Correct 135 ms 14080 KB Output is correct
14 Correct 154 ms 14160 KB Output is correct
15 Correct 126 ms 15188 KB Output is correct
16 Correct 135 ms 14272 KB Output is correct
17 Correct 141 ms 14148 KB Output is correct
18 Correct 99 ms 16208 KB Output is correct
19 Correct 114 ms 13388 KB Output is correct
20 Correct 99 ms 14676 KB Output is correct
21 Correct 105 ms 18224 KB Output is correct
22 Correct 163 ms 18468 KB Output is correct
23 Correct 174 ms 17324 KB Output is correct
24 Correct 168 ms 17084 KB Output is correct
25 Correct 131 ms 19092 KB Output is correct
26 Correct 165 ms 17540 KB Output is correct
27 Correct 165 ms 17492 KB Output is correct
28 Correct 110 ms 20108 KB Output is correct
29 Correct 127 ms 16284 KB Output is correct
30 Correct 113 ms 18260 KB Output is correct
31 Correct 156 ms 18132 KB Output is correct
32 Correct 174 ms 18260 KB Output is correct
33 Correct 129 ms 17052 KB Output is correct
34 Correct 164 ms 17232 KB Output is correct
35 Correct 169 ms 17492 KB Output is correct
36 Correct 111 ms 16204 KB Output is correct
37 Correct 142 ms 18256 KB Output is correct
38 Correct 156 ms 16212 KB Output is correct
39 Correct 139 ms 17264 KB Output is correct
40 Correct 166 ms 17236 KB Output is correct
41 Correct 27 ms 16128 KB Output is correct
42 Correct 31 ms 16116 KB Output is correct
43 Correct 31 ms 15932 KB Output is correct
44 Correct 40 ms 16172 KB Output is correct
45 Correct 29 ms 16220 KB Output is correct
46 Correct 36 ms 15976 KB Output is correct
47 Correct 35 ms 15972 KB Output is correct
48 Correct 26 ms 16384 KB Output is correct
49 Correct 30 ms 16164 KB Output is correct
50 Correct 27 ms 16116 KB Output is correct
51 Correct 32 ms 16120 KB Output is correct
52 Correct 39 ms 16112 KB Output is correct
53 Correct 29 ms 15944 KB Output is correct
54 Correct 35 ms 16112 KB Output is correct
55 Correct 33 ms 16116 KB Output is correct
56 Correct 26 ms 15976 KB Output is correct
57 Correct 29 ms 16116 KB Output is correct
58 Correct 33 ms 15880 KB Output is correct
59 Correct 31 ms 16112 KB Output is correct
60 Correct 38 ms 16112 KB Output is correct
61 Correct 29 ms 16116 KB Output is correct
62 Correct 41 ms 16116 KB Output is correct
63 Correct 33 ms 15972 KB Output is correct
64 Correct 26 ms 16064 KB Output is correct
65 Correct 31 ms 15988 KB Output is correct
66 Correct 31 ms 15980 KB Output is correct
67 Correct 32 ms 16124 KB Output is correct
68 Correct 32 ms 15988 KB Output is correct
69 Correct 33 ms 16128 KB Output is correct
70 Correct 33 ms 16124 KB Output is correct
71 Correct 163 ms 40436 KB Output is correct
72 Correct 234 ms 40436 KB Output is correct
73 Correct 264 ms 39152 KB Output is correct
74 Correct 427 ms 40476 KB Output is correct
75 Correct 207 ms 41464 KB Output is correct
76 Correct 356 ms 39776 KB Output is correct
77 Correct 307 ms 39668 KB Output is correct
78 Correct 158 ms 43508 KB Output is correct
79 Correct 241 ms 37316 KB Output is correct
80 Correct 169 ms 40436 KB Output is correct
81 Correct 301 ms 40108 KB Output is correct
82 Correct 417 ms 39408 KB Output is correct
83 Correct 209 ms 38500 KB Output is correct
84 Correct 361 ms 39412 KB Output is correct
85 Correct 354 ms 39524 KB Output is correct
86 Correct 152 ms 37364 KB Output is correct
87 Correct 229 ms 40292 KB Output is correct
88 Correct 231 ms 37364 KB Output is correct
89 Correct 249 ms 39156 KB Output is correct
90 Correct 308 ms 39308 KB Output is correct
91 Correct 246 ms 38656 KB Output is correct
92 Correct 415 ms 39724 KB Output is correct
93 Correct 380 ms 39664 KB Output is correct
94 Correct 165 ms 37520 KB Output is correct
95 Correct 304 ms 39412 KB Output is correct
96 Correct 286 ms 39424 KB Output is correct
97 Correct 289 ms 39432 KB Output is correct
98 Correct 298 ms 39472 KB Output is correct
99 Correct 301 ms 39412 KB Output is correct
100 Correct 272 ms 39408 KB Output is correct