답안 #64573

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
64573 2018-08-04T23:52:49 Z keko37 Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
818 ms 66560 KB
#include<iostream>

using namespace std;

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

int n, q, a, b, c, sol, mask;
char l[30], cc;
int val[MAXN], sum0[MAXN], sum1[MAXN], pc[MAXN];

void precompute () {
	for (int i=0; i<(1<<n); i++) {
		pc[i] = __builtin_popcount(i);
		sum0[i] = val[i];
		sum1[i] = val[i ^ ((1<<n) - 1)];
	}
	for (int i=0; i<n; i++) {
		for (int 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)];
		}
	}
}
  #define pcc(x) putchar(x);
    inline void writeInt (int n)
    {
        int N = n, rev, count = 0;
        rev = N;
        if (N == 0) { pcc('0'); pcc('\n'); return ;}
        while ((rev % 10) == 0) { count++; rev /= 10;} //obtain the count of the number of 0s
        rev = 0;
        while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}  //store reverse of N in rev
        while (rev != 0) { pcc(rev % 10 + '0'); rev /= 10;}
        while (count--) pcc('0');
    }
    
int main () {
	cin >> n >> q;
	do {
		cc = getchar();
	} while (!((cc>='0' && cc<='9') || cc=='?'));
	for (int i=0; i<(1<<n); i++) {
		if (i>0) {
			cc = getchar();
		}
		val[i] = cc - '0';
	}
	precompute();
	for (int tc=0; tc<q; tc++) {
		a = b = c = 0;
		do {
			cc = getchar();
		} while (!((cc>='0' && cc<='9') || cc=='?'));
		for (int 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, mask = 0;
		if (pc[a] <= n/3) {
			do {
				sol += val[mask | c];
			} while (mask = (mask-a) & a);
		} else if (pc[b] <= n/3) {
			do {
				if (pc[mask | a] & 1) {
					sol -= sum1[mask | a];
				} else {
					sol += sum1[mask | a];
				}
			} while (mask = (mask-b) & b);
			if (sol < 0) sol = -sol;
		} else {
			do {
				if (pc[mask | a] & 1) {
					sol -= sum0[mask | a];
				} else {
					sol += sum0[mask | a];
				}
			} while (mask = (mask-c) & c);
			if (sol < 0) sol = -sol;
		}
		writeInt(sol);
		putchar('\n');
	}
	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:69:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    } while (mask = (mask-a) & a);
             ~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp:77:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    } while (mask = (mask-b) & b);
             ~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp:86:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    } while (mask = (mask-c) & c);
             ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 3 ms 496 KB Output is correct
