답안 #203004

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
203004 2020-02-19T01:42:48 Z Noam527 Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
1710 ms 50424 KB
#include <bits/stdc++.h>
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 4e18;
const int OO = 1;
const int OOO = 1;
using namespace std;

/*
Solution idea:
first compute through dp, the values F[mask] = sum(a[S]) for S contained in mask, G[mask] = sum(a[S]) for S contained in mask.
this can be done in O(L * 2^L) (through a nontrivial dp state).
if the string would contain only (0, ?), then the answer is F[mask], mask is binary representation of ?'s.
if the string would contain only (1, ?), same thing but G[mask].

so we use both arrays to cut the time complexity for the general case.
consider the character with minimum frequency in the query string, out of (0, 1, ?). The minimum frequency is between 0 and floor(L / 3).

if the minimum is ?: solve the query naively - try to replace each ? with either 0 or 1
if the minimum is 0: we get rid of 0's, and turn them to 1's and ?'s:
	for the query string x0y (x and y can be any part of the query string), its answer is x?y - x1y.
	once we end up with no 0's (on base cases of this recursive procedure), we can use array G[] as described above.
if the minimum is 1: we turn 1's to 0's and ?'s, in the same manner as above, then use F[].

in all cases we produce 2 possibilities for each character from the lowest frequency, and solve each possibility in O(1), so the complexity is:
time: O(2^L * L + Q * 2^floor(L / 3)).
memory: although the dp has O(2^L * L) states, we eventually only care about the last layer for all queries, so memory is O(2^L).

the problem was very interesting so I decided to write this little description.
the code may not be clear though (and I did not explain the dp); you are welcome to send questions to my codeforces account, Noam527.
*/

const int N = 1 << 20;

int L, L2, q, a[N];
int Z[2][N], O[2][N];

int MP[256] = {};

inline int zero(int mask) {
	return Z[L & 1][mask];
}
inline int one(int mask) {
	return O[L & 1][mask];
}

// cur = binary rep. by 1's
int work_question(const vector<int> &ind, int nxt, int cur) {
	if (nxt == ind.size()) return a[cur];
	return work_question(ind, nxt + 1, cur) + work_question(ind, nxt + 1, cur | (1 << ind[nxt]));
}

// gets to 0's.
// cur = binary rep. of ?'s
int work_0(const vector<int> &ind, int nxt, int cur) {
	if (nxt == ind.size()) return zero(cur);
	return work_0(ind, nxt + 1, cur | (1 << ind[nxt])) - work_0(ind, nxt + 1, cur);
}

// gets to 1's.
// cur = binary rep. of ?'s
int work_1(const vector<int> &ind, int nxt, int cur) {
	if (nxt == ind.size()) return one(L2 - 1 - cur);
	return work_1(ind, nxt + 1, cur | (1 << ind[nxt])) - work_1(ind, nxt + 1, cur);
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	MP['0'] = 0, MP['1'] = 1, MP['?'] = 2;

	cin >> L >> q;
	L2 = (1 << L);
	for (int i = 0; i < L2; i++) {
		char c;
		cin >> c;
		a[i] = c - '0';
		Z[0][i] = O[0][i] = a[i];
	}

	for (int i = 1; i <= L; i++) {
		for (int j = 0; j < L2; j++) {
			int x = j ^ (1 << (i - 1));
			if (j & (1 << (i - 1))) {
				Z[i & 1][j] = Z[i & 1 ^ 1][j] + Z[i & 1 ^ 1][x];
				O[i & 1][j] = O[i & 1 ^ 1][j];
			}
			else {
				Z[i & 1][j] = Z[i & 1 ^ 1][j];
				O[i & 1][j] = O[i & 1 ^ 1][j] + O[i & 1 ^ 1][x];
			}
		}
	}

	while (q--) {
		string s;
		cin >> s;
		int cnt[3] = {};
		vector<int> pos;
		for (int i = 0; i < L; i++)
			cnt[MP[s[i]]]++;
		int at = 0;
		if (cnt[1] < cnt[at]) at = 1;
		if (cnt[2] < cnt[at]) at = 2;

		pos.reserve(cnt[at]);
		for (int i = 0; i < L; i++)
			if (at == MP[s[i]])
				pos.push_back(L - 1 - i);

		if (at == 2) {
			int cur = 0;
			for (int i = 0; i < L; i++)
				if (s[i] == '1')
					cur |= 1 << (L - 1 - i);
			cout << work_question(pos, 0, cur) << '\n';
		}
		else {
			int cur = 0;
			for (int i = 0; i < L; i++)
				if (s[i] == '?')
					cur |= 1 << (L - 1 - i);
			if (at == 0)
				cout << work_1(pos, 0, cur) << '\n';
			else
				cout << work_0(pos, 0, cur) << '\n';
		}
	}
}

