[Codeforces 1237C] Balanced Removals

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_33831360/article/details/102626378

 n^2做法,枚举找每个点的曼哈顿距离距离最近点

int dis(Data a,Data b) {
	return abs(a.x-b.x)+abs(a.y-b.y)+abs(a.z-b.z);
}


    cin >> n;
    for (int i = 1; i <= n; i++) g[i].init();
    for (int i = 1; i <= n; i++) {
    	if (v[i]) continue;
    	int ds = (int)1e9,tp = -1;
    	for (int j = i+1; j <= n; j++) 
    	 if(!v[j] && dis(g[j],g[i])<ds)
    	   ds = dis(g[i],g[tp=j]);
    	cout << i << " " << tp << "\n";
    	v[tp] = 1;
	}

Harder的做法

以x,y,z为第1,2,3关键字排序

对于x,y相同,z相邻的两个点肯定符合

上述操作后,没有x,y都相同的点了,可以把z压扁成x,y平面。

x相同,y相邻的点肯定可以

之后没有x相同的点了,把x轴压扁成y直线坐标

那y相邻的两个点肯定可以

#include <cstdio>
#include <vector>
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long LL;
#define int LL
void read(int &x) {
    char c;bool flag = 0;
    while((c=getchar())<'0'||c>'9') flag |= (c=='-');
    x=c-'0';while((c=getchar())>='0'&&c<='9') x = (x<<3)+(x<<1)+c-'0';
    flag?x=-x:x;
}

#define N 100020
int n,m,k;
vector<int> u[N],d[N],l[N],r[N];
int uu[N],dd[N],ll[N],rr[N]; 
 
signed main() {
    read(n); read(m); read(k);
    for (int i = 1; i <= n; i++) {
    //	l[i] = 0; r[i] = m+1;
        l[i].push_back(0);
        r[i].push_back(m+1);
	}
	for (int i = 1; i <= m; i++) {
		//u[i] = 0; d[i] = n+1;
		u[i].push_back(0);
		d[i].push_back(n+1);
	}
	for (int i = 1; i <= k; i++) {
		int x,y; read(x); read(y);
		l[x].push_back(y);
		r[x].push_back(y);
		u[y].push_back(x);
		d[y].push_back(x);
	} 
	for (int i = 1; i <= n; i++) {
		sort(r[i].begin(),r[i].end());
	    sort(l[i].begin(),l[i].end(),greater<int>());
	}
	for (int i = 1; i<= m; i++) {
	    sort(d[i].begin(),d[i].end());
	    sort(u[i].begin(),u[i].end(),greater<int>());
	}
	int L = 0,R = m+1,U = 0,D = n+1;
	int dir = 1,dx = 1,dy = 1;
	LL sum = 1;
	bool turned = 0;
	while(1) {
		int tpx = dx,tpy = dy;
		if (dir == 1) {
		   while (r[dx][rr[dx]] < dy) rr[dx]++;	
		   dy = min(r[dx][rr[dx]]-1,R-1);
		   U = dx;
		}
		if (dir == 2) {
		   while(d[dy][dd[dy]] < dx) dd[dy]++;
		   dx = min(d[dy][dd[dy]]-1,D-1);
		   R = dy; 
		}
		if (dir == 3) {
		   while(l[dx][ll[dx]] > dy) ll[dx]++;
		   dy = max(l[dx][ll[dx]]+1,L+1);
		   D = dx;
		}
		if (dir == 4) {
		   while(u[dy][uu[dy]] > dx) uu[dy]++;
		   dx = max(u[dy][uu[dy]]+1,U+1);
		   L = dy;
		}
		dir++;
		if (dir == 5) dir = 1;
		if (dx==tpx && dy==tpy) {
		   if (dx==1 && dy==1 && !turned) {
		   	 turned = 1;
		   	 continue;
		   } 
		   break;
		}
		sum += abs(dx-tpx)+abs(dy-tpy); 
	} 
	puts(sum+k==1LL*n*m?"Yes":"No");
    return 0;
}

This is a harder version of the problem. In this version, n≤50000n≤50000.

There are nn distinct points in three-dimensional space numbered from 11 to nn. The ii-th point has coordinates (xi,yi,zi)(xi,yi,zi). The number of points nn is even.

You'd like to remove all nn points using a sequence of n2n2 snaps. In one snap, you can remove any two points aa and bb that have not been removed yet and form a perfectly balanced pair. A pair of points aa and bb is perfectly balanced if no other point cc (that has not been removed yet) lies within the axis-aligned minimum bounding box of points aa and bb.

Formally, point cc lies within the axis-aligned minimum bounding box of points aa and bb if and only if min(xa,xb)≤xc≤max(xa,xb)min(xa,xb)≤xc≤max(xa,xb), min(ya,yb)≤yc≤max(ya,yb)min(ya,yb)≤yc≤max(ya,yb), and min(za,zb)≤zc≤max(za,zb)min(za,zb)≤zc≤max(za,zb). Note that the bounding box might be degenerate.

Find a way to remove all points in n2n2 snaps.

Input

The first line contains a single integer nn (2≤n≤500002≤n≤50000; nn is even), denoting the number of points.

Each of the next nn lines contains three integers xixi, yiyi, zizi (−108≤xi,yi,zi≤108−108≤xi,yi,zi≤108), denoting the coordinates of the ii-th point.

No two points coincide.

Output

Output n2n2 pairs of integers ai,biai,bi (1≤ai,bi≤n1≤ai,bi≤n), denoting the indices of points removed on snap ii. Every integer between 11 and nn, inclusive, must appear in your output exactly once.

We can show that it is always possible to remove all points. If there are many solutions, output any of them.

Examples

input

Copy

6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0

output

Copy

3 6
5 1
2 4

input

Copy

8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3

output

Copy

4 5
1 6
2 7
3 8

Note

In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z=0z=0 plane). Note that order of removing matters: for example, points 55 and 11 don't form a perfectly balanced pair initially, but they do after point 33 is removed.

猜你喜欢

转载自blog.csdn.net/qq_33831360/article/details/102626378