nyoj 题目220 推桌子 贪心算法

这篇博客介绍了nyoj题目220中的推桌子问题,探讨了如何使用贪心算法解决该问题。通过将走廊宽度视为限制条件,将搬运过程视为区间问题,博主提出将相邻的房间视为等价,以优化搬运顺序,从而减少总时间。文章重点讨论了如何判断区间不相交以实现同时搬运,以达到最少的搬运时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

推桌子

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 3
描述
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. 

The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving. 

For each room, at most one table will be either moved in or moved out. Now, the manager seeks out a method to minimize the time to move all the tables. Your job is to write a program to solve the manager's problem.
输入
The input consists of T test cases. The number of test cases ) (T is given in the first line of the input file. Each test case begins with a line containing an integer N , 1 <= N <= 200, that represents the number of tables to move. 
Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t each room number appears at most once in the N lines). From the 3 + N -rd 
line, the remaining test cases are listed in the same manner as above.
输出
The output should contain the minimum time in minutes to complete the moving, one per line.
样例输入
3 
4 
10 20 
30 40 
50 60 
70 80 
2 
1 3 
2 200 
3 
10 100 
20 80 
30 50
样例输出
10
20
30
上传者
苗栋栋
基本题意:

400间房间如图所示,从一个房间搬东西到任意另一房间,都是花费10分钟。走廊宽度只能够一次过的,例如从3搬到7,从4搬到6,走廊不能同时过,只能等上一个搬好,再搬另一个房间的,给出一些数据,求搬运完的最少时间。

思路:

贪心算法的区间问题,把搬运房间的起点,终点看做是区间的两端,即左右坐标。没有区间相交的房间可以一起搬。需要注意的是,房间1-2,3-4,5-6........是等价的,需要把二排房间化为一排。

#include<stdio.h>
#include<stdlib.h>
struct fun
{
	int k;//开始的房间 
	int j;//结束的房间 
}s[205];
	
	int cmp(const void*a,const void*b)
	{
		return (*(fun*)a).k>(*(fun*)b).k?1:-1;
	}
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		int t,x,y,max;
		int sum=0;
		scanf("%d",&t);
		for(int i=0;i<t;i++)
		{
			scanf("%d %d",&x,&y);//起始房间,结束房间 
			s[i].k =(x+1)/2;//二排化做一排, 
			s[i].j =(y+1)/2;//同样道理 
			if(s[i].k>s[i].j)//将从大号房间向小号房间搬运的,
							//转化为从小号向大号搬运,
							//好判断,二者等价, 
			{
				max=s[i].k ;
				s[i].k =s[i].j;
				s[i].j =max;
			}
		}
		qsort(s,t,sizeof(s[0]),cmp);//左坐标从小到大排 
		for(int i=0;i<t;i++)//从第一个要搬的房间判断 
			{
			if(s[i].k!=0)//做标记,让不重叠房间为零 
			{
				sum++;//记录花费时间的搬运次数 
				for(int j=i+1;j<t;j++)
			{
					if(s[i].j<s[j].k )//目前的搬运终点,即右坐标大于下一个区间的左坐标,
											//二者不重叠 ,可同时进行 
					{
						s[i].j=s[j].j ;//把可同时进行的右坐标赋给上一个,
										//以便进行下下个判断 
							s[j].k =0;//可同时进行的,计为零,不另费时间。 
					}	
			}
			
			}
			
			}
		
		printf("%d\n",sum*10);//注意输出乘10 
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值