This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "paint.h"
using namespace std;
#define fi first
#define se second
#define _size(x) (int)x.size()
#define BIT(i, x) ((x >> i) & 1)
#define MASK(n) ((1 << n) - 1)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; i++)
#define FORD(i, a, b) for (int i = a, _b = (b); i >= _b; i--)
#define FORB1(i, mask) for (int i = mask; i > 0; i ^= i & - i)
#define FORB0(i, n, mask) for (int i = ((1 << n) - 1) ^ mask; i > 0; i ^= i & - i)
#define FORALL(i, a) for (auto i: a)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);
string solve_puzzle(string s, vector<int> c) {
int n = _size(s), k = _size(c);
s = ' ' + s;
vector<vector<int>> cnt(n + 1, vector<int>(2));
FOR(i, 1, n) {
REP(type, 2) cnt[i][type] = cnt[i - 1][type];
if (s[i] == 'X') cnt[i][0]++;
else if (s[i] == '_') cnt[i][1]++;
}
vector<vector<short int>> f(n + 1, vector<short int>(k + 1));
f[0][0] |= (1 << 1);
FOR(i, 1, n) {
FOR(j, 0, k) {
REP(type, 2) {
if ((s[i] == 'X' && type) || (s[i] == '_' && !type)) continue;
if (s[i] == 'X') {
if (!j) continue;
if (i < c[j - 1] || cnt[i][1] - cnt[i - c[j - 1]][1]) continue;
if (BIT(1, f[i - c[j - 1]][j - 1])) f[i][j] |= (1 << type);
}
else if (s[i] == '_') {
if (max(BIT(0, f[i - 1][j]), BIT(1, f[i - 1][j]))) f[i][j] |= (1 << type);
}
else {
if (max(BIT(0, f[i - 1][j]), BIT(1, f[i - 1][j]))) f[i][j] |= (1 << 1);
if (!j) continue;
if (i < c[j - 1] || cnt[i][1] - cnt[i - c[j - 1]][1]) continue;
if (BIT(1, f[i - c[j - 1]][j - 1])) f[i][j] |= (1 << 0);
}
}
}
}
FORD(i, n, 1) {
FORD(j, k, 0) {
REP(type, 2) {
if (!BIT(type, f[i][j])) continue;
if (j == k && cnt[n][0] - cnt[i][0] == 0) f[i][j] |= (1 << (type + 2));
if (!BIT(type + 2, f[i][j])) continue;
if (type == 1) {
if (BIT(0, f[i - 1][j])) f[i - 1][j] |= (1 << 2);
if (BIT(1, f[i - 1][j])) f[i - 1][j] |= (1 << 3);
}
else if (BIT(1, f[i - c[j - 1]][j - 1])) f[i - c[j - 1]][j - 1] |= (1 << 3);
}
}
}
vector<int> _cnt(n + 2);
FOR(i, 1, n) {
FOR(j, 1, k) if (BIT(2, f[i][j])) _cnt[i - c[j - 1] + 1]++, _cnt[i + 1]--;
}
FOR(i, 1, n) _cnt[i] += _cnt[i - 1];
string ans = "";
FOR(i, 1, n) {
if (s[i] == 'X') ans += 'X';
else if (s[i] == '_') ans += '_';
else {
int res = 0;
FOR(j, 0, k) if (BIT(3, f[i][j])) res++;
if (_cnt[i]) {
if (res) ans += '?';
else ans += 'X';
}
else ans += '_';
}
}
return ans;
}
Compilation message (stderr)
paint.cpp: In function 'std::string solve_puzzle(std::string, std::vector<int>)':
paint.cpp:56:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | if (!BIT(type + 2, f[i][j])) continue;
| ~~~~~^~~
paint.cpp:8:26: note: in definition of macro 'BIT'
8 | #define BIT(i, x) ((x >> i) & 1)
| ^
# | 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... |