#include<stdio.h>
#include<algorithm>
using namespace std;
struct Go//存储路线的起点房间和终点房间,以及是否使用过
{
int st,ed;
int move;
}go[205];
int cmp(Go a,Go b)//将起点从小到大排起来
{
return a.st<b.st;//这里不排终点,因为下面是计算最少有多少时间,也就是最少同时移动几次,而不是最多同时移动多少桌子,例如(1,3)(2,5)(4,7)(6,9)(8,20)(10,12)(13,15)就不能用排结束房间来做
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,t,j,sr/*当前结束房间号*/,times/*最少几次的变量*/,flag/*所有房间的桌子都是否移动过的变量*/;
for(i=0;i<n;i++)
{
scanf("%d",&t);
for(j=0;j<t;j++)
{
scanf("%d%d",&go[j].st,&go[j].ed);
if(go[j].st>go[j].ed)
{
int item;
item=go[j].st;
go[j].st=go[j].ed;
go[j].ed=item;
}
//下面是将(1,3)(4,5)会同时的情况避免,由题可知,3与4对门,共用走廊不能同时
if(go[j].st%2==0)//因为起点都视为奇数
{
go[j].st--;//将起点变为奇数
}
if(go[j].ed%2==1)//因为终点都视为偶数
{
go[j].ed++;//将终点变为偶数
}
go[j].move=0;//将所有房间的桌子都变为未移动状态
}
sort(go,go+t,cmp);//按照起点排序
times=0;
flag=0;
while(flag==0)
{
sr=0;//重新遍历
flag=1;
times++;
for(j=0;j<t;j++)
{
if(go[j].move==0&&sr<go[j].st)//判断是否还有桌子需要移动
{
sr=go[j].ed;//当前位置改变
flag=0;//当前移动过桌子,则继续循环,进行下一次判定
go[j].move=1;//当前桌子标记为移动过
}
}
}
printf("%d\n",(times-1)*10);//因为次数先增加再判断,所以需要-1
}
}
}
Hdu1050 - Moving Tables - 贪心算法
最新推荐文章于 2018-04-29 19:28:23 发布