Submission #100698

# Submission time Handle Problem Language Result Execution time Memory
100698 2019-03-13T12:51:44 Z JPN20 Security Gate (JOI18_security_gate) C++17
30 / 100
852 ms 540 KB
#include <bits/stdc++.h>
using namespace std;

int dep[109], N;

bool check(string V) {
	int depth = 0;
	for (int i = 0; i < V.size(); i++) {
		if (V[i] == '(') depth++;
		if (V[i] == ')') depth--;
		if (depth < 0) return false;
	}
	if (depth != 0) return false;
	return true;
}

bool isvalid(string V) {
	// 反転位置は実は一通りに定まる
	dep[0] = 0;
	for (int i = 0; i < V.size(); i++) {
		if (V[i] == '(') dep[i + 1] = dep[i] + 1;
		if (V[i] == ')') dep[i + 1] = dep[i] - 1;
	}
	
	int cl = 0, cr = N;
	for (int i = 0; i <= V.size(); i++) { if (dep[i] < 0) break; cl = i; }
	for (int i = V.size(); i >= 0; i--) { if (dep[i] < dep[V.size()]) break; cr = i; }
	
	if (cl == N && cr == 0) {
		// 両方全部の場合
		return true;
	}
	
	if (cl == N) {
		// 左側だけ全部の場合
		int sabun = dep[V.size()] / 2;
		int mr = -10000;
		for (int i = cr; i <= N; i++) mr = max(mr, dep[i]);
		
		int pr = 10000; for (int i = N; i >= cr; i--) { if (dep[i] == mr) pr = i; }
		int pl = -1; for (int i = 0; i <= pr; i++) { if (dep[i] == mr - sabun) pl = i; }
		if (pl == -1 || pr == -1) return false;
		
		string V1 = V;
		for (int i = pl; i < pr; i++) {
			if (V1[i] == '(') V1[i] = ')';
			else V1[i] = '(';
		}
		return check(V1);
	}
	if (cr == 0) {
		// 右側だけ全部の場合
		int sabun = dep[V.size()] / 2;
		int ml = -10000;
		for (int i = 0; i <= cl; i++) ml = max(ml, dep[i]);
		
		int pl = -1; for (int i = 0; i <= cl; i++) { if (dep[i] == ml) pl = i; }
		int pr = -1; for (int i = N; i >= pl; i--) { if (dep[i] == ml + sabun) pr = i; }
		if (pl == -1 || pr == -1) return false;
		
		string V1 = V;
		for (int i = pl; i < pr; i++) {
			if (V1[i] == '(') V1[i] = ')';
			else V1[i] = '(';
		}
		return check(V1);
	}
	
	// 共通部分がない場合
	int sabun = dep[V.size()] / 2;
	int ml = -10000, mr = -10000;
	for (int i = 0; i <= cl; i++) ml = max(ml, dep[i]);
	for (int i = cr; i <= N; i++) mr = max(mr, dep[i]);
	
	int s = min(ml, mr - sabun);
	int pl = -1; for (int i = 0; i <= cl; i++) { if (dep[i] == s) pl = i; }
	int pr = -1; for (int i = N; i >= cr; i--) { if (dep[i] == s + sabun) pr = i; }
	if (pl == -1 || pr == -1) return false;
	
	string V1 = V;
	for (int i = pl; i < pr; i++) {
		if (V1[i] == '(') V1[i] = ')';
		else V1[i] = '(';
	}
	return check(V1);
}

string S;

int main(){
	cin >> N >> S; vector<int>vec;
	if (N % 2 == 1) { cout << "0" << endl; return 0; }
	for (int i = 0; i < S.size(); i++) {
		if (S[i] == 'x') vec.push_back(i);
	}
	
	int cnt = 0;
	
	for (int i = 0; i < (1 << vec.size()); i++) {
		string T = S;
		for (int j = 0; j < vec.size(); j++) {
			if ((i & (1 << j)) != 0) T[vec[j]] = ')';
			else T[vec[j]] = '(';
		}
		
		bool I = isvalid(T);
		if (I == true) cnt++;
	}
	cout << cnt << endl;
	return 0;
}

Compilation message

