QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#472136 | #6414. Classical Maximization Problem | UESTC_DebugSimulator# | RE | 1ms | 5724kb | C++17 | 2.8kb | 2024-07-11 14:41:23 | 2024-07-11 14:41:23 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(i) (i&-i)
#define rand() (myrand())
using namespace std;
mt19937 myrand(time(0));
const int maxn = 5e5 + 5;
int n, _, cx[maxn], cy[maxn], vis[maxn];
struct node{
int x, y;
}a[maxn];
void solve() {
cin >> n;
n <<= 1;
vector<int>px, py;
vector<pair<int, int> >ans;
priority_queue<pair<int, int> >qx, qy;
map<pair<int, int>, int>id;
int num = 0;
for (int i = 1 ; i <= n ; ++i) {
int x, y;
cin >> x >> y;
a[i].x = x, a[i].y = y;
id[{x, y}] = i;
vis[i] = 0;
if (!cx[x]) px.push_back(x);
if (!cy[y]) py.push_back(y);
cx[x]++, cy[y]++;
}
for (auto x : px) qx.push({cx[x], x});
for (auto y : py) qy.push({cy[y], y});
while(!qx.empty() && !qy.empty()) {
pair<int, int>t1, t2, t, p1, p2;
int maxh = -1, jud = 0;
if (qx.size() >= 2 && qy.size() >= 1) {
t1 = qx.top();qx.pop();
t2 = qx.top();qx.pop();
t = qy.top();qy.pop();
if (t.first - 2 >= 0) {
int last = maxh;
maxh = max(maxh, min({t1.first - 1, t2.first - 1, t.first - 2}));
if (maxh > last) {
p1.second = p2.second = t.second;
p1.first = t1.second;
p2.first = t2.second;
}
jud = 1;
}
qx.push(t1);
qx.push(t2);
qy.push(t);
}
if (qx.size() >= 1 && qy.size() >= 2) {
t1 = qy.top();qy.pop();
t2 = qy.top();qy.pop();
t = qx.top();qx.pop();
if (t.first - 2 >= 0) {
int last = maxh;
maxh = max(maxh, min({t1.first - 1, t2.first - 1, t.first - 2}));
if (maxh > last) {
p1.first = p2.first = t.second;
p1.second = t1.second;
p2.second = t2.second;
}
jud = 1;
}
qy.push(t1);
qy.push(t2);
qx.push(t);
}
if (!jud) break;
num++;
if (p1.second == p2.second) {
t1 = qx.top();qx.pop();
t2 = qx.top();qx.pop();
t = qy.top();qy.pop();
t1.first--;
t2.first--;
t.first -= 2;
if (t1.first > 0) qx.push(t1);
if (t2.first > 0) qx.push(t2);
if (t.first > 0) qy.push(t);
}
else {
t1 = qy.top();qy.pop();
t2 = qy.top();qy.pop();
t = qx.top();qx.pop();
t1.first--;
t2.first--;
t.first -= 2;
if (t1.first > 0) qy.push(t1);
if (t2.first > 0) qy.push(t2);
if (t.first > 0) qx.push(t);
}
ans.push_back({id[p1], id[p2]});
vis[id[p1]] = vis[id[p2]] = 1;
}
vector<int>res;
for (int i = 1 ; i <= n ; ++i) {
if (!vis[i]) res.push_back(i);
}
for (int i = 0 ; i < res.size() ; i += 2) ans.push_back({res[i], res[i + 1]});
cout << num << '\n';
for (auto [x, y] : ans) cout << x << ' ' << y << '\n';
for (int i = 1 ; i <= n ; ++i) cx[a[i].x] = cy[a[i].y] = 0;
}
signed main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> _;
while(_--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5724kb
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 4 2 3 1 2 4 3 2 1 0 1 2 3 4
result:
ok ok (3 test cases)
Test #2:
score: -100
Runtime Error
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...