Submission #881488

# Submission time Handle Problem Language Result Execution time Memory
881488 2023-12-01T09:49:07 Z mgl_diamond Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
609 ms 38996 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
 
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define file "input"

template<class T> bool minimize(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> bool maximize(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }

void setIO() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  if (fopen(file".inp", "r")) {
    freopen(file".inp", "r", stdin);
    freopen(file".out", "w", stdout);
  }
}

const int N = 20;

int n, q, full;
char a[1<<20];
int sum[1<<20], rsum[1<<20];

int main() {
  setIO();

  cin >> n >> q;
  full = (1<<n)-1;
  foru(i, 0, full) cin >> a[i];
  foru(i, 0, full) {
    a[i] -= '0';
    sum[i] = a[i];
    rsum[i] = a[i];
  }

  foru(bt, 0, n-1) {
    foru(i, 0, full) {
      if (i >> bt & 1)
        sum[i] += sum[i-(1<<bt)];
      else
        rsum[i] += rsum[i+(1<<bt)];
    }
  }

  int cap = n/3;
  while (q--) {
    string s;
    cin >> s;
    reverse(all(s));

    int cnt[3] = {}, ans = 0, ma = 0, mb = 0, mc = 0;
    foru(i, 0, n-1) {
      if (s[i] == '?') { cnt[0] += 1; ma += 1 << i; }
      else if (s[i] == '1') { cnt[1] += 1; mb += 1 << i; }
      else { cnt[2] += 1; mc += 1 << i; }
    }

    if (cnt[0] <= cap) {
      for(int i=ma; ; i=(i-1)&ma) {
        ans += a[mb|i];
        if (i == 0) break;
      }
    }
    else if (cnt[1] <= cap) {
      for(int i=mb; ; i=(i-1)&mb) {
        if ((cnt[1]-__builtin_popcount(i)) & 1) ans -= sum[ma|i];
        else ans += sum[ma|i];
        if (i == 0) break;
      }
    }
    else {
      for(int i=mc; ; i=(i-1)&mc) {
        if (__builtin_popcount(i) & 1) ans -= rsum[mb|i];
        else ans += rsum[mb|i];
        if (i == 0) break;
      }
    }
    cout << ans << "\n";
  }
}

Compilation message