Compilation message

snake_escaping.cpp: In function 'int work_question(const std::vector<int>&, int, int)':
snake_escaping.cpp:51:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (nxt == ind.size()) return a[cur];
      ~~~~^~~~~~~~~~~~~
snake_escaping.cpp: In function 'int work_0(const std::vector<int>&, int, int)':
snake_escaping.cpp:58:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (nxt == ind.size()) return zero(cur);
      ~~~~^~~~~~~~~~~~~
snake_escaping.cpp: In function 'int work_1(const std::vector<int>&, int, int)':
snake_escaping.cpp:65:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (nxt == ind.size()) return one(L2 - 1 - cur);
      ~~~~^~~~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:86:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
     Z[i & 1][j] = Z[i & 1 ^ 1][j] + Z[i & 1 ^ 1][x];
                     ~~^~~
snake_escaping.cpp:86:41: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
     Z[i & 1][j] = Z[i & 1 ^ 1][j] + Z[i & 1 ^ 1][x];
                                       ~~^~~
snake_escaping.cpp:87:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
     O[i & 1][j] = O[i & 1 ^ 1][j];
                     ~~^~~
snake_escaping.cpp:90:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
     Z[i & 1][j] = Z[i & 1 ^ 1][j];
                     ~~^~~
snake_escaping.cpp:91:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
     O[i & 1][j] = O[i & 1 ^ 1][j] + O[i & 1 ^ 1][x];
                     ~~^~~
snake_escaping.cpp:91:41: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
     O[i & 1][j] = O[i & 1 ^ 1][j] + O[i & 1 ^ 1][x];
                                       ~~^~~
snake_escaping.cpp:102:15: warning: array subscript has type 'char' [-Wchar-subscripts]
    cnt[MP[s[i]]]++;
               ^
snake_escaping.cpp:109:21: warning: array subscript has type 'char' [-Wchar-subscripts]
    if (at == MP[s[i]])
                     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 304 ms 10936 KB Output is correct
