QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#84243 | #5668. Cell Nuclei Detection | rniya | AC ✓ | 3008ms | 146280kb | C++17 | 6.8kb | 2023-03-06 03:10:57 | 2023-03-06 03:11:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ALL(x) (x).begin(), (x).end()
#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) void(0)
#endif
template <typename T> istream& operator>>(istream& is, vector<T>& v) {
for (T& x : v) is >> x;
return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
for (size_t i = 0; i < v.size(); i++) {
os << v[i] << (i + 1 == v.size() ? "" : " ");
}
return os;
}
template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }
template <class T> T ceil(T x, T y) {
assert(y >= 1);
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
assert(y >= 1);
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> void mkuni(vector<T>& v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
template <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }
const int INF = (1 << 30) - 1;
const long long IINF = (1LL << 60) - 1;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MOD = 998244353;
// const int MOD = 1000000007;
#include <algorithm>
#include <cassert>
#include <queue>
#include <random>
#include <utility>
#include <vector>
struct BipartiteMatching {
int U, V, t;
bool solved;
std::vector<std::vector<int>> G;
std::vector<int> L, R, visited;
BipartiteMatching(int U, int V) : U(U), V(V), t(0), solved(false), G(U), L(U, -1), R(V, -1), visited(U, -1) {}
void add_edge(int u, int v) {
assert(0 <= u && u < U);
assert(0 <= v && v < V);
G[u].emplace_back(v);
}
void shuffle() {
static std::mt19937 mt;
for (auto& v : G) std::shuffle(v.begin(), v.end(), mt);
}
int solve() {
for (bool updated = true; std::exchange(updated, false); t++) {
for (int i = 0; i < U; i++) {
if (L[i] == -1) {
updated |= dfs(i);
}
}
}
solved = true;
return U - std::count(L.begin(), L.end(), -1);
}
std::vector<std::pair<int, int>> max_matching() const {
assert(solved);
std::vector<std::pair<int, int>> res;
for (int i = 0; i < U; i++) {
if (~L[i]) {
res.emplace_back(i, L[i]);
}
}
return res;
}
private:
bool dfs(int u) {
if (std::exchange(visited[u], t) == t) return false;
for (int& v : G[u]) {
if (R[v] == -1) {
L[u] = v, R[v] = u;
return true;
}
}
for (int& v : G[u]) {
if (dfs(R[v])) {
L[u] = v, R[v] = u;
return true;
}
}
return false;
}
};
struct rectangle {
int lx, ly, rx, ry;
rectangle(int lx, int ly, int rx, int ry) : lx(lx), ly(ly), rx(rx), ry(ry) {}
int area() const { return (rx - lx) * (ry - ly); }
};
int intersection(const rectangle& l, const rectangle& r) {
int lx = max(l.lx, r.lx), ly = max(l.ly, r.ly);
int rx = min(l.rx, r.rx), ry = min(l.ry, r.ry);
if (rx <= lx or ry <= ly) return 0;
return (rx - lx) * (ry - ly);
}
const int MAX = 2010;
vector<int> idxs[MAX][MAX];
void solve() {
int m, n;
cin >> m >> n;
vector<rectangle> L, R;
for (int i = 0; i < m; i++) {
int lx, ly, rx, ry;
cin >> lx >> ly >> rx >> ry;
L.emplace_back(lx, ly, rx, ry);
}
for (int i = 0; i < n; i++) {
int lx, ly, rx, ry;
cin >> lx >> ly >> rx >> ry;
R.emplace_back(lx, ly, rx, ry);
}
auto move = [&]() {
int mx = INF, my = INF;
for (auto& rec : L) {
chmin(mx, rec.lx);
chmin(my, rec.ly);
}
for (auto& rec : R) {
chmin(mx, rec.lx);
chmin(my, rec.ly);
}
for (auto& rec : L) {
rec.lx -= mx;
rec.ly -= my;
rec.rx -= mx;
rec.ry -= my;
}
for (auto& rec : R) {
rec.lx -= mx;
rec.ly -= my;
rec.rx -= mx;
rec.ry -= my;
}
};
auto rotate = [&]() {
vector<rectangle> nL, nR;
for (auto& rec : L) nL.emplace_back(-rec.ry, rec.lx, -rec.ly, rec.rx);
for (auto& rec : R) nR.emplace_back(-rec.ry, rec.lx, -rec.ly, rec.rx);
swap(L, nL);
swap(R, nR);
};
BipartiteMatching BM(m, n);
set<pair<int, int>> s;
for (int _ = 0; _ < 4; _++) {
move();
for (int i = 0; i < n; i++) {
auto& rec = R[i];
idxs[rec.lx][rec.ly].emplace_back(i);
}
for (int i = 0; i < m; i++) {
auto& rec = L[i];
for (int x = rec.rx - 4; x < rec.rx; x++) {
if (x < 0) continue;
if ((rec.rx - x) * 2 < rec.rx - rec.lx) continue;
for (int y = rec.ry - 4; y < rec.ry; y++) {
if (y < 0) continue;
if ((rec.rx - x) * (rec.ry - y) * 2 < rec.area()) continue;
for (int& j : idxs[x][y]) {
if (intersection(rec, R[j]) * 2 >= rec.area() and not s.count({i, j})) {
BM.add_edge(i, j);
s.emplace(i, j);
}
}
}
}
}
for (int i = 0; i < n; i++) {
auto& rec = R[i];
idxs[rec.lx][rec.ly].pop_back();
}
rotate();
}
int ans = BM.solve();
cout << ans << '\n';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int t;
cin >> t;
for (; t--;) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 16ms
memory: 98012kb
input:
3 2 2 1 1 3 3 3 3 5 5 2 2 4 4 4 4 6 6 2 3 1 1 3 3 3 3 5 5 1 3 3 5 2 1 4 5 3 1 5 3 3 3 1 1 2 2 2 2 3 3 3 3 4 4 1 1 3 3 2 2 4 4 3 3 5 5
output:
0 1 3
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 24ms
memory: 98084kb
input:
3 2 2 1 1 3 3 3 3 5 5 2 2 4 4 4 4 6 6 2 3 1 1 3 3 3 3 5 5 1 3 3 5 2 1 4 5 3 1 5 3 3 3 1 1 2 2 2 2 3 3 3 3 4 4 1 1 3 3 2 2 4 4 3 3 5 5
output:
0 1 3
result:
ok 3 lines
Test #3:
score: 0
Accepted
time: 513ms
memory: 146116kb
input:
5 50000 50000 0 0 4 4 4 0 8 4 8 0 12 4 12 0 16 4 16 0 20 4 20 0 24 4 24 0 28 4 28 0 32 4 32 0 36 4 36 0 40 4 40 0 44 4 44 0 48 4 48 0 52 4 52 0 56 4 56 0 60 4 60 0 64 4 64 0 68 4 68 0 72 4 72 0 76 4 76 0 80 4 80 0 84 4 84 0 88 4 88 0 92 4 92 0 96 4 96 0 100 4 100 0 104 4 104 0 108 4 108 0 112 4 112 ...
output:
50000 50000 0 50000 3150
result:
ok 5 lines
Test #4:
score: 0
Accepted
time: 675ms
memory: 146280kb
input:
5 50000 50000 0 0 1 1 1 0 2 1 2 0 3 1 3 0 4 1 4 0 5 1 5 0 6 1 6 0 7 1 7 0 8 1 8 0 9 1 9 0 10 1 10 0 11 1 11 0 12 1 12 0 13 1 13 0 14 1 14 0 15 1 15 0 16 1 16 0 17 1 17 0 18 1 18 0 19 1 19 0 20 1 20 0 21 1 21 0 22 1 22 0 23 1 23 0 24 1 24 0 25 1 25 0 26 1 26 0 27 1 27 0 28 1 28 0 29 1 29 0 30 1 30 0 ...
output:
50000 25050 12500 16000 8000
result:
ok 5 lines
Test #5:
score: 0
Accepted
time: 422ms
memory: 119532kb
input:
5 50000 50000 0 0 2 4 4 0 7 1 8 0 10 1 12 0 15 3 16 0 19 1 20 0 22 2 24 0 26 4 28 0 30 4 32 0 36 3 36 0 40 1 40 0 44 1 44 0 47 2 48 0 49 3 52 0 54 1 56 0 59 4 60 0 64 3 64 0 68 3 68 0 70 1 72 0 76 4 76 0 80 3 80 0 84 4 84 0 87 2 88 0 90 1 92 0 94 4 96 0 98 1 100 0 104 1 104 0 107 2 108 0 110 4 112 0...
output:
10594 10779 10618 10381 10779
result:
ok 5 lines
Test #6:
score: 0
Accepted
time: 3008ms
memory: 140804kb
input:
5 50000 50000 0 0 4 4 1 0 5 4 2 0 6 4 3 0 7 4 4 0 8 4 5 0 9 4 6 0 10 4 7 0 11 4 8 0 12 4 9 0 13 4 10 0 14 4 11 0 15 4 12 0 16 4 13 0 17 4 14 0 18 4 15 0 19 4 16 0 20 4 17 0 21 4 18 0 22 4 19 0 23 4 20 0 24 4 21 0 25 4 22 0 26 4 23 0 27 4 24 0 28 4 25 0 29 4 26 0 30 4 27 0 31 4 28 0 32 4 29 0 33 4 30...
output:
50000 50000 50000 50000 49600
result:
ok 5 lines
Test #7:
score: 0
Accepted
time: 11ms
memory: 98036kb
input:
1 4 4 1 1 3 3 2 1 4 3 1 2 3 4 2 2 4 4 2 1 4 3 3 2 5 4 1 2 3 4 2 3 4 5
output:
3
result:
ok single line: '3'