Submission #204862

#TimeUsernameProblemLanguageResultExecution timeMemory
204862WLZPaint By Numbers (IOI16_paint)C++14
59 / 100
6 ms384 KiB
#include "paint.h" #include <bits/stdc++.h> using namespace std; string solve_puzzle(string s, vector<int> c) { int n = (int) s.length(), k = (int) c.size(); c.insert(c.begin(), 0); s = '$' + s + '$'; vector< vector<int> > pre(n + 1, vector<int>(2, 0)); for (int i = 1; i <= n; i++) { if (s[i] == '_') { pre[i][0] = 1; } else if (s[i] == 'X') { pre[i][1] = 1; } pre[i][0] += pre[i - 1][0]; pre[i][1] += pre[i - 1][1]; } vector< vector<int> > dp_left(k + 1, vector<int>(n + 1, 0)), dp_right(k + 2, vector<int>(n + 2, 0)); for (int j = 0; j <= n; j++) { dp_left[0][j] = (pre[j][1] == 0); } for (int i = 1; i <= k; i++) { for (int j = 1; j <= n; j++) { int b1 = dp_left[i][j - 1]; int b2 = (j - c[i] >= 0 && pre[j][0] - pre[j - c[i]][0] == 0 && s[j - c[i]] != 'X'); if (i == 1) { if (j - c[i] >= 1) { b2 = b2 && dp_left[i - 1][j - c[i] - 1]; } } else { b2 = b2 && (j - c[i] >= 1 && dp_left[i - 1][j - c[i] - 1]); } if (s[j] == 'X') { dp_left[i][j] = b2; } else if (s[j] == '_') { dp_left[i][j] = b1; } else { dp_left[i][j] = b1 || b2; } } } for (int j = n; j >= 1; j--) { dp_right[k + 1][j] = (pre[n][1] - pre[j - 1][1] == 0); } dp_right[k + 1][0] = (pre[n][1] == 0); dp_right[k + 1][n + 1] = 1; for (int i = k; i >= 1; i--) { for (int j = n; j >= 1; j--) { int b1 = dp_right[i][j + 1]; int b2 = (j + c[i] <= n + 1 && pre[j + c[i] - 1][0] - pre[j - 1][0] == 0 && s[j + c[i]] != 'X'); if (i == k) { if (j + c[i] <= n) { b2 = b2 && dp_right[i + 1][j + c[i] + 1]; } } else { b2 = b2 && (j + c[i] <= n && dp_right[i + 1][j + c[i] + 1]); } if (s[j] == 'X') { dp_right[i][j] = b2; } else if (s[j] == '_') { dp_right[i][j] = b1; } else { dp_right[i][j] = b1 || b2; } } } vector<int> white(n + 1), black(n + 1); for (int j = 1; j <= n; j++) { if (s[j] == 'X') { white[j] = 0; continue; } if (s[j] == '_') { white[j] = 1; continue; } for (int i = 0; i <= k; i++) { if (dp_left[i][j - 1] && dp_right[i + 1][j + 1]) { white[j] = 1; break; } } } for (int i = 1; i <= k; i++) { for (int j = 1; j + c[i] - 1 <= n; j++) { if (s[j - 1] == 'X' || s[j + c[i]] == 'X') { continue; } if (pre[j + c[i] - 1][0] - pre[j - 1][0] > 0) { continue; } int b1, b2; if (j == 1) { b1 = (i == 1); } else { b1 = dp_left[i - 1][j - 2]; } if (j + c[i] - 1 == n) { b2 = (i == k); } else { b2 = dp_right[i + 1][j + c[i] + 1]; } if (b1 && b2) { for (int t = j + c[i] - 1; t >= j && !black[t]; t--) { black[t] = 1; } } } } for (int i = 1; i <= n; i++) { if (s[i] == '.') { if (white[i] && black[i]) { s[i] = '?'; } else if (white[i] && !black[i]) { s[i] = '_'; } else if (!white[i] && black[i]) { s[i] = 'X'; } } } return s.substr(1, n); }
#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...