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;
ifstream in("file.in");
int get_between(int i, int j, vector<int> &Pre_Sum)
{
if (i > j)
swap(i, j);
if (i == 0)
return Pre_Sum[j];
return Pre_Sum[j] - Pre_Sum[i - 1];
}
int check_block_P (int i, int j, int set, vector<vector<int>> &P, string &s, vector<int> &Pre_Sum)
{
if (j - set + 1 < 0)
return 0;
if (get_between(j - set + 1, j, Pre_Sum) > 0)
return 0;
if (j - set >= 0 && s[j - set] == 'X')
return 0;
if (j + 1 < s.length() && s[j + 1] == 'X')
return 0;
if (j - set > 0 && P[i - 1][j - set - 1] == 0)
return 0;
if (j - set <= 0 && i != 1)
return 0;
return 1;
}
int check_block_S (int i, int j, int set, vector<vector<int>> &S, string &s, vector<int> &Pre_Sum)
{
int l = s.length();
if (j + set - 1 >= l)
return 0;
if (get_between(j, j + set - 1, Pre_Sum) > 0)
return 0;
if (j - 1 >= 0 && s[j - 1] == 'X')
return 0;
if (j + set <= l && s[j + set] == 'X')
return 0;
if (j + set < l - 1 && S[i - 1][j + set + 1] == 0)
return 0;
if (j + set >= l - 1 && i != 1)
return 0;
return 1;
}
void fill (string &ans, int j, int set)
{
for (int i = j; i < j + set; i++)
if (ans[i] == '_')
ans[i] = '?';
}
bool can_end_1 (vector<vector<int>> &P, vector<vector<int>> &S, int i, int j, int k, int l)
{
bool p1 = true, p2 = true;
if (i == 0)
{
if (j != 0)
p1 = false;
}
else p1 = P[j][i - 1];
if (i == l - 1)
{
if (j != k)
p2 = false;
}
else p2 = S[k - j][i + 1];
return p1 && p2;
}
bool can_end_2 (vector<vector<int>> &P, vector<vector<int>> &S, int i, int j, int k, int l, int set)
{
//P[i - 1][j - 1] == 1 && S[k - i][j + set] == 1
bool p1 = true, p2 = true;
if (j < 1)
{
if (i - 1 != 0)
p1 = false;
}
else p1 = P[i - 1][j - 1];
if (j + set == l)
{
if (k - i != 0)
p2 = false;
}
else p2 = S[k - i][j + set];
return p1 && p2;
}
string solve_puzzle (string s, vector<int> c)
{
int k = c.size(), l = s.length();
bool change_back = false;
string ans;
vector<int> Pre_Sum(l, 0);
vector<vector<int>> P(k + 1, vector<int> (l, 0)), S = P;
ans.assign(l, '_');
for (int i = 0; i < l; i++)
{
if (s[i] == 'X')
ans[i] = 'X';
if (s[i] == '_')
Pre_Sum[i]++;
if (i > 0)
Pre_Sum[i]+= Pre_Sum[i - 1];
}
for (int i = 0; i < l; i++)
{
P[0][i] = 1;
if (s[i] == 'X')
{ P[0][i] = 0; break; }
}
for (int i = 1; i <= k; i++)
for (int j = 0; j < l; j++)
{
switch(s[j])
{
case '_':{
if (j > 0)
P[i][j] = P[i][j - 1];
break;
}
case 'X':{
P[i][j] = check_block_P(i, j, c[i - 1], P, s, Pre_Sum);
break;
}
case '.':{
if (j > 0)
P[i][j] = P[i][j - 1];
P[i][j] = max(P[i][j], check_block_P(i, j, c[i - 1], P, s, Pre_Sum));
}
}
}
for (int i = l - 1; i >= 0; i--)
{
S[0][i] = 1;
if (s[i] == 'X')
{ S[0][i] = 0; break; }
}
for (int i = 1; i <= k; i++)
for (int j = l - 1; j >= 0; j--)
{
switch(s[j])
{
case '_':{
if (j + 1 < l)
S[i][j] = S[i][j + 1];
break;
}
case 'X':{
S[i][j] = check_block_S(i, j, c[k - i], S, s, Pre_Sum);
break;
}
case '.':{
if (j + 1 < l)
S[i][j] = S[i][j + 1];
S[i][j] = max(S[i][j], check_block_S(i, j, c[k - i], S, s, Pre_Sum));
break;
}
}
}
for (int i = 0; i < l; i++)
{
bool ok = false;
for (int j = 0; j <= k; j++)
if (can_end_1(P, S, i, j, k, l))
{ ok = true; break; }
if (!ok)
ans[i] = 'X';
}
for (int i = 1; i <= k; i++)
{
int set = c[i - 1];
for (int j = 0; j <= l - set; j++)
{
if (get_between(j, j + set - 1, Pre_Sum) > 0)
continue;
if (j > 0 && ans[j - 1] == 'X')
continue;
if (j + set < l && ans[j + set] == 'X')
continue;
if (!can_end_2(P, S, i, j, k, l, set))
continue;
fill(ans, j, set);
}
}
return ans;
}
Compilation message (stderr)
paint.cpp: In function 'int check_block_P(int, int, int, std::vector<std::vector<int> >&, std::string&, std::vector<int>&)':
paint.cpp:22:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | if (j + 1 < s.length() && s[j + 1] == 'X')
| ~~~~~~^~~~~~~~~~~~
paint.cpp: In function 'std::string solve_puzzle(std::string, std::vector<int>)':
paint.cpp:98:10: warning: unused variable 'change_back' [-Wunused-variable]
98 | bool change_back = false;
| ^~~~~~~~~~~
# | 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... |