UVa 11134 Fabled Rooks
来源:互联网 发布:lol国服数据 编辑:程序博客网 时间:2024/06/12 18:10
题目链接:https://vjudge.net/problem/UVA-11134
本题只需考虑行和列,与对角线无关,故可划分为两个一维问题。需要注意的是须先按每个小区间的右端点排序,再从左往右扫一次即可,这样可以最大程度上防止后面的区间无点可选。第一次忽略了这一点。。。代码如下:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef struct node
{
int d,x1,y1,x2,y2;
int p,q;
node(int dd=0,int xx1=0,int yy1=0,int xx2=0,int yy2=0):d(dd),x1(xx1),y1(yy1),x2(xx2),y2(yy2){}
}node;
bool cmp1(const node a,const node b)
{
if(a.x2<b.x2) return true;
else if(a.x2==b.x2) return a.x1<b.x1;
else return false;
}
bool cmp2(const node a,const node b)
{
if(a.y2<=b.y2) return true;
else if(a.y2==b.y2) return a.y1<=b.y1;
else return false;
}
bool cmp3(const node a,const node b)
{
return a.d<b.d;
}
int main()
{
int t,d,x1,x2,y1,y2;
while(cin>>t&&t)
{
int sum=0;
bool r1[5005]={false},r2[5005]={false};
vector<node> v;
for(int i=0;i<t;++i)
{
cin>>x1>>y1>>x2>>y2;
v.push_back(node(i,x1,y1,x2,y2));
}
sort(v.begin(),v.end(),cmp1);
for(int i=0;i<t;++i)
{
for(int j=v[i].x1;j<=v[i].x2;++j)
{
if(!r1[j])
{
v[i].p=j;
r1[j]=true;
++sum;
break;
}
}
}
if(sum<t)
{
cout<<"IMPOSSIBLE"<<endl;
continue;
}
sum=0;
sort(v.begin(),v.end(),cmp2);
for(int i=0;i<t;++i)
{
for(int j=v[i].y1;j<=v[i].y2;++j)
{
if(!r2[j])
{
v[i].q=j;
r2[j]=true;
++sum;
break;
}
}
}
if(sum<t)
{
cout<<"IMPOSSIBLE"<<endl;
continue;
}
sort(v.begin(),v.end(),cmp3);
for(int i=0;i<t;++i) cout<<v[i].p<<" "<<v[i].q<<endl;
}
return 0;
}
#include<algorithm>
#include<vector>
using namespace std;
typedef struct node
{
int d,x1,y1,x2,y2;
int p,q;
node(int dd=0,int xx1=0,int yy1=0,int xx2=0,int yy2=0):d(dd),x1(xx1),y1(yy1),x2(xx2),y2(yy2){}
}node;
bool cmp1(const node a,const node b)
{
if(a.x2<b.x2) return true;
else if(a.x2==b.x2) return a.x1<b.x1;
else return false;
}
bool cmp2(const node a,const node b)
{
if(a.y2<=b.y2) return true;
else if(a.y2==b.y2) return a.y1<=b.y1;
else return false;
}
bool cmp3(const node a,const node b)
{
return a.d<b.d;
}
int main()
{
int t,d,x1,x2,y1,y2;
while(cin>>t&&t)
{
int sum=0;
bool r1[5005]={false},r2[5005]={false};
vector<node> v;
for(int i=0;i<t;++i)
{
cin>>x1>>y1>>x2>>y2;
v.push_back(node(i,x1,y1,x2,y2));
}
sort(v.begin(),v.end(),cmp1);
for(int i=0;i<t;++i)
{
for(int j=v[i].x1;j<=v[i].x2;++j)
{
if(!r1[j])
{
v[i].p=j;
r1[j]=true;
++sum;
break;
}
}
}
if(sum<t)
{
cout<<"IMPOSSIBLE"<<endl;
continue;
}
sum=0;
sort(v.begin(),v.end(),cmp2);
for(int i=0;i<t;++i)
{
for(int j=v[i].y1;j<=v[i].y2;++j)
{
if(!r2[j])
{
v[i].q=j;
r2[j]=true;
++sum;
break;
}
}
}
if(sum<t)
{
cout<<"IMPOSSIBLE"<<endl;
continue;
}
sort(v.begin(),v.end(),cmp3);
for(int i=0;i<t;++i) cout<<v[i].p<<" "<<v[i].q<<endl;
}
return 0;
}
0 0
- uva 11134 Fabled Rooks
- Uva-11134-Fabled Rooks
- uva 11134 Fabled Rooks
- uva 11134 - Fabled Rooks
- UVA 11134 - Fabled Rooks
- UVa:11134 Fabled Rooks
- uva 11134 Fabled Rooks
- UVa 11134 - Fabled Rooks
- UVa 11134 Fabled Rooks
- UVA 11134 - Fabled Rooks
- Uva - 11134 - Fabled Rooks
- UVA - 11134 Fabled Rooks
- UVA 11134 Fabled Rooks
- UVA - 11134 Fabled Rooks
- UVA 11134 Fabled Rooks
- UVA 11134 Fabled Rooks
- UVA 11134Fabled Rooks
- Uva 11134 Fabled Rooks
- Iptables
- a的b次方的前N位数和后N位数
- ahalei_10
- 算法面试题解析:仅使用指定运算符,实现问号表达式的功能
- ajax入门学习(一)
- UVa 11134 Fabled Rooks
- 素数环
- grep 命令
- A tutorial on Principal Components Analysis - 主成分分析(PCA)教程
- 集合概述
- org.hibernate.exception.ConstraintViolationException: could not execute statment 异常解决
- 9
- Linux 文件类型与扩展名
- 根据第xx天推算日期