3 Correct 3 ms 496 KB Output is correct
4 Correct 3 ms 536 KB Output is correct
5 Correct 3 ms 544 KB Output is correct
6 Correct 3 ms 556 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 3 ms 496 KB Output is correct
3 Correct 3 ms 496 KB Output is correct
4 Correct 3 ms 536 KB Output is correct
5 Correct 3 ms 544 KB Output is correct
6 Correct 3 ms 556 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 270 ms 6648 KB Output is correct
12 Correct 288 ms 6648 KB Output is correct
13 Correct 274 ms 6648 KB Output is correct
14 Correct 278 ms 6648 KB Output is correct
15 Correct 276 ms 7428 KB Output is correct
16 Correct 294 ms 7428 KB Output is correct
17 Correct 297 ms 7428 KB Output is correct
18 Correct 223 ms 9172 KB Output is correct
19 Correct 242 ms 9172 KB Output is correct
20 Correct 250 ms 9172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 3 ms 496 KB Output is correct
3 Correct 3 ms 496 KB Output is correct
4 Correct 3 ms 536 KB Output is correct
5 Correct 3 ms 544 KB Output is correct
6 Correct 3 ms 556 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 270 ms 6648 KB Output is correct
12 Correct 288 ms 6648 KB Output is correct
13 Correct 274 ms 6648 KB Output is correct
14 Correct 278 ms 6648 KB Output is correct
15 Correct 276 ms 7428 KB Output is correct
16 Correct 294 ms 7428 KB Output is correct
17 Correct 297 ms 7428 KB Output is correct
18 Correct 223 ms 9172 KB Output is correct
19 Correct 242 ms 9172 KB Output is correct
20 Correct 250 ms 9172 KB Output is correct
21 Correct 333 ms 9172 KB Output is correct
22 Correct 378 ms 9172 KB Output is correct
23 Correct 349 ms 9172 KB Output is correct
24 Correct 358 ms 9172 KB Output is correct
25 Correct 359 ms 9440 KB Output is correct
26 Correct 362 ms 9440 KB Output is correct
27 Correct 338 ms 9440 KB Output is correct
28 Correct 287 ms 10448 KB Output is correct
29 Correct 316 ms 10448 KB Output is correct
30 Correct 348 ms 10448 KB Output is correct
31 Correct 426 ms 10448 KB Output is correct
32 Correct 383 ms 10448 KB Output is correct
33 Correct 317 ms 10448 KB Output is correct
34 Correct 355 ms 10448 KB Output is correct
35 Correct 344 ms 22628 KB Output is correct
36 Correct 259 ms 27236 KB Output is correct
37 Correct 332 ms 29024 KB Output is correct
38 Correct 309 ms 29024 KB Output is correct
39 Correct 346 ms 29024 KB Output is correct
40 Correct 467 ms 41660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 3 ms 496 KB Output is correct
3 Correct 3 ms 496 KB Output is correct
4 Correct 3 ms 536 KB Output is correct
5 Correct 3 ms 544 KB Output is correct
6 Correct 3 ms 556 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 109 ms 41660 KB Output is correct
12 Correct 138 ms 41660 KB Output is correct
13 Correct 92 ms 41660 KB Output is correct
14 Correct 176 ms 41660 KB Output is correct
15 Correct 147 ms 41660 KB Output is correct
16 Correct 147 ms 41660 KB Output is correct
17 Correct 134 ms 41660 KB Output is correct
18 Correct 81 ms 41660 KB Output is correct
19 Correct 85 ms 41660 KB Output is correct
20 Correct 108 ms 41660 KB Output is correct
21 Correct 110 ms 41660 KB Output is correct
22 Correct 159 ms 41660 KB Output is correct
23 Correct 107 ms 41660 KB Output is correct
24 Correct 107 ms 41660 KB Output is correct
25 Correct 185 ms 41660 KB Output is correct
26 Correct 74 ms 41660 KB Output is correct
27 Correct 127 ms 41660 KB Output is correct
28 Correct 91 ms 41660 KB Output is correct
29 Correct 112 ms 41660 KB Output is correct
30 Correct 127 ms 41660 KB Output is correct
31 Correct 118 ms 41660 KB Output is correct
32 Correct 135 ms 41660 KB Output is correct
33 Correct 145 ms 41660 KB Output is correct
34 Correct 76 ms 41660 KB Output is correct
35 Correct 123 ms 41660 KB Output is correct
36 Correct 110 ms 41660 KB Output is correct
37 Correct 122 ms 41660 KB Output is correct
38 Correct 108 ms 41660 KB Output is correct
39 Correct 109 ms 41660 KB Output is correct
40 Correct 110 ms 41660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 3 ms 496 KB Output is correct
3 Correct 3 ms 496 KB Output is correct
4 Correct 3 ms 536 KB Output is correct
5 Correct 3 ms 544 KB Output is correct
6 Correct 3 ms 556 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 270 ms 6648 KB Output is correct
12 Correct 288 ms 6648 KB Output is correct
13 Correct 274 ms 6648 KB Output is correct
14 Correct 278 ms 6648 KB Output is correct
15 Correct 276 ms 7428 KB Output is correct
16 Correct 294 ms 7428 KB Output is correct
17 Correct 297 ms 7428 KB Output is correct
18 Correct 223 ms 9172 KB Output is correct
19 Correct 242 ms 9172 KB Output is correct
20 Correct 250 ms 9172 KB Output is correct
21 Correct 333 ms 9172 KB Output is correct
22 Correct 378 ms 9172 KB Output is correct
23 Correct 349 ms 9172 KB Output is correct
24 Correct 358 ms 9172 KB Output is correct
25 Correct 359 ms 9440 KB Output is correct
26 Correct 362 ms 9440 KB Output is correct
27 Correct 338 ms 9440 KB Output is correct
28 Correct 287 ms 10448 KB Output is correct
29 Correct 316 ms 10448 KB Output is correct
30 Correct 348 ms 10448 KB Output is correct
31 Correct 426 ms 10448 KB Output is correct
32 Correct 383 ms 10448 KB Output is correct
33 Correct 317 ms 10448 KB Output is correct
34 Correct 355 ms 10448 KB Output is correct
35 Correct 344 ms 22628 KB Output is correct
36 Correct 259 ms 27236 KB Output is correct
37 Correct 332 ms 29024 KB Output is correct
38 Correct 309 ms 29024 KB Output is correct
39 Correct 346 ms 29024 KB Output is correct
40 Correct 467 ms 41660 KB Output is correct
41 Correct 109 ms 41660 KB Output is correct
42 Correct 138 ms 41660 KB Output is correct
43 Correct 92 ms 41660 KB Output is correct
44 Correct 176 ms 41660 KB Output is correct
45 Correct 147 ms 41660 KB Output is correct
46 Correct 147 ms 41660 KB Output is correct
47 Correct 134 ms 41660 KB Output is correct
48 Correct 81 ms 41660 KB Output is correct
49 Correct 85 ms 41660 KB Output is correct
50 Correct 108 ms 41660 KB Output is correct
51 Correct 110 ms 41660 KB Output is correct
52 Correct 159 ms 41660 KB Output is correct
53 Correct 107 ms 41660 KB Output is correct
54 Correct 107 ms 41660 KB Output is correct
55 Correct 185 ms 41660 KB Output is correct
56 Correct 74 ms 41660 KB Output is correct
57 Correct 127 ms 41660 KB Output is correct
58 Correct 91 ms 41660 KB Output is correct
59 Correct 112 ms 41660 KB Output is correct
60 Correct 127 ms 41660 KB Output is correct
61 Correct 118 ms 41660 KB Output is correct
62 Correct 135 ms 41660 KB Output is correct
63 Correct 145 ms 41660 KB Output is correct
64 Correct 76 ms 41660 KB Output is correct
65 Correct 123 ms 41660 KB Output is correct
66 Correct 110 ms 41660 KB Output is correct
67 Correct 122 ms 41660 KB Output is correct
68 Correct 108 ms 41660 KB Output is correct
69 Correct 109 ms 41660 KB Output is correct
70 Correct 110 ms 41660 KB Output is correct
71 Correct 701 ms 45972 KB Output is correct
72 Runtime error 818 ms 66560 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
73 Halted 0 ms 0 KB -