12 Correct 313 ms 10616 KB Output is correct
13 Correct 423 ms 9976 KB Output is correct
14 Correct 413 ms 10104 KB Output is correct
15 Correct 375 ms 11000 KB Output is correct
16 Correct 416 ms 10232 KB Output is correct
17 Correct 413 ms 10104 KB Output is correct
18 Correct 258 ms 11896 KB Output is correct
19 Correct 284 ms 8952 KB Output is correct
20 Correct 314 ms 10744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 304 ms 10936 KB Output is correct
12 Correct 313 ms 10616 KB Output is correct
13 Correct 423 ms 9976 KB Output is correct
14 Correct 413 ms 10104 KB Output is correct
15 Correct 375 ms 11000 KB Output is correct
16 Correct 416 ms 10232 KB Output is correct
17 Correct 413 ms 10104 KB Output is correct
18 Correct 258 ms 11896 KB Output is correct
19 Correct 284 ms 8952 KB Output is correct
20 Correct 314 ms 10744 KB Output is correct
21 Correct 356 ms 11104 KB Output is correct
22 Correct 370 ms 11128 KB Output is correct
23 Correct 461 ms 10104 KB Output is correct
24 Correct 538 ms 9848 KB Output is correct
25 Correct 433 ms 11860 KB Output is correct
26 Correct 519 ms 10080 KB Output is correct
27 Correct 519 ms 10036 KB Output is correct
28 Correct 291 ms 12280 KB Output is correct
29 Correct 332 ms 8060 KB Output is correct
30 Correct 363 ms 10104 KB Output is correct
31 Correct 470 ms 9592 KB Output is correct
32 Correct 539 ms 9336 KB Output is correct
33 Correct 425 ms 8060 KB Output is correct
34 Correct 501 ms 7928 KB Output is correct
35 Correct 497 ms 8056 KB Output is correct
36 Correct 282 ms 6388 KB Output is correct
37 Correct 345 ms 8184 KB Output is correct
38 Correct 350 ms 6008 KB Output is correct
39 Correct 478 ms 7132 KB Output is correct
40 Correct 541 ms 6648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 101 ms 23236 KB Output is correct
12 Correct 111 ms 23288 KB Output is correct
13 Correct 113 ms 23032 KB Output is correct
14 Correct 127 ms 23160 KB Output is correct
15 Correct 98 ms 23288 KB Output is correct
16 Correct 149 ms 23160 KB Output is correct
17 Correct 132 ms 23160 KB Output is correct
18 Correct 88 ms 23288 KB Output is correct
19 Correct 97 ms 23052 KB Output is correct
20 Correct 103 ms 23288 KB Output is correct
21 Correct 117 ms 23160 KB Output is correct
22 Correct 126 ms 23032 KB Output is correct
23 Correct 104 ms 23032 KB Output is correct
24 Correct 142 ms 23216 KB Output is correct
25 Correct 142 ms 23160 KB Output is correct
26 Correct 83 ms 22960 KB Output is correct
27 Correct 97 ms 23132 KB Output is correct
28 Correct 93 ms 23032 KB Output is correct
29 Correct 109 ms 23032 KB Output is correct
30 Correct 123 ms 23032 KB Output is correct
31 Correct 98 ms 23032 KB Output is correct
32 Correct 144 ms 23160 KB Output is correct
33 Correct 145 ms 23160 KB Output is correct
34 Correct 94 ms 23164 KB Output is correct
35 Correct 128 ms 23160 KB Output is correct
36 Correct 122 ms 23160 KB Output is correct
37 Correct 125 ms 23160 KB Output is correct
38 Correct 128 ms 23316 KB Output is correct
39 Correct 123 ms 23160 KB Output is correct
40 Correct 126 ms 23160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 304 ms 10936 KB Output is correct
12 Correct 313 ms 10616 KB Output is correct
13 Correct 423 ms 9976 KB Output is correct
14 Correct 413 ms 10104 KB Output is correct
15 Correct 375 ms 11000 KB Output is correct
16 Correct 416 ms 10232 KB Output is correct
17 Correct 413 ms 10104 KB Output is correct
18 Correct 258 ms 11896 KB Output is correct
19 Correct 284 ms 8952 KB Output is correct
20 Correct 314 ms 10744 KB Output is correct
21 Correct 356 ms 11104 KB Output is correct
22 Correct 370 ms 11128 KB Output is correct
23 Correct 461 ms 10104 KB Output is correct
24 Correct 538 ms 9848 KB Output is correct
25 Correct 433 ms 11860 KB Output is correct
26 Correct 519 ms 10080 KB Output is correct
27 Correct 519 ms 10036 KB Output is correct
28 Correct 291 ms 12280 KB Output is correct
29 Correct 332 ms 8060 KB Output is correct
30 Correct 363 ms 10104 KB Output is correct
31 Correct 470 ms 9592 KB Output is correct
32 Correct 539 ms 9336 KB Output is correct
33 Correct 425 ms 8060 KB Output is correct
34 Correct 501 ms 7928 KB Output is correct
35 Correct 497 ms 8056 KB Output is correct
36 Correct 282 ms 6388 KB Output is correct
37 Correct 345 ms 8184 KB Output is correct
38 Correct 350 ms 6008 KB Output is correct
39 Correct 478 ms 7132 KB Output is correct
40 Correct 541 ms 6648 KB Output is correct
41 Correct 101 ms 23236 KB Output is correct
42 Correct 111 ms 23288 KB Output is correct
43 Correct 113 ms 23032 KB Output is correct
44 Correct 127 ms 23160 KB Output is correct
45 Correct 98 ms 23288 KB Output is correct
46 Correct 149 ms 23160 KB Output is correct
47 Correct 132 ms 23160 KB Output is correct
48 Correct 88 ms 23288 KB Output is correct
49 Correct 97 ms 23052 KB Output is correct
50 Correct 103 ms 23288 KB Output is correct
51 Correct 117 ms 23160 KB Output is correct
52 Correct 126 ms 23032 KB Output is correct
53 Correct 104 ms 23032 KB Output is correct
54 Correct 142 ms 23216 KB Output is correct
55 Correct 142 ms 23160 KB Output is correct
56 Correct 83 ms 22960 KB Output is correct
57 Correct 97 ms 23132 KB Output is correct
58 Correct 93 ms 23032 KB Output is correct
59 Correct 109 ms 23032 KB Output is correct
60 Correct 123 ms 23032 KB Output is correct
61 Correct 98 ms 23032 KB Output is correct
62 Correct 144 ms 23160 KB Output is correct
63 Correct 145 ms 23160 KB Output is correct
64 Correct 94 ms 23164 KB Output is correct
65 Correct 128 ms 23160 KB Output is correct
66 Correct 122 ms 23160 KB Output is correct
67 Correct 125 ms 23160 KB Output is correct
68 Correct 128 ms 23316 KB Output is correct
69 Correct 123 ms 23160 KB Output is correct
70 Correct 126 ms 23160 KB Output is correct
71 Correct 637 ms 26100 KB Output is correct
72 Correct 627 ms 26104 KB Output is correct
73 Correct 960 ms 24824 KB Output is correct
74 Correct 1254 ms 24952 KB Output is correct
75 Correct 723 ms 27060 KB Output is correct
76 Correct 1550 ms 27768 KB Output is correct
77 Correct 1406 ms 46712 KB Output is correct
78 Correct 487 ms 50424 KB Output is correct
79 Correct 619 ms 44444 KB Output is correct
80 Correct 661 ms 47592 KB Output is correct
81 Correct 1036 ms 47556 KB Output is correct
82 Correct 1271 ms 46456 KB Output is correct
83 Correct 734 ms 45840 KB Output is correct
84 Correct 1710 ms 46584 KB Output is correct
85 Correct 1466 ms 46756 KB Output is correct
86 Correct 484 ms 44376 KB Output is correct
87 Correct 637 ms 47480 KB Output is correct
88 Correct 655 ms 44624 KB Output is correct
89 Correct 1010 ms 46328 KB Output is correct
90 Correct 1151 ms 46460 KB Output is correct
91 Correct 732 ms 45816 KB Output is correct
92 Correct 1705 ms 46916 KB Output is correct
93 Correct 1470 ms 46748 KB Output is correct
94 Correct 482 ms 44536 KB Output is correct
95 Correct 1213 ms 46748 KB Output is correct
96 Correct 1206 ms 46584 KB Output is correct
97 Correct 1214 ms 46616 KB Output is correct
98 Execution timed out 390 ms 28972 KB Time limit exceeded (wall clock)
99 Halted 0 ms 0 KB -