securitygate.cpp: In function 'bool check(std::__cxx11::string)':
securitygate.cpp:8:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < V.size(); i++) {
                  ~~^~~~~~~~~~
securitygate.cpp: In function 'bool isvalid(std::__cxx11::string)':
securitygate.cpp:20:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < V.size(); i++) {
                  ~~^~~~~~~~~~
securitygate.cpp:26:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i <= V.size(); i++) { if (dep[i] < 0) break; cl = i; }
                  ~~^~~~~~~~~~~
securitygate.cpp: In function 'int main()':
securitygate.cpp:93:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < S.size(); i++) {
                  ~~^~~~~~~~~~
securitygate.cpp:101:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < vec.size(); j++) {
                   ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 540 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 540 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 3 ms 304 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 7 ms 256 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 4 ms 256 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 6 ms 384 KB Output is correct
29 Correct 3 ms 256 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 6 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 540 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 3 ms 304 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 7 ms 256 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 4 ms 256 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 6 ms 384 KB Output is correct
29 Correct 3 ms 256 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 6 ms 384 KB Output is correct
32 Correct 24 ms 384 KB Output is correct
33 Correct 7 ms 256 KB Output is correct
34 Correct 480 ms 384 KB Output is correct
35 Correct 25 ms 376 KB Output is correct
36 Correct 45 ms 256 KB Output is correct
37 Correct 201 ms 468 KB Output is correct
38 Correct 742 ms 256 KB Output is correct
39 Correct 22 ms 384 KB Output is correct
40 Correct 852 ms 360 KB Output is correct
41 Correct 791 ms 256 KB Output is correct
42 Correct 805 ms 504 KB Output is correct
43 Correct 540 ms 476 KB Output is correct
44 Correct 49 ms 476 KB Output is correct
45 Correct 842 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 540 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 3 ms 304 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 7 ms 256 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 4 ms 256 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 6 ms 384 KB Output is correct
29 Correct 3 ms 256 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 6 ms 384 KB Output is correct
32 Correct 24 ms 384 KB Output is correct
33 Correct 7 ms 256 KB Output is correct
34 Correct 480 ms 384 KB Output is correct
35 Correct 25 ms 376 KB Output is correct
36 Correct 45 ms 256 KB Output is correct
37 Correct 201 ms 468 KB Output is correct
38 Correct 742 ms 256 KB Output is correct
39 Correct 22 ms 384 KB Output is correct
40 Correct 852 ms 360 KB Output is correct
41 Correct 791 ms 256 KB Output is correct
42 Correct 805 ms 504 KB Output is correct
43 Correct 540 ms 476 KB Output is correct
44 Correct 49 ms 476 KB Output is correct
45 Correct 842 ms 376 KB Output is correct
46 Correct 2 ms 384 KB Output is correct
47 Correct 3 ms 384 KB Output is correct
48 Correct 2 ms 384 KB Output is correct
49 Incorrect 3 ms 384 KB Output isn't correct
50 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 540 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 3 ms 304 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 7 ms 256 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 4 ms 256 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 6 ms 384 KB Output is correct
29 Correct 3 ms 256 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 6 ms 384 KB Output is correct
32 Correct 24 ms 384 KB Output is correct
33 Correct 7 ms 256 KB Output is correct
34 Correct 480 ms 384 KB Output is correct
35 Correct 25 ms 376 KB Output is correct
36 Correct 45 ms 256 KB Output is correct
37 Correct 201 ms 468 KB Output is correct
38 Correct 742 ms 256 KB Output is correct
39 Correct 22 ms 384 KB Output is correct
40 Correct 852 ms 360 KB Output is correct
41 Correct 791 ms 256 KB Output is correct
42 Correct 805 ms 504 KB Output is correct
43 Correct 540 ms 476 KB Output is correct
44 Correct 49 ms 476 KB Output is correct
45 Correct 842 ms 376 KB Output is correct
46 Correct 2 ms 384 KB Output is correct
47 Correct 3 ms 384 KB Output is correct
48 Correct 2 ms 384 KB Output is correct
49 Incorrect 3 ms 384 KB Output isn't correct
50 Halted 0 ms 0 KB -