UVA 11134 Fabled Rooks

在给定的一个棋盘中按照要求放置n个rook,每个rook给定一个矩形范围,也就是说这个rook只能放在这个矩形范围之内。

同时任何两个rook不同行和列。

显然行的选择不影响列的选择,反之亦然。

所以我们可以分开处理。

比如只看列:每个rook就有个一维范围,然后我们根据第二键值排序,然后吧前面的尽量排在前面。

这样的谈心显然是最优的。某个rook不能放置的话就标记下。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2016
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
#define lson rt << 1
#define rson rt << 1 | 1
#define bug cout << "BUG HERE\n"
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int nCase = 0;
/**********************Point*****************************/
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B){
	return Vector(A.x + B.x,A.y + B.y);
}
Vector operator - (Vector A,Vector B){//向量减法
	return Vector(A.x - B.x,A.y - B.y);
}
Vector operator * (Vector A,double p){//向量数乘
	return Vector(A.x * p,A.y * p);
}
Vector operator / (Vector A,double p){//向量除实数
	return Vector(A.x / p,A.y / p);
}
int dcmp(double x){//精度正负、0的判断
	if (fabs(x) < eps) return 0;
	return x < 0?-1:1;
}
bool operator < (const Point& A,const Point& B){//小于符号的重载
	return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A,const Point& B){//点重的判断
	return dcmp(A.x - B.x) == 0&& dcmp(A.y - B.y) == 0;
}
double Dot(Vector A,Vector B){//向量的点乘
	return A.x * B.x + A.y * B.y;
}
double Length(Vector A){//向量的模
	return sqrt(Dot(A,A));
}
double Angle(Vector A,Vector B){//向量的夹角
	return acos(Dot(A,B) / Length(A) / Length(B));
}
double Cross(Vector A,Vector B){//向量的叉积
	return A.x * B.y - A.y * B.x;
}
double Area2(Point A,Point B,Point C){//三角形面积
	return Cross(B - A,C - A);
}
Vector Rotate(Vector A,double rad){//向量的旋转
	return Vector(A.x * cos(rad) - A.y * sin(rad),A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A){//法向量
	int L = Length(A);
	return Vector(-A.y / L,A.x / L);
}
double DistanceToLine(Point p,Point A,Point B){//p到直线AB的距离
	Vector v1 = B - A,v2 = p - A;
	return fabs(Cross(v1,v2)) / Length(v1);
}
double DistanceToSegment(Point p,Point A,Point B){//p到线段AB的距离
	if (A == B) return Length(p - A);
	Vector v1 = B - A, v2 = p - A,v3 = p - B;
	if (dcmp(Dot(v1,v2) < 0)) return Length(v2);
	else if (dcmp(Dot(v1,v3)) > 0) return Length(v3);
	else return DistanceToLine(p,A,B);
}
bool SegmentProperIntersection(Point A1,Point A2,Point B1,Point B2){//线段相交
	double c1 = Cross(A2 - A1,B1 - A1),c2 = Cross(A2 - A1,B2 - A1);
	double c3 = Cross(B2 - B1,A1 - B1),c4 = Cross(B2 - B1,A2 - B1);
	return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
const int N = 5555;
struct Item {
	int l, r, num;
}x[N],y[N];
int A[N],B[N];
int HASH[N];
bool cmp(Item& A,Item& B)
{
	if (A.r != B.r) return A.r < B.r;
	return A.l < B.l;
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int n;
	while(~scanf("%d",&n) && n) {
		for(int i = 0;i < n;++i)
		{
			scanf("%d%d%d%d",&x[i].l,&y[i].l,&x[i].r,&y[i].r);
			x[i].num = i;
			y[i].num = i;
		}
		sort(x,x+n,cmp);
		sort(y,y+n,cmp);
		memset(HASH, 0,sizeof HASH);
		bool No = true;
		for (int i = 0;i < n;++i)
		{
			bool flag = false;
			for (int j = x[i].l;j <= x[i].r;++j)
			{
				if (!HASH[j])
				{
					A[x[i].num] = j;
					HASH[j] = 1;
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				No = false;
				break;
			}
		}
		if (!No)
		{
			puts("IMPOSSIBLE");
			continue;
		}
		memset(HASH, 0,sizeof HASH);
		for (int i = 0;i < n;++i)
		{
			bool flag = false;
			for (int j = y[i].l;j <= y[i].r;++j)
			{
				if (!HASH[j])
				{
					HASH[j] = 1;
					B[y[i].num] = j;
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				No = false;
				break;
			}
		}
		if (!No)
		{
			puts("IMPOSSIBLE");
			continue;
		}
		for (int i = 0;i < n;++i)
			printf("%d %d\n", A[i],B[i]);
	}
	return 0;
}


你可能感兴趣的:(谈心)