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>
using namespace std;
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python!
// pairs
using pii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
#define mp make_pair
#define f first
#define s second
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pl>;
using vpd = V<pd>;
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
const int MOD = 998244353;
const int MX = 2e5+5;
const ll BIG = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4]{1,0,-1,0}, dy[4]{0,1,0,-1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
struct DSU {
vi e; void init(int N) { e = vi(N,-1); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool sameSet(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) { // union by size
x = get(x), y = get(y); if (x == y) return 0;
if (e[x] > e[y]) swap(x,y);
e[x] += e[y]; e[y] = x; return 1;
}
};
/*
inline namespace Helpers {
//////////// is_iterable
// https://stackoverflow.com/questions/13830158/check-if-a-variable-type-is-iterable
// this gets used only when we can call begin() and end() on that type
tcT, class = void> struct is_iterable : false_type {};
tcT> struct is_iterable<T, void_t<decltype(begin(declval<T>())),
decltype(end(declval<T>()))
>
> : true_type {};
tcT> constexpr bool is_iterable_v = is_iterable<T>::value;
//////////// is_readable
tcT, class = void> struct is_readable : false_type {};
tcT> struct is_readable<T,
typename std::enable_if_t<
is_same_v<decltype(cin >> declval<T&>()), istream&>
>
> : true_type {};
tcT> constexpr bool is_readable_v = is_readable<T>::value;
//////////// is_printable
// // https://nafe.es/posts/2020-02-29-is-printable/
tcT, class = void> struct is_printable : false_type {};
tcT> struct is_printable<T,
typename std::enable_if_t<
is_same_v<decltype(cout << declval<T>()), ostream&>
>
> : true_type {};
tcT> constexpr bool is_printable_v = is_printable<T>::value;
}*/
#define f first
#define s second
#define pii pair <int, int>
#define pb push_back
const int N = 2e5 + 5;
int t,n,a[N], prw[N],prt[N],prx[N],prsum[105][N],bad[N], dp[N][105],k,c[N], posprx[N],posw[N];
string s;
int badsf[N],sfsum[105][N],dpsuf[N][105];
int getw(int le, int ri) {
if (le > ri || le < 0 || ri > n) return 0;
return prw[ri] - prw[le - 1];
}
int get_(int le, int ri) {
if (le > ri || le < 0 || ri > n) return 0;
return prt[ri] - prt[le - 1];
}
int getx(int le, int ri) {
if (le > ri || le < 0 || ri > n) return 0;
return prx[ri] - prx[le - 1];
}
int check(int idx, int k) {
if (k == 0 && getx(1,idx) == 0) return 1;
if (k == 0 && idx == 0) return 1;
if (s[idx] == 'X') return 0;
int pridx = bad[idx];// bolo X
if (prsum[k][idx - 1] - prsum[k][pridx - 1] > 0) return 1;
else return 0;
}
void add(int k) {
for (int i = 1; i <= n; i++) {
prsum[k][i] = dp[i][k] + prsum[k][i - 1];
}
}
int check1(int idx, int k) {
if (k == 0 && getx(idx, n) == 0) return 1;
if (k == 0 && idx == n + 1) return 1;
if (s[idx] == 'X') return 0;
int sfidx = badsf[idx];
if (sfsum[k][idx + 1] - sfsum[k][sfidx + 1] > 0) return 1;
else return 0;
}
void add1(int k) {
for(int i = n; i >= 1; i--) {
sfsum[k][i] = dpsuf[i][k] + sfsum[k][i + 1];
}
}
std::string solve_puzzle(std::string ss, std::vector<int> cc) {
s = ss;
n = s.size();
s = "@" + s;
int id = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == 'X') id = i;
bad[i] = id;
prw[i] = prw[i - 1] + (s[i] == '.');
prt[i] = prt[i - 1] + (s[i] == '_');
prx[i] = prx[i - 1] + (s[i] == 'X');
}
id = n + 1;
for (int i = n; i >= 1; i--) {
if (s[i] == 'X') id = i;
badsf[i] = id;
}
k = cc.size();
for (int i = 1; i <= k; i++) {
c[i] = cc[i - 1];
}
// . unknown ------------ X black ------- _ white
for (int j = 0; j <= k; j++) {
for (int i = 1; i <= n; i++) {
if (j == 0) {
if (getx(1, i) == 0) dp[i][j] = 1;
else dp[i][j] = 0;
continue;
}
if (j == 1) {
if (i - c[j] + 1 > 0 && get_(i - c[j] + 1, i) == 0 && getx(1, i - c[j]) == 0) dp[i][j] = 1;
else dp[i][j] = 0;
continue;
}
if (s[i] == '.') {
// dp[i][j]
// aq rom X davwero
if ( i - c[j] + 1 >= 1 && get_(i - c[j] + 1, i) == 0 && check(i - c[j], j - 1)) {
dp[i][j] = 1;
} else dp[i][j] = 0;
continue;
}
if (s[i] == '_') {
dp[i][j] = 0;
continue;
}
if (s[i] == 'X') {
if (i - c[j] + 1 >= 1 && get_(i - c[j] + 1, i) == 0 && check(i - c[j], j - 1)) {
dp[i][j] = 1;
} else dp[i][j] = 0;
//if (dp[i][j] == 0) assert(false);
continue;
}
}
add(j);
}
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= k; j++) {
// if (dp[i][j]) cout<<i<<" "<<" "<<j<<" "<<dp[i][j]<<"\n";
// }
// }
for (int j = 0; j <= k; j++) {
for (int i = n; i >= 1; i--) {
if (j == 0) {
if (getx(i,n) == 0) dpsuf[i][j] = 1;
else dpsuf[i][j] = 0;
continue;
}
if (j == 1) {
if (i + c[k - j + 1] - 1 <= n && get_(i, i + c[k - j + 1] - 1) == 0 && getx(i + c[k - j + 1], n) == 0) dpsuf[i][j] = 1;
else dpsuf[i][j] = 0;
//cout<<i+ c[k - j + 1] - 1<<" "<<get_(i, i + c[k - j + 1] - 1)<<" "<<getx(i + c[k - j + 1],n)<<" "<<dpsuf[i][j]<<endl;
continue;
}
if (s[i] == '.') {
// dp[i][j]
// aq rom X davwero
if (i + c[k - j + 1] - 1 <= n && get_(i, i + c[k - j + 1] - 1) == 0 && check1(i + c[k - j + 1], j - 1)) {
//cout<<i<<" "<<c[k - j + 1]<<endl;
dpsuf[i][j] = 1;
} else dpsuf[i][j] = 0;
continue;
}
if (s[i] == '_') {
dpsuf[i][j] = 0;
continue;
}
if (s[i] == 'X') {
if (i + c[k - j + 1] - 1 <= n && get_(i, i + c[k - j + 1] - 1) == 0 && check1(i + c[k - j + 1], j - 1)) {
dpsuf[i][j] = 1;
} else dpsuf[i][j] = 0;
//if (dp[i][j] == 0) assert(false);
continue;
}
}
add1(j);
}
for (int j = 0; j <= k; j++) {
// cout<<j<<" --- ";
for (int i = n; i >= 1; i--) {
// cout<<dpsuf[i][j]<<" ";
}
// cout<<"\n";
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
if (dp[i][j] && check1(i + 1, k - j)) {
//cout<<i<<" "<<j<<"\n";
posprx[i + 1]--, posprx[i - c[j] + 1]++;
}
}
}
for (int i = 1; i <= n; i++) {
posprx[i] += posprx[i - 1];
}
for (int i = 1; i <= n; i++) {
if (posprx[i] < 0 ) assert(false);
//if (posprx[i]) cout<<i<<" ";
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= k; j++) {
if (check(i, j) && check1(i, k - j)) posw[i] = 1;
}
}
for (int i = 1; i <= n; i++) {
if (posw[i] && posprx[i]) cout<<"?";
else if(posw[i]) cout<<"_";
else if (posprx[i]) cout<<"X";
}
}
/*
Sample test:
..._._....
1 3
*/
Compilation message (stderr)
paint.cpp: In function 'std::string solve_puzzle(std::string, std::vector<int>)':
paint.cpp:282:1: warning: control reaches end of non-void function [-Wreturn-type]
282 | }
| ^
# | 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... |