Submission #861865

#TimeUsernameProblemLanguageResultExecution timeMemory
861865iskhakkutbilimparentrises (BOI18_parentrises)C++17
22 / 100
1064 ms262144 KiB
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define all(a) a.begin(), a.end() const int mod = 1e9 + 7; int P; int readable(string &s){ int n = s.size(); map<pair<int, int>, bool> dp[n]; if(s[0] == ')' or s[n-1] == '('){ return 0; } dp[0][{0, 1}] = 1, dp[0][{1, 0}] = 1; dp[0][{1, 1}] = 1; for(int i = 1;i < n; i++){ for(auto [pp, y] : dp[i-1]){ int x1 = pp.ff, x2 = pp.ss; int ok = 0; int c = s[i]==')' ? -1 : 1; if(x2 + c >= 0){ ok++; dp[i][{x1, x2 + c}] = 1; } if(x1 + c >= 0){ ok++; dp[i][{x1 + c, x2}] = 1; } if(ok == 2){ dp[i][{x1 + c, x2 + c}] = 1; } } } if(dp[n-1][{0, 0}]){ return 1; }else{ return 0; } } void solve(){ string s; cin >> s; int n = s.size(); map<pair<int, int>, bool> dp[n]; string path[n][n + 1][n + 1]; // RBG if(s[0] == ')' or s[n-1] == '('){ cout << "impossible"; return; } dp[0][{0, 1}] = 1, dp[0][{1, 0}] = 1; dp[0][{1, 1}] = 1; path[0][0][1] = "R"; path[0][1][1] = "G"; path[0][1][0] = "B"; vector<int> pref(n); pref[0] = 1; for(int i = 1;i < n; i++){ pref[i] = pref[i-1] + s[i]==')' ? -1 : 1; } for(int i = 1;i < n; i++){ for(auto [pp, y] : dp[i-1]){ int x1 = pp.ff, x2 = pp.ss; int ok = 0; int c = s[i]==')' ? -1 : 1; if(x2 + c >= 0){ ok++; dp[i][{x1, x2 + c}] = 1; path[i][x1][x2 + c] = path[i-1][x1][x2] + "R"; } if(x1 + c >= 0){ ok++; dp[i][{x1 + c, x2}] = 1; path[i][x1 + c][x2] = path[i-1][x1][x2] + "B"; } if(ok == 2){ dp[i][{x1 + c, x2 + c}] = 1; path[i][x1 + c][x2 + c] = path[i-1][x1][x2] + "G"; } } } if(dp[n-1][{0, 0}]){ cout << path[n-1][0][0]; }else{ cout << "impossible"; } } void solve1(){ int n; cin >> n; int ans = 0; for(int mask = 0; mask < (1<<n); mask++){ string s = ""; for(int i = 0;i < n; i++){ if(mask & (1<<i)) s.push_back('('); else s.push_back(')'); } ans+= readable(s); } cout << ans; } main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> P; int test; cin >> test; while(test--){ if(P == 1)solve(); else solve1(); cout << '\n'; } return 0; }

Compilation message (stderr)

parentrises.cpp:109:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  109 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...