QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#528533 | #1820. Contest Construction | PetroTarnavskyi# | WA | 0ms | 3788kb | C++20 | 2.5kb | 2024-08-23 15:56:16 | 2024-08-23 15:56:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
struct DSU
{
int n;
VI p, sz;
void init(int _n)
{
n = _n;
p.resize(n);
iota(ALL(p), 0);
sz.assign(n, 1);
}
int find(int v)
{
if (v == p[v])
return v;
return p[v] = find(p[v]);
}
bool unite(int u, int v)
{
u = find(u);
v = find(v);
if (u == v)
return false;
if (sz[u] > sz[v])
swap(u, v);
p[u] = v;
sz[v] += sz[u];
return true;
}
};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int k;
cin >> k;
vector<VI> obst(n, VI(m));
FOR (i, 0, k)
{
int x, y;
cin >> x >> y;
x--, y--;
obst[x][y]++;
}
vector<VI> s(n + 1, VI(m + 1));
FOR (i, 0, n)
{
FOR (j, 0, m)
{
s[i + 1][j + 1] = obst[i][j] + s[i + 1][j] + s[i][j + 1] - s[i + 1][j + 1];
}
}
int l = 0, r = n + m;
while (l + 1 < r)
{
int sz = (r + l) / 2;
vector<VI> good(n, VI(m));
FOR (i, 0, n)
{
FOR (j, 0, m)
{
if (i + sz < n && j + sz < m)
{
int sum = s[i + sz][j + sz] - s[i + sz][j] - s[i][j + sz] + s[i][j];
if (sum == 0)
good[i][j] = 1;
}
}
}
DSU d;
d.init(n * m);
int cnt = 0;
int mx = 0;
FOR (i, 0, n)
{
FOR (j, 0, m)
{
if (good[i][j])
{
cnt++;
if (i && good[i - 1][j])
d.unite(i * m + j, i * m - m + j);
if (j && good[i][j - 1])
d.unite(i * m + j, i * m - 1 + j);
mx = max(mx, d.sz[d.find(i * m + j)]);
}
}
}
if (mx < cnt)
{
r = sz;
continue;
}
vector<VI> pref(n + 1, VI(m + 1));
FOR (i, 0, n)
{
FOR (j, 0, m)
{
pref[i + 1][j + 1] = good[i][j] + pref[i + 1][j] + pref[i][j + 1] - pref[i + 1][j + 1];
}
}
bool ok = true;
FOR (i, 0, n)
{
FOR (j, 0, m)
{
if (!obst[i][j])
{
int sum = pref[i + 1][j + 1] - pref[i + 1][max(j + 1 - sz, 0)] - pref[max(i + 1 - sz, 0)][j + 1] + pref[max(i + 1 - sz, 0)][max(j + 1 - sz, 0)];
if (sum == 0)
ok = false;
}
}
}
if (ok)
l = sz;
else
r = sz;
}
if (l == 0)
l = -1;
cout << l << '\n';
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3788kb
input:
5 4 2 1 4 3 5
output:
-1
result:
wrong answer expected '4', found '-1'