QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#500767#6407. Classical A+B Problemucup-team2307#TL 0ms0kbC++173.7kb2024-08-01 19:48:542024-08-01 19:48:57

Judging History

你现在查看的是最新测评结果

  • [2024-08-01 19:48:57]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-08-01 19:48:54]
  • 提交

answer

#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
map<array<int, 2>, int> id;
struct Points {
	int n;
	vector<set<array<int, 2>>> X_to_pts, Y_to_pts;
	priority_queue<array<int, 2>> Xq, Yq;
	Points(vector<array<int, 2>> pts) : n(pts.size()), X_to_pts(n), Y_to_pts(n) {
		vector<int> X, Y;
		for(auto &[x, y] : pts) {
			X.push_back(x);
			Y.push_back(y);
		}
		sort(all(X)); sort(all(Y));
		for(auto &[x, y] : pts) {
			x = lower_bound(all(X), x) - X.begin();
			y = lower_bound(all(Y), y) - Y.begin();
			X_to_pts[x].insert({x, y});
			Y_to_pts[y].insert({x, y});
		}
		for(int i = 0; i < n; i++) {
			Xq.push({X_to_pts[i].size(), i});
			Yq.push({Y_to_pts[i].size(), i});
		}
		
		
		id.clear();
		for(int i = 0; i < pts.size(); i++) 
		    id[pts[i]] = i;
	}

	int getX() {
		while(Xq.size()) {
			auto [cnt, x] = Xq.top();
			if(X_to_pts[x].size() == cnt) {
				return x;
			} Xq.pop();
		}
		return -1;
	}
	int getY() {
		while(Yq.size()) {
			auto [cnt, y] = Yq.top();
			if(Y_to_pts[y].size() == cnt) {
				return y;
			} Yq.pop();
		}
		return -1;
	}

	void delete_point(int x, int y) {
		X_to_pts[x].erase({x, y});
		Y_to_pts[y].erase({x, y});
		Xq.push({X_to_pts[x].size(), x});
		Yq.push({Y_to_pts[y].size(), y});
	}
};
vector<array<int, 4>> solve(int n, vector<array<int, 2>> pts) {
	Points p(pts);
	vector<array<int, 4>> ans;
	auto finish = [&](auto chuj) {
	    int s = chuj.size();
	    for(int i = 0; i < s - i; i++) {
	        ans.push_back({chuj[i][0], chuj[i][1],
	                chuj[(i + s/2)%s][0], chuj[(i+s/2)%s][1]});
	    }
	};
	for(int iters = 0; iters < n; iters++) {
		int x = p.getX();
		int y = p.getY();
	    vector<array<int, 2>> chuj;
		if(p.X_to_pts[x].size() == 2*(n-iters)) {//everything has this x
		    vector<array<int, 2>> chuj;
		    chuj.insert(chuj.end(), all(p.X_to_pts[x]));
		} else if(p.Y_to_pts[y].size() == 2*(n-iters)) {//everything has this y
		    chuj.insert(chuj.end(), all(p.Y_to_pts[y]));
		} else if(p.X_to_pts[x].size() == 1) {//all x distinct
		    while(chuj.size() < 2*(n - iters)) {
		        int y = p.getY();
		        vector<array<int, 2>> tmp(all(p.Y_to_pts[y]));
		        chuj.insert(chuj.end(), all(tmp));
		        for(auto [u, v] : tmp) p.delete_point(u, v);
		    }		    
		} else if(p.Y_to_pts[y].size() == 1) {//all y distinct
		    while(chuj.size() < 2*(n - iters)) {
		        int x = p.getX();
		        vector<array<int, 2>> tmp(all(p.X_to_pts[x]));
		        chuj.insert(chuj.end(), all(tmp));
		        for(auto [u, v] : tmp) p.delete_point(u, v);
		    }		    
		}
		if(chuj.size()) {
		    finish(chuj);
		    break;
		}
		auto A = *p.X_to_pts[x].begin();
		if(A == array<int, 2>{x, y}) A = *p.X_to_pts[x].rbegin();
		
		
		auto B = *p.Y_to_pts[y].begin();
		if(B == array<int, 2>{x, y}) B = *p.Y_to_pts[y].rbegin();
		
		ans.push_back({A[0], A[1], B[0], B[1]});
	    p.delete_point(A[0], A[1]);
	    p.delete_point(B[0], B[1]);
	}
	return ans;
}
 main() {
	cin.tie(0)->sync_with_stdio(0);
	int T;
	cin >> T;
	while(T--) {
	    int n;
	    cin >> n;
	    vector<array<int, 2>> pts(2*n);
	    int I = 0;
	    for(auto &[x, y] : pts) {
	       // cin >> x >> y;
	       x = 1, y = I;
	       I++;
	       // id[{x, y}] = I++;
	    }
	    auto ans = solve(n, pts);
	    int bad = 0;
	    for(auto [x, y, u, v] : ans) {
	        bad += x==u || y==v;
	    }
	    cout<< bad << '\n';
	    for(auto [x, y, u, v] : ans) {
	        cout << 1+id[{x, y}] << " " << 1+id[{u, v}] << '\n';
	       // cout << x << " " << y << " " << u << " " << v << endl;
	    }
	}
}/*
2 piles sum <= 2
0 0
0 1
0 2
1 1
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

6
2
786
1332
89110
2333333
10000000000000000000000000001

output:

2
1 4
2 3
786
1 1572
2 1571
3 1570
4 1569
5 1568
6 1567
7 1566
8 1565
9 1564
10 1563
11 1562
12 1561
13 1560
14 1559
15 1558
16 1557
17 1556
18 1555
19 1554
20 1553
21 1552
22 1551
23 1550
24 1549
25 1548
26 1547
27 1546
28 1545
29 1544
30 1543
31 1542
32 1541
33 1540
34 1539
35 1538
36 1537
37 1536...

result: