답안 #64590

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
64590 2018-08-05T01:01:45 Z keko37 Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1944 ms 51616 KB
#include<iostream>
#include<cstdio>

using namespace std;

const int MAXN = (1<<20) + 5;

int n, q, a, b, c, sol, mask, N, rev, count, i, j, tc;
char ss[MAXN];
char l[30], cc;
int val[MAXN], sum0[MAXN], sum1[MAXN], pc[MAXN];

int main () {
	scanf("%d%d", &n, &q);
	scanf("%s", ss);
	for (i=0; i<(1<<n); i++) {
		val[i] = ss[i] - '0';
		pc[i] = pc[i - (i&-i)]+1;
		sum0[i] = val[i];
	}
	for (int i=0; i<(1<<n); i++) {
		sum1[i] = val[i ^ ((1<<n) - 1)];
	}
	for (i=0; i<n; i++) {
		for (j=0; j<(1<<n); j++) {
			if (j & (1<<i)) sum0[j] += sum0[j ^ (1<<i)];
			if (j & (1<<i)) sum1[j] += sum1[j ^ (1<<i)];
		}
	}
	for (tc=0; tc<q; tc++) {
		a = b = c = 0;
		do {
			cc = getchar();
		} while (!(cc=='0' || cc=='1' || cc=='?'));
		for (i=0; i<n; i++) {
			if (i>0) cc = getchar();
			l[i] = cc;
			if (l[i] == '?') {
				a ^= (1<<(n-1-i));
			} else if (l[i] == '0') {
				b ^= (1<<(n-1-i));
			} else {
				c ^= (1<<(n-1-i));
			}
		}
		sol = 0;
		if (pc[a] <= pc[b] && pc[a] <= pc[c]) {
			for (mask = a; ; mask = (mask - 1) & a) {
				sol += val[mask | c];
				if (!mask) break;
			}
		} else if (pc[b] <= pc[a] && pc[b] <= pc[c]) {
			for (mask = b; ; mask = (mask-1) & b) {
				rev = mask | a;
				if (pc[rev] & 1) {
					sol -= sum1[rev];
				} else {
					sol += sum1[rev];
				}
				if (!mask) break;
			}
			if (sol < 0) sol = -sol;
		} else {
			for (mask = c; ; mask = (mask - 1) & c) {
				rev = mask | a;
				if (pc[rev] & 1) {
					sol -= sum0[rev];
				} else {
					sol += sum0[rev];
				}
				if (!mask) break;
			}
			if (sol < 0) sol = -sol;
		}
		printf("%d\n", sol);
	}
	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:14:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &q);
  ~~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp:15:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", ss);
  ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 3 ms 500 KB Output is correct
