QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#167513 | #6414. Classical Maximization Problem | realIyxiang# | WA | 101ms | 28732kb | C++14 | 2.9kb | 2023-09-07 15:30:51 | 2023-09-07 15:30:52 |
Judging History
answer
#include <bits/stdc++.h>
#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)
using namespace std;
using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;
template < typename T > T read() {
T x = 0; bool f = 0; char ch = getchar();
while(!isdigit(ch)) f |= ch == '-', ch = getchar();
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }
const int N = 2e5 + 10;
int n, x[N], y[N];
set < int > pt[N << 1];
set < pii > all;
pii hav[N];
bool vis[N], pvis[N];
mt19937 rnd(19491001);
struct valpot {
int t[N], tot, pt[N];
void reset() { tot = 0; }
void ins(int x) { t[++tot] = x; }
void gener() { rep(i, 1, tot) pt[i] = i; shuffle(pt + 1, pt + tot + 1, rnd); }
void build() { sort(t + 1, t + tot + 1), tot = unique(t + 1, t + tot + 1) - t - 1; gener(); }
int operator [](int x) { return pt[lower_bound(t + 1, t + tot + 1, x) - t]; }
} a, b;
void solve() {
a.reset(), b.reset();
n = in * 2; rep(i, 1, n) x[i] = in, y[i] = in, vis[i] = pvis[i] = 0, a.ins(x[i]), b.ins(y[i]);
a.build(), b.build();
rep(i, 1, n) x[i] = a[x[i]], y[i] = b[y[i]];
int at = a.tot, bt = b.tot;
rep(i, 1, at + bt) set < int >().swap(pt[i]);
set < pii >().swap(all);
rep(i, 1, n) hav[i] = { x[i], y[i] + at }, pt[x[i]].ep(i), pt[y[i] + at].ep(i);
rep(i, 1, at + bt) all.ep(pt[i].size(), i);
auto del = [&](int id) {
if(!vis[id]) vis[id] = true; else return;
pt[x[id]].erase(id), pt[y[id] + at].erase(id);
all.erase({ pt[x[id]].size() + 1, x[id] });
all.erase({ pt[y[id] + at].size() + 1, y[id] + at });
if(pt[x[id]].size()) all.ep(pt[x[id]].size(), x[id]);
if(pt[y[id] + at].size()) all.ep(pt[y[id] + at].size(), y[id] + at);
};
veg ans; int cnt = 0;
auto pairit = [&](int a, int b) {
pvis[a] = pvis[b] = 1; ans.eb(a, b); cnt += x[a] == x[b] || y[a] == y[b];
del(a), del(b);
};
while(all.size()) {
auto x = *all.begin(); assert(x.fi >= 1);
if(x.fi == 1) {
int id = *pt[x.se].begin();
int tid = hav[id].fi ^ hav[id].se ^ x.se;
del(id); if(pt[tid].size() == 0) continue;
int nid = *pt[tid].begin();
pairit(id, nid);
} else {
int id = *pt[x.se].begin(), nid = *pt[x.se].rbegin();
pairit(id, nid);
}
}
int lst = 0;
rep(i, 1, n)
if(!pvis[i]) {
if(!lst) lst = i;
else pairit(lst, i), lst = 0;
}
printf("%d\n", cnt);
for(auto v : ans) printf("%d %d\n", v.fi, v.se);
}
int main() {
#ifdef YJR_2333_TEST
freopen("1.in", "r", stdin);
#endif
for(int T = in; T; T--) solve(); return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 28332kb
input:
3 2 0 0 0 1 1 0 1 1 2 0 0 0 1 0 2 0 3 2 0 0 1 1 2 2 3 3
output:
2 1 2 4 3 2 3 1 4 2 0 1 2 3 4
result:
ok ok (3 test cases)
Test #2:
score: 0
Accepted
time: 95ms
memory: 26388kb
input:
10000 2 -107276936 -310501829 419434212 585811870 -65754386 -491212232 381152038 897148193 3 -474045168 493506332 299114415 540203303 165808153 983551 -506936261 -694189769 766718170 -725540031 975267148 -593051087 1 -818952276 -762387923 584023914 -612401389 6 -77701228 -266484128 659434465 6322062...
output:
0 1 2 3 4 0 1 2 3 4 5 6 0 1 2 0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 0 1 2 3 4...
result:
ok ok (10000 test cases)
Test #3:
score: 0
Accepted
time: 101ms
memory: 26420kb
input:
10000 1 999855386 999580905 999342928 999615227 21 999601032 999015398 999155628 999176944 999309856 999524434 999121011 999509537 999323572 999685730 999272272 999769606 999450559 999390758 999632027 999178534 999024993 999463838 999784856 999374197 999980525 999366771 999241260 999516879 999599548...
output:
0 1 2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 0 1 2 3 4 5 6 7...
result:
ok ok (10000 test cases)
Test #4:
score: 0
Accepted
time: 89ms
memory: 28660kb
input:
10000 5 999984799 999981445 999958394 999984217 999994978 999981258 999955539 999938710 999936554 999963561 999907222 999907508 999938166 999941959 999910567 999986887 999901446 999961092 999994730 999963038 5 999916115 999962400 999948250 999940355 999954204 999920844 999928148 999990369 999978118 ...
output:
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1...
result:
ok ok (10000 test cases)
Test #5:
score: 0
Accepted
time: 87ms
memory: 28356kb
input:
10000 1 999990146 999993828 999995909 999996353 56 999999851 999991179 999997250 999997987 999990590 999997316 999997350 999996856 999997034 999996236 999999396 999996897 999991180 999993309 999991265 999995185 999993952 999994054 999990210 999994471 999993201 999995893 999997170 999998971 999998201...
output:
0 1 2 1 111 76 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 ...
result:
ok ok (10000 test cases)
Test #6:
score: -100
Wrong Answer
time: 99ms
memory: 28732kb
input:
10000 5 999999432 999999813 999999271 999999233 999999043 999999606 999999523 999999406 999999564 999999274 999999641 999999102 999999903 999999858 999999058 999999098 999999974 999999119 999999643 999999620 5 999999370 999999738 999999181 999999907 999999163 999999783 999999393 999999086 999999661 ...
output:
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 0 1 2 3 4 5 6 7 8 9 10 1 9 10 1 2 3 4 5 6 7 8 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 1 12 13 1 2 3 4 5 6 7 8 9 10 11 14 15 16 0 1 2 3 4 0 1 2 3 4 1 10 44 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20 21 22...
result:
wrong answer worse than author's solution: expected 7, found 6 (test case 2335)