이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*input
*/
#include <bits/stdc++.h>
#include "paint.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
str solve_puzzle(str s, vi c) {
int N = (int)s.size();
int K = (int)c.size();
s.insert(s.begin(), ' ');
c.insert(c.begin(), 0);
vbb dp(N + 1, vb(K + 1, false));
vii trues(K + 1);
vi black(N + 1, 0);
vi white(N + 1, 0);
for (int i = 1; i <= N; ++i)
{
black[i] = black[i - 1] + int(s[i] == 'X');
white[i] = white[i - 1] + int(s[i] == '_');
}
vi lastB(N + 1);
vi lastW(N + 1);
{
int lb = -1;
int lw = -1;
for (int i = 0; i <= N; ++i)
{
if (s[i] == 'X') lb = i;
if (s[i] == '_') lw = i;
lastB[i] = lb;
lastW[i] = lw;
}
}
auto get = [](int a, int b, vi & vec) {
return vec[b] - (a ? vec[a - 1] : 0);
};
dp[0][0] = true;
trues[0].emplace_back(-1);
// print(white);
// print(black);
for (int k = 1; k <= K; ++k)
{
for (int n = 1; n <= N; ++n)
{
if (s[n] == '_') continue;
if (n - c[k] < 0) continue;
if (get(n - c[k] + 1, n, white) > 0) continue;
if (k == K and get(n + 1, N, black) > 0) continue;
int lb = lastB[n - c[k]];
auto it = lower_bound(trues[k - 1].begin(), trues[k - 1].end(), lb);
// printf("k = %d, n = %d whites = %d\n", k, n, get(n - c[k], n, white));
// printf("lb = %d, it = %d\n", lb, *it);
if (it != trues[k - 1].end() and * it < n - c[k]) {
dp[n][k] = true;
trues[k].emplace_back(n);
}
}
}
// for (int i = 1; i <= K; ++i) print(trues[i]);
vi yrB(N + 2, 0);
vi yrW(N + 2, 0);
{
vb legal(N + 1, true);
for (int k = K; k >= 1; --k)
{
vi newLegal(N + 2, 0);
for (int n = 1; n <= N; ++n)
{
if (dp[n][k] and legal[n]) {
yrB[n - c[k] + 1]++;
yrB[n + 1]--;
int lb = lastB[n - c[k]];
auto it = lower_bound(trues[k - 1].begin(), trues[k - 1].end(), lb);
// printf("k = %d, n = %d whites = %d\n", k, n, get(n - c[k], n, white));
// printf("lb = %d, it = %d\n", lb, *it);
if (it != trues[k - 1].end() and * it < n - c[k]) {
yrW[*it + 1]++;
yrW[n - c[k] + 1]--;
newLegal[max(1, *it)]++;
newLegal[n - c[k]]--;
}
}
}
for (int i = 1; i <= N; ++i)
{
newLegal[i] += newLegal[i - 1];
legal[i] = bool(newLegal[i] > 0);
}
}
}
int pab = trues[K][0];
yrW[pab + 1]++;
// printf("pab = %d\n", pab);
str ats(N, '?');
for (int i = 1; i <= N; ++i)
{
yrW[i] += yrW[i - 1];
yrB[i] += yrB[i - 1];
if (s[i] != '.') {
ats[i - 1] = s[i];
continue;
}
if (yrB[i] > 0 and yrW[i] > 0) ats[i - 1] = '?';
else if (yrW[i] > 0) ats[i - 1] = '_';
else if (yrB[i] > 0) ats[i - 1] = 'X';
}
// print(yrW, "yrW: ");
// print(yrB, "yrB: ");
return ats;
}
# | 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... |