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;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 2003;
const int lx[] = {-1, 0, 0, 1}, ly[] = {0, -1, 1, 0};
struct State {
int x, y, t;
State(int _x = 0, int _y = 0, int _t = 0) : x(_x), y(_y), t(_t) {};
};
int dp[MAXN][MAXN][4], nSize;
bool dx[MAXN][MAXN][4], isTree[MAXN][MAXN];
bool bfsCheck(int x, int y) {
int cntEmpty(0);
for (int i = 1; i <= nSize; ++i) {
for (int j = 1; j <= nSize; ++j) {
for (int t = 0; t < 4; ++t) {
dp[i][j][t] = (isTree[i][j]) ? -1 : 1e9;
dx[i][j][t] = 0;
}
cntEmpty += (!isTree[i][j]);
}
}
deque<State> dq;
for (int t = 0; t < 4; ++t) {
dq.push_back(State(x, y, t));
dp[x][y][t] = 1;
}
int maxdp(0), cnt(0);
while(sz(dq)) {
int x(dq.front().x), y(dq.front().y), t(dq.front().t);
dq.pop_front();
if(dx[x][y][t])
continue;
dx[x][y][t] = 1;
for (int id = 0; id < 4; ++id) {
int u(x + lx[id]), v(y + ly[id]);
if(min(u, v) < 1 || max(u, v) > nSize || dp[u][v][id] <= dp[x][y][t] + (t != id))
continue;
dp[u][v][id] = dp[x][y][t] + (t != id);
if(t == id) {
dq.push_front(State(u, v, id));
} else {
dq.push_back(State(u, v, id));
}
}
}
for (int i = 1; i <= nSize; ++i) {
for (int j = 1; j <= nSize; ++j) {
int mindp = min({dp[i][j][0], dp[i][j][1], dp[i][j][2], dp[i][j][3]});
maxdp = max(maxdp, mindp);
cnt += (!isTree[i][j] && mindp < int(1e9));
}
}
return (maxdp <= 2 && cnt == cntEmpty);
}
int biggest_stadium(int n, vector<vector<int>> F) {
nSize = n;
int cntTree(0);
for (int i = 1; i <= nSize; ++i) {
for (int j = 1; j <= nSize; ++j) {
isTree[i][j] = (F[i - 1][j - 1]);
cntTree += isTree[i][j];
}
}
bool check(1);
for (int i = 1; i <= nSize; ++i) {
for (int j = 1; j <= nSize; ++j) {
if(isTree[i][j])
continue;
if((i == 1) + (j == 1) + (i == nSize) + (j == nSize) + (i > 1 && isTree[i - 1][j]) + (j > 1 && isTree[i][j - 1]) + (j < nSize && isTree[i][j + 1]) + (i < nSize && isTree[i + 1][j]) >= 2)
check &= (bfsCheck(i, j));
}
}
if(cntTree == 0 || check)
return nSize * nSize - cntTree;
return 1;
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "soccer"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
vector<vector<int>> F;
int n;
cin >> n;
F.resize(n);
for (int i = 0; i < n; ++i) {
F[i].resize(n);
for (int j = 0; j < n; ++j)
cin >> F[i][j];
}
int ans = biggest_stadium(n, F);
cout << "ANSWER: " << ans << '\n';
return 0;
}
#endif // Nhoksocqt1
# | 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... |