Submission #62091

# Submission time Handle Problem Language Result Execution time Memory
62091 2018-07-27T12:39:57 Z gs14004 parentrises (BOI18_parentrises) C++17
56 / 100
1000 ms 6556 KB
#include<bits/stdc++.h>
using namespace std;
using pi = pair<int, int>;
using lint = long long;
const int MAXN = 1000050;

bool det(string s){
	queue<int> que;
	string chk;
	int cnt = 0;
	chk.resize(s.size());
	for(int i=0; i<s.size(); i++){
		if(s[i] == '('){
			que.push(i);
			cnt++;
			chk[i] = '?';
		}
		else{
			if(cnt > 0){
				chk[i] = '!';
				cnt--;
			}
			else{
				if(que.empty()) return 0;
				int x = que.front();
				que.pop();
				chk[x] = 'G';
				chk[i] = '!';
			}
		}
	}
	for(int i=(int)s.size()-1; i>=0; i--){
		if(s[i] == ')' && cnt){
			chk[i] = 'G';
			cnt--;
		}
	}
	if(cnt > 0) return 0;
	int c0 = 0, c1 = 0;
	for(int i=0; i<s.size(); i++){
		if(s[i] == '('){
			if(chk[i] == 'G') cnt += 2;
			else{
				cnt++;
				if(cnt & 1) chk[i] = 'R';
				else chk[i] = 'B';
			}
			if(chk[i] != 'B') c0++;
			if(chk[i] != 'R') c1++;
		}
		else{
			if(chk[i] == 'G') cnt -= 2;
			else{
				if(cnt & 1) chk[i] = 'R';
				else chk[i] = 'B';
				cnt--;
			}
			if(chk[i] != 'B') c0--;
			if(chk[i] != 'R') c1--;
		}
		if(c0 < 0 || c1 < 0) return 0;
	}
	if(c0 == 0 && c1 == 0) return 1;
	else return 0;
}

int f(int n){
	int ans = 0;
	for(int i=0; i<(1<<n); i++){
		string s;
		for(int j=0; j<n; j++){
			if((i >> j) & 1) s.push_back('(');
			else s.push_back(')');
		}
		if(det(s)) ans++;
	}
	return ans;
}

string solve(string s){
	queue<int> que;
	string chk;
	int cnt = 0;
	chk.resize(s.size());
	for(int i=0; i<s.size(); i++){
		if(s[i] == '('){
			que.push(i);
			cnt++;
			chk[i] = '?';
		}
		else{
			if(cnt > 0){
				chk[i] = '!';
				cnt--;
			}
			else{
				if(que.empty()) return "impossible";
				int x = que.front();
				que.pop();
				chk[x] = 'G';
				chk[i] = '!';
			}
		}
	}
	for(int i=(int)s.size()-1; i>=0; i--){
		if(s[i] == ')' && cnt){
			chk[i] = 'G';
			cnt--;
		}
	}
	if(cnt > 0) return "impossible";
	int c0 = 0, c1 = 0;
	for(int i=0; i<s.size(); i++){
		if(s[i] == '('){
			if(chk[i] == 'G') cnt += 2;
			else{
				cnt++;
				if(cnt & 1) chk[i] = 'R';
				else chk[i] = 'B';
			}
			if(chk[i] != 'B') c0++;
			if(chk[i] != 'R') c1++;
		}
		else{
			if(chk[i] == 'G') cnt -= 2;
			else{
				if(cnt & 1) chk[i] = 'R';
				else chk[i] = 'B';
				cnt--;
			}
			if(chk[i] != 'B') c0--;
			if(chk[i] != 'R') c1--;
		}
		if(c0 < 0 || c1 < 0) return "impossible";
	}
	if(c0 == 0 && c1 == 0) return chk;
	else return "impossible";
}

int main(){
	char buf[MAXN];
	int p, t; cin >> p;
	if(p == 1){
		scanf("%d",&t);
		while(t--){
			scanf("%s",buf);
			string ans = buf;
			printf("%s\n", solve(buf).c_str());
		}
	}
	else{
		scanf("%d",&t);
		while(t--){
			int n; cin >> n;
			cout << f(n) << endl;
		}
	}
}

Compilation message

parentrises.cpp: In function 'bool det(std::__cxx11::string)':
parentrises.cpp:12:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<s.size(); i++){
               ~^~~~~~~~~
parentrises.cpp:40:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<s.size(); i++){
               ~^~~~~~~~~
parentrises.cpp: In function 'std::__cxx11::string solve(std::__cxx11::string)':
parentrises.cpp:85:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<s.size(); i++){
               ~^~~~~~~~~
parentrises.cpp:113:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<s.size(); i++){
               ~^~~~~~~~~
parentrises.cpp: In function 'int main()':
parentrises.cpp:144:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&t);
   ~~~~~^~~~~~~~~
parentrises.cpp:146:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%s",buf);
    ~~~~~^~~~~~~~~~
parentrises.cpp:152:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&t);
   ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 4 ms 484 KB Output is correct
4 Correct 3 ms 484 KB Output is correct
5 Correct 3 ms 484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 484 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Correct 3 ms 508 KB Output is correct
4 Correct 2 ms 508 KB Output is correct
5 Correct 3 ms 536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 484 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Correct 3 ms 508 KB Output is correct
4 Correct 2 ms 508 KB Output is correct
5 Correct 3 ms 536 KB Output is correct
6 Correct 3 ms 536 KB Output is correct
7 Correct 3 ms 540 KB Output is correct
8 Correct 3 ms 540 KB Output is correct
9 Correct 2 ms 540 KB Output is correct
10 Correct 3 ms 616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 484 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Correct 3 ms 508 KB Output is correct
4 Correct 2 ms 508 KB Output is correct
5 Correct 3 ms 536 KB Output is correct
6 Correct 3 ms 536 KB Output is correct
7 Correct 3 ms 540 KB Output is correct
8 Correct 3 ms 540 KB Output is correct
9 Correct 2 ms 540 KB Output is correct
10 Correct 3 ms 616 KB Output is correct
11 Correct 4 ms 616 KB Output is correct
12 Correct 1 ms 744 KB Output is correct
13 Correct 2 ms 744 KB Output is correct
14 Correct 3 ms 744 KB Output is correct
15 Correct 3 ms 744 KB Output is correct
16 Correct 11 ms 744 KB Output is correct
17 Correct 7 ms 1232 KB Output is correct
18 Correct 6 ms 1232 KB Output is correct
19 Correct 6 ms 1232 KB Output is correct
20 Correct 7 ms 1232 KB Output is correct
21 Correct 63 ms 1436 KB Output is correct
22 Correct 40 ms 6556 KB Output is correct
23 Correct 23 ms 6556 KB Output is correct
24 Correct 37 ms 6556 KB Output is correct
25 Correct 43 ms 6556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 6556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 6556 KB Output is correct
2 Execution timed out 1086 ms 6556 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 29 ms 6556 KB Output is correct
2 Execution timed out 1086 ms 6556 KB Time limit exceeded