snake_escaping.cpp: In function 'void setIO()':
snake_escaping.cpp:22:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     freopen(file".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:23:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |     freopen(file".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 197 ms 19220 KB Output is correct
12 Correct 175 ms 18932 KB Output is correct
13 Correct 209 ms 18428 KB Output is correct
14 Correct 198 ms 18444 KB Output is correct
15 Correct 204 ms 19540 KB Output is correct
16 Correct 213 ms 18308 KB Output is correct
17 Correct 212 ms 18204 KB Output is correct
18 Correct 163 ms 20144 KB Output is correct
19 Correct 161 ms 17236 KB Output is correct
20 Correct 183 ms 18808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 197 ms 19220 KB Output is correct
12 Correct 175 ms 18932 KB Output is correct
13 Correct 209 ms 18428 KB Output is correct
14 Correct 198 ms 18444 KB Output is correct
15 Correct 204 ms 19540 KB Output is correct
16 Correct 213 ms 18308 KB Output is correct
17 Correct 212 ms 18204 KB Output is correct
18 Correct 163 ms 20144 KB Output is correct
19 Correct 161 ms 17236 KB Output is correct
20 Correct 183 ms 18808 KB Output is correct
21 Correct 204 ms 22096 KB Output is correct
22 Correct 205 ms 22476 KB Output is correct
23 Correct 222 ms 21328 KB Output is correct
24 Correct 237 ms 21032 KB Output is correct
25 Correct 243 ms 23120 KB Output is correct
26 Correct 265 ms 21692 KB Output is correct
27 Correct 248 ms 21576 KB Output is correct
28 Correct 156 ms 24144 KB Output is correct
29 Correct 197 ms 20116 KB Output is correct
30 Correct 212 ms 22352 KB Output is correct
31 Correct 254 ms 22096 KB Output is correct
32 Correct 299 ms 22100 KB Output is correct
33 Correct 207 ms 20920 KB Output is correct
34 Correct 231 ms 21152 KB Output is correct
35 Correct 249 ms 21520 KB Output is correct
36 Correct 153 ms 20052 KB Output is correct
37 Correct 182 ms 22352 KB Output is correct
38 Correct 211 ms 20252 KB Output is correct
39 Correct 226 ms 21328 KB Output is correct
40 Correct 232 ms 21076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 44 ms 11760 KB Output is correct
12 Correct 40 ms 11876 KB Output is correct
13 Correct 42 ms 11856 KB Output is correct
14 Correct 53 ms 11860 KB Output is correct
15 Correct 47 ms 11856 KB Output is correct
16 Correct 70 ms 11832 KB Output is correct
17 Correct 47 ms 11860 KB Output is correct
18 Correct 36 ms 11956 KB Output is correct
19 Correct 40 ms 11604 KB Output is correct
20 Correct 41 ms 11856 KB Output is correct
21 Correct 62 ms 11860 KB Output is correct
22 Correct 60 ms 11756 KB Output is correct
23 Correct 41 ms 11712 KB Output is correct
24 Correct 53 ms 11812 KB Output is correct
25 Correct 61 ms 11868 KB Output is correct
26 Correct 38 ms 11664 KB Output is correct
27 Correct 41 ms 11860 KB Output is correct
28 Correct 39 ms 11600 KB Output is correct
29 Correct 43 ms 11860 KB Output is correct
30 Correct 46 ms 11872 KB Output is correct
31 Correct 39 ms 11708 KB Output is correct
32 Correct 50 ms 11860 KB Output is correct
33 Correct 49 ms 11832 KB Output is correct
34 Correct 35 ms 11604 KB Output is correct
35 Correct 49 ms 11860 KB Output is correct
36 Correct 49 ms 11712 KB Output is correct
37 Correct 46 ms 11704 KB Output is correct
38 Correct 48 ms 11860 KB Output is correct
39 Correct 45 ms 11920 KB Output is correct
40 Correct 48 ms 11712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 197 ms 19220 KB Output is correct
12 Correct 175 ms 18932 KB Output is correct
13 Correct 209 ms 18428 KB Output is correct
14 Correct 198 ms 18444 KB Output is correct
15 Correct 204 ms 19540 KB Output is correct
16 Correct 213 ms 18308 KB Output is correct
17 Correct 212 ms 18204 KB Output is correct
18 Correct 163 ms 20144 KB Output is correct
19 Correct 161 ms 17236 KB Output is correct
20 Correct 183 ms 18808 KB Output is correct
21 Correct 204 ms 22096 KB Output is correct
22 Correct 205 ms 22476 KB Output is correct
23 Correct 222 ms 21328 KB Output is correct
24 Correct 237 ms 21032 KB Output is correct
25 Correct 243 ms 23120 KB Output is correct
26 Correct 265 ms 21692 KB Output is correct
27 Correct 248 ms 21576 KB Output is correct
28 Correct 156 ms 24144 KB Output is correct
29 Correct 197 ms 20116 KB Output is correct
30 Correct 212 ms 22352 KB Output is correct
31 Correct 254 ms 22096 KB Output is correct
32 Correct 299 ms 22100 KB Output is correct
33 Correct 207 ms 20920 KB Output is correct
34 Correct 231 ms 21152 KB Output is correct
35 Correct 249 ms 21520 KB Output is correct
36 Correct 153 ms 20052 KB Output is correct
37 Correct 182 ms 22352 KB Output is correct
38 Correct 211 ms 20252 KB Output is correct
39 Correct 226 ms 21328 KB Output is correct
40 Correct 232 ms 21076 KB Output is correct
41 Correct 44 ms 11760 KB Output is correct
42 Correct 40 ms 11876 KB Output is correct
43 Correct 42 ms 11856 KB Output is correct
44 Correct 53 ms 11860 KB Output is correct
45 Correct 47 ms 11856 KB Output is correct
46 Correct 70 ms 11832 KB Output is correct
47 Correct 47 ms 11860 KB Output is correct
48 Correct 36 ms 11956 KB Output is correct
49 Correct 40 ms 11604 KB Output is correct
50 Correct 41 ms 11856 KB Output is correct
51 Correct 62 ms 11860 KB Output is correct
52 Correct 60 ms 11756 KB Output is correct
53 Correct 41 ms 11712 KB Output is correct
54 Correct 53 ms 11812 KB Output is correct
55 Correct 61 ms 11868 KB Output is correct
56 Correct 38 ms 11664 KB Output is correct
57 Correct 41 ms 11860 KB Output is correct
58 Correct 39 ms 11600 KB Output is correct
59 Correct 43 ms 11860 KB Output is correct
60 Correct 46 ms 11872 KB Output is correct
61 Correct 39 ms 11708 KB Output is correct
62 Correct 50 ms 11860 KB Output is correct
63 Correct 49 ms 11832 KB Output is correct
64 Correct 35 ms 11604 KB Output is correct
65 Correct 49 ms 11860 KB Output is correct
66 Correct 49 ms 11712 KB Output is correct
67 Correct 46 ms 11704 KB Output is correct
68 Correct 48 ms 11860 KB Output is correct
69 Correct 45 ms 11920 KB Output is correct
70 Correct 48 ms 11712 KB Output is correct
71 Correct 319 ms 36276 KB Output is correct
72 Correct 323 ms 36236 KB Output is correct
73 Correct 367 ms 34712 KB Output is correct
74 Correct 609 ms 34936 KB Output is correct
75 Correct 388 ms 37216 KB Output is correct
76 Correct 544 ms 35560 KB Output is correct
77 Correct 483 ms 35464 KB Output is correct
78 Correct 224 ms 38996 KB Output is correct
79 Correct 292 ms 33160 KB Output is correct
80 Correct 319 ms 36176 KB Output is correct
81 Correct 472 ms 36216 KB Output is correct
82 Correct 595 ms 35228 KB Output is correct
83 Correct 311 ms 34476 KB Output is correct
84 Correct 405 ms 35156 KB Output is correct
85 Correct 441 ms 35240 KB Output is correct
86 Correct 225 ms 32752 KB Output is correct
87 Correct 303 ms 36176 KB Output is correct
88 Correct 300 ms 33180 KB Output is correct
89 Correct 377 ms 34724 KB Output is correct
90 Correct 409 ms 35156 KB Output is correct
91 Correct 307 ms 34388 KB Output is correct
92 Correct 506 ms 35528 KB Output is correct
93 Correct 439 ms 35520 KB Output is correct
94 Correct 214 ms 32988 KB Output is correct
95 Correct 412 ms 35156 KB Output is correct
96 Correct 447 ms 35232 KB Output is correct
97 Correct 439 ms 35320 KB Output is correct
98 Correct 444 ms 35156 KB Output is correct
99 Correct 395 ms 35228 KB Output is correct
100 Correct 464 ms 35288 KB Output is correct