4 Correct 3 ms 516 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 4 ms 616 KB Output is correct
7 Correct 3 ms 616 KB Output is correct
8 Correct 3 ms 628 KB Output is correct
9 Correct 3 ms 680 KB Output is correct
10 Correct 3 ms 680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 3 ms 500 KB Output is correct
4 Correct 3 ms 516 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 4 ms 616 KB Output is correct
7 Correct 3 ms 616 KB Output is correct
8 Correct 3 ms 628 KB Output is correct
9 Correct 3 ms 680 KB Output is correct
10 Correct 3 ms 680 KB Output is correct
11 Correct 354 ms 6452 KB Output is correct
12 Correct 341 ms 6452 KB Output is correct
13 Correct 382 ms 6452 KB Output is correct
14 Correct 341 ms 6452 KB Output is correct
15 Correct 340 ms 6988 KB Output is correct
16 Correct 348 ms 6988 KB Output is correct
17 Correct 366 ms 6988 KB Output is correct
18 Correct 308 ms 8672 KB Output is correct
19 Correct 328 ms 8672 KB Output is correct
20 Correct 320 ms 8672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 3 ms 500 KB Output is correct
4 Correct 3 ms 516 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 4 ms 616 KB Output is correct
7 Correct 3 ms 616 KB Output is correct
8 Correct 3 ms 628 KB Output is correct
9 Correct 3 ms 680 KB Output is correct
10 Correct 3 ms 680 KB Output is correct
11 Correct 354 ms 6452 KB Output is correct
12 Correct 341 ms 6452 KB Output is correct
13 Correct 382 ms 6452 KB Output is correct
14 Correct 341 ms 6452 KB Output is correct
15 Correct 340 ms 6988 KB Output is correct
16 Correct 348 ms 6988 KB Output is correct
17 Correct 366 ms 6988 KB Output is correct
18 Correct 308 ms 8672 KB Output is correct
19 Correct 328 ms 8672 KB Output is correct
20 Correct 320 ms 8672 KB Output is correct
21 Correct 420 ms 8672 KB Output is correct
22 Correct 468 ms 8672 KB Output is correct
23 Correct 392 ms 8672 KB Output is correct
24 Correct 422 ms 8672 KB Output is correct
25 Correct 491 ms 8976 KB Output is correct
26 Correct 431 ms 8976 KB Output is correct
27 Correct 423 ms 8976 KB Output is correct
28 Correct 287 ms 10268 KB Output is correct
29 Correct 434 ms 10268 KB Output is correct
30 Correct 429 ms 10268 KB Output is correct
31 Correct 408 ms 10268 KB Output is correct
32 Correct 519 ms 10268 KB Output is correct
33 Correct 397 ms 10268 KB Output is correct
34 Correct 538 ms 10268 KB Output is correct
35 Correct 472 ms 10268 KB Output is correct
36 Correct 255 ms 10268 KB Output is correct
37 Correct 406 ms 10268 KB Output is correct
38 Correct 428 ms 10268 KB Output is correct
39 Correct 472 ms 10268 KB Output is correct
40 Correct 406 ms 10268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 3 ms 500 KB Output is correct
4 Correct 3 ms 516 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 4 ms 616 KB Output is correct
7 Correct 3 ms 616 KB Output is correct
8 Correct 3 ms 628 KB Output is correct
9 Correct 3 ms 680 KB Output is correct
10 Correct 3 ms 680 KB Output is correct
11 Correct 81 ms 22908 KB Output is correct
12 Correct 84 ms 22908 KB Output is correct
13 Correct 111 ms 22908 KB Output is correct
14 Correct 157 ms 22908 KB Output is correct
15 Correct 98 ms 23044 KB Output is correct
16 Correct 129 ms 23044 KB Output is correct
17 Correct 118 ms 23044 KB Output is correct
18 Correct 86 ms 23072 KB Output is correct
19 Correct 101 ms 23072 KB Output is correct
20 Correct 89 ms 23072 KB Output is correct
21 Correct 115 ms 23072 KB Output is correct
22 Correct 149 ms 23072 KB Output is correct
23 Correct 101 ms 23072 KB Output is correct
24 Correct 138 ms 23072 KB Output is correct
25 Correct 120 ms 23072 KB Output is correct
26 Correct 75 ms 23072 KB Output is correct
27 Correct 129 ms 23072 KB Output is correct
28 Correct 101 ms 23072 KB Output is correct
29 Correct 139 ms 23072 KB Output is correct
30 Correct 125 ms 23072 KB Output is correct
31 Correct 99 ms 23072 KB Output is correct
32 Correct 144 ms 23072 KB Output is correct
33 Correct 164 ms 23072 KB Output is correct
34 Correct 89 ms 23072 KB Output is correct
35 Correct 118 ms 23072 KB Output is correct
36 Correct 112 ms 23072 KB Output is correct
37 Correct 110 ms 23072 KB Output is correct
38 Correct 125 ms 23072 KB Output is correct
39 Correct 103 ms 23072 KB Output is correct
40 Correct 120 ms 23072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 3 ms 500 KB Output is correct
4 Correct 3 ms 516 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 4 ms 616 KB Output is correct
7 Correct 3 ms 616 KB Output is correct
8 Correct 3 ms 628 KB Output is correct
9 Correct 3 ms 680 KB Output is correct
10 Correct 3 ms 680 KB Output is correct
11 Correct 354 ms 6452 KB Output is correct
12 Correct 341 ms 6452 KB Output is correct
13 Correct 382 ms 6452 KB Output is correct
14 Correct 341 ms 6452 KB Output is correct
15 Correct 340 ms 6988 KB Output is correct
16 Correct 348 ms 6988 KB Output is correct
17 Correct 366 ms 6988 KB Output is correct
18 Correct 308 ms 8672 KB Output is correct
19 Correct 328 ms 8672 KB Output is correct
20 Correct 320 ms 8672 KB Output is correct
21 Correct 420 ms 8672 KB Output is correct
22 Correct 468 ms 8672 KB Output is correct
23 Correct 392 ms 8672 KB Output is correct
24 Correct 422 ms 8672 KB Output is correct
25 Correct 491 ms 8976 KB Output is correct
26 Correct 431 ms 8976 KB Output is correct
27 Correct 423 ms 8976 KB Output is correct
28 Correct 287 ms 10268 KB Output is correct
29 Correct 434 ms 10268 KB Output is correct
30 Correct 429 ms 10268 KB Output is correct
31 Correct 408 ms 10268 KB Output is correct
32 Correct 519 ms 10268 KB Output is correct
33 Correct 397 ms 10268 KB Output is correct
34 Correct 538 ms 10268 KB Output is correct
35 Correct 472 ms 10268 KB Output is correct
36 Correct 255 ms 10268 KB Output is correct
37 Correct 406 ms 10268 KB Output is correct
38 Correct 428 ms 10268 KB Output is correct
39 Correct 472 ms 10268 KB Output is correct
40 Correct 406 ms 10268 KB Output is correct
41 Correct 81 ms 22908 KB Output is correct
42 Correct 84 ms 22908 KB Output is correct
43 Correct 111 ms 22908 KB Output is correct
44 Correct 157 ms 22908 KB Output is correct
45 Correct 98 ms 23044 KB Output is correct
46 Correct 129 ms 23044 KB Output is correct
47 Correct 118 ms 23044 KB Output is correct
48 Correct 86 ms 23072 KB Output is correct
49 Correct 101 ms 23072 KB Output is correct
50 Correct 89 ms 23072 KB Output is correct
51 Correct 115 ms 23072 KB Output is correct
52 Correct 149 ms 23072 KB Output is correct
53 Correct 101 ms 23072 KB Output is correct
54 Correct 138 ms 23072 KB Output is correct
55 Correct 120 ms 23072 KB Output is correct
56 Correct 75 ms 23072 KB Output is correct
57 Correct 129 ms 23072 KB Output is correct
58 Correct 101 ms 23072 KB Output is correct
59 Correct 139 ms 23072 KB Output is correct
60 Correct 125 ms 23072 KB Output is correct
61 Correct 99 ms 23072 KB Output is correct
62 Correct 144 ms 23072 KB Output is correct
63 Correct 164 ms 23072 KB Output is correct
64 Correct 89 ms 23072 KB Output is correct
65 Correct 118 ms 23072 KB Output is correct
66 Correct 112 ms 23072 KB Output is correct
67 Correct 110 ms 23072 KB Output is correct
68 Correct 125 ms 23072 KB Output is correct
69 Correct 103 ms 23072 KB Output is correct
70 Correct 120 ms 23072 KB Output is correct
71 Correct 742 ms 27496 KB Output is correct
72 Correct 788 ms 27752 KB Output is correct
73 Correct 1108 ms 27752 KB Output is correct
74 Correct 1717 ms 27752 KB Output is correct
75 Correct 728 ms 29944 KB Output is correct
76 Correct 1516 ms 29944 KB Output is correct
77 Correct 1408 ms 29944 KB Output is correct
78 Correct 473 ms 32288 KB Output is correct
79 Correct 749 ms 32288 KB Output is correct
80 Correct 783 ms 32288 KB Output is correct
81 Correct 1113 ms 32288 KB Output is correct
82 Correct 1944 ms 32288 KB Output is correct
83 Correct 835 ms 32288 KB Output is correct
84 Correct 1490 ms 32288 KB Output is correct
85 Correct 1219 ms 50776 KB Output is correct
86 Correct 434 ms 50776 KB Output is correct
87 Correct 723 ms 51616 KB Output is correct
88 Correct 715 ms 51616 KB Output is correct
89 Correct 1004 ms 51616 KB Output is correct
90 Correct 1332 ms 51616 KB Output is correct
91 Correct 873 ms 51616 KB Output is correct
92 Correct 1700 ms 51616 KB Output is correct
93 Correct 1193 ms 51616 KB Output is correct
94 Correct 526 ms 51616 KB Output is correct
95 Correct 1223 ms 51616 KB Output is correct
96 Correct 1176 ms 51616 KB Output is correct
97 Correct 1228 ms 51616 KB Output is correct
98 Correct 1276 ms 51616 KB Output is correct
99 Correct 1339 ms 51616 KB Output is correct
100 Correct 1137 ms 51616 KB Output is correct