比赛 20120704 评测结果 AAAAAAAAAA
题目名称 椰子 最终得分 100
用户昵称 CC 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2012-07-04 11:45:03
显示代码纯文本
#include <cstdio>
#include <algorithm>
#include <cstring>
#define E 1005
struct coor {int x,y;}a[1005];
int T,n,m;
bool d[2050][2050];
int c[2050][2050],p[1005],w[1005];
void work(int x,int y,int u) {
	int o,tmp;
	if (d[x - 1][y - 1] && d[x + 1][y - 1]) {
		a[u].x = x;a[u].y = y;
		d[x][y] = 1;
		c[x][y] = u;
	}
	else if (!d[x - 1][y - 1] && !d[x + 1][y - 1]) {
		if (w[u] < w[c[x][y - 1]]) {
			x--;
			y--;
			work(x,y,u);
		} else {
			a[u].x = x;a[u].y = y - 1;
			tmp = c[x][y - 1];
			c[x][y - 1] = u;
			x++;
			y--;
			work(x,y,tmp);
		}
	} 
	else {
		if (!d[x - 1][y - 1]) o = -1;else o = 1;
		if (w[u] < w[c[x][y - 1]]) {
			x += o;
			y--;
			work(x,y,u);
		} else {
			a[u].x = x;a[u].y = y - 1;
			tmp = c[x][y - 1];
			c[x][y - 1] = u;
			x += o;
			y--;
			work(x,y,tmp);
		}
	}
}
int main() {
	freopen("coconuts.in","r",stdin);
	freopen("coconuts.out","w",stdout);
	scanf("%d", &T);
	while (T--) {
		memset(d,0,sizeof(d));
		memset(c,0,sizeof(c));
		memset(a,0,sizeof(a));
		for (int i = 0;i <= 2040;i++) d[i][0] = 1;
		scanf("%d", &n);
		for (int i = 1;i <= n;i++) {
			scanf("%d%d", &p[i], &w[i]);
			for (int j = 1;j <= 1000;j++) 
				if (!d[p[i] + E][j]) {
					work(p[i] + E,j,i);
					break;
				}
		}
		for (int i = 1;i <= n;i++) printf("%d %d\n", a[i].y,a[i].x - E);
		printf("\n");
	}
	return 0;
}