题目:
http://poj.org/problem?id=1083
题解:
按桌子移动的起点一级排序,终点2级排序,输入的数据不一定是从小到大,需处理。
贪心时,奇数号的房间被占用时,下面的那间房也不能移动桌子。
代码:
#include<cstdio>
#include<stdlib.h>
#include<cstring>
struct in
{
int L,R;
}a[201];
int temp[201];
int cmp(const void *a,const void *b)
{
in *c=(in*)a;
in *d=(in*)b;
if(c->L==d->L)
{
return c->R-d->R;
}
else
{
return c->L-d->L;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int N;
scanf("%d",&N);
int i;
for(i=0;i<N;i++)
{
scanf("%d%d",&a[i].L,&a[i].R);
if(a[i].L>a[i].R)//处理成从小到大搬
{
int temp=a[i].L;
a[i].L=a[i].R;
a[i].R=temp;
}
}
qsort(a,N,sizeof(a[0]),cmp);
memset(temp,0,sizeof(temp));
int ans=0;
while(1)
{
int last=0;
int t=0;
for(i=0;i<N;i++)
{
if(temp[i]==0)
{
t=1;
if(a[i].L>((last+1)/2)*2)//假设3号房的桌子处于移动中,4号房也不能移动桌子。
{
last=a[i].R;
temp[i]=1;
}
}
}
if(t) ans+=10;
else break;
}
printf("%d\n",ans);
}
return 0;
}