# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
781071 | Blagoj | Paint By Numbers (IOI16_paint) | C++17 | 0 ms | 0 KiB |
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;
const int MAXN = 105;
int n, m;
int h[MAXN], v[MAXN];
int a[MAXN][MAXN];
int dp[MAXN][MAXN];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
for (int j = 1; j <= m; j++) {
cin >> v[j];
}
// Initialize the first row and column of the dp table
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
dp[i][0] = dp[i-1][0] && (h[i] == 0);
}
for (int j = 1; j <= m; j++) {
dp[0][j] = dp[0][j-1] && (v[j] == 0);
}
// Fill in the rest of the dp table
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (h[i] == 0 && v[j] == 0) {
dp[i][j] = dp[i-1][j] || dp[i][j-1];
} else if (h[i] == 0) {
int sum = 0;
for (int k = j-v[j]; k <= j-1; k++) {
sum += a[i][k];
}
dp[i][j] = (sum == 0) && dp[i-1][j];
} else if (v[j] == 0) {
int sum = 0;
for (int k = i-h[i]; k <= i-1; k++) {
sum += a[k][j];
}
dp[i][j] = (sum == 0) && dp[i][j-1];
} else {
int sum1 = 0;
for (int k = i-h[i]; k <= i-1; k++) {
sum1 += a[k][j];
}
int sum2 = 0;
for (int k = j-v[j]; k <= j-1; k++) {
sum2 += a[i][k];
}
dp[i][j] = (sum1 == 0) && (sum2 == 0) && dp[i-1][j] && dp[i][j-1];
}
if (dp[i][j]) {
if (h[i] > 0) {
for (int k = j-v[j]; k <= j-1; k++) {
a[i][k] = 1;
}
}
if (v[j] > 0) {
for (int k = i-h[i]; k <= i-1; k++) {
a[k][j] = 1;
}
}
}
}
}
// Check if the solution is valid
if (dp[n][m]) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}