이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "paint.h"
#include <bits/stdc++.h>
using namespace std;
//20 2
string solve_puzzle(string s, vector<int> c) {
int k = c.size(), n = s.size();
//dp[i][j] - if it's possible to cover all first i characters by using j segments
int dp[n][k];
memset(dp, 0, sizeof dp);
int blackPref[n], whitePref[n];
for (int i = 0; i < n; i++) {
whitePref[i] = (i ? whitePref[i - 1] : 0) + (s[i] == '_');
blackPref[i] = (i ? blackPref[i - 1] : 0) + (s[i] == 'X');
}
for (int i = 0; i < n; i++) {
if (i + 1 >= c[0]) {
if (i < c[0])
dp[i][0] = whitePref[i] == 0;
else
dp[i][0] = (whitePref[i] - whitePref[i - c[0]] == 0) && (blackPref[i - c[0]] == 0);
}
for (int j = 1; j < k; j++)
if (i > c[j])
dp[i][j] = (whitePref[i] - whitePref[i - c[j]] == 0) && (s[i - c[j]] != 'X') && dp[i - c[j] - 1][j - 1];
}
//for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) cout << dp[j][i]; cout << endl; }
for (int i = k - 1; i > 0; i--)
for (int j = n - 1; j >= 0; j--) {
if (dp[j][i]) {
for (int l = j - c[i]; l < n; l++)
dp[l][i - 1] = 0;
break;
}
}
//for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) cout << dp[j][i]; cout << endl; }
string ans = s;
for (char& i : ans)
if (i == '.')
i = '?';
int canBeBlack[n], canBeWhite[n];
memset(canBeBlack, 0, sizeof canBeBlack);
memset(canBeWhite, 0, sizeof canBeWhite);
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
if (dp[i][j]) {
canBeBlack[i - c[j] + 1]++;
if (i + 1 < n)
canBeBlack[i + 1]--;
if (j == 0) {
canBeWhite[0]++;
if (i - c[j] + 1 < n)
canBeWhite[i - c[j] + 1]--;
}
if (j == k - 1) {
if (i + 1 < n)
canBeWhite[i + 1]++;
}
if (i - c[j] >= 0) {
canBeWhite[i - c[j]]++;
if (i - c[j] + 1 < n)
canBeWhite[i - c[j] + 1]--;
}
}
int sum = 0, sumw = 0;
for (int i = 0; i < n; i++) {
sum += canBeBlack[i];
sumw += canBeWhite[i];
//cout << i << " " << sum << " " << sumw << endl;
if (sum && sumw)
assert(ans[i] == '?');
else if (sum) {
assert(ans[i] != '_');
ans[i] = 'X';
} else if (sumw) {
assert(ans[i] != 'X');
ans[i] = '_';
} else
assert(0);
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |