POJ1083 Moving Tables

问题:

Moving Tables

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 35163 Accepted: 11734

Description

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.

Input

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.

Output

The output should contain the minimum time in minutes to complete the moving, one per line.

Sample Input

3 
4 
10 20 
30 40 
50 60 
70 80 
2 
1 3 
2 200 
3 
10 100 
20 80 
30 50 

Sample Output

10
20
30

大概意思:

公司准备搬东西,有一个走廊很窄一次只能通过一个桌子(如图,上边是奇数号房间,下边是偶数号房间,共400个房间),一个桌子从 i 房间搬到 j 房间需要十分钟(不考虑距离),桌子从房间 i 搬到房间 j 时,房间 i 到房间 j 的这段走廊相当于被使用,第二张图是可能同时的搬运和不可能同时的搬运,你需要做的就是以最小的时间搬运好这些桌子。

输入:

            第一行:T,要测试的案例的数目

            第二行:第一个案例中的搬运活动数目N(1 <= N <= 200),接下来几行是从 i 房间搬到 j 房间的具体事务......

输出:第一个案例所需时间,第二个案例所需时间...........

想法:

想法1(自己想的笨方法,考虑情况太多,没有抓住问题本质):

把每个案例中的活动数先按照房间号(出发点)大小从小到大排序(题中好像输入时就是按房间号从小到大排好的),之后看冲突的房间(按照房间号小的优先,房间跨度大的优先都不行),我想的是按照冲突的数量最大的搬运优先的原则,小冲突向后推延十分钟,之后小冲突的冲突数量减少1,再次比较冲突数量,依此循环。

想法2(最大经过次数):

考虑每个房间有多少搬运过程需要经过,经过次数最大的房间乘以10分钟即为最小时间,即记录每个房间走过次数

代码实现:

想法1(未实现):

#include<iostream>
#include<algorithm>
using namespace std;

void countCollision(int activities[][3],int num)//统计冲突次数
{
	for (int i = 0; i < num; i++)
	{
		//将上面的房间对应转化到下面,方便计算
		if (activities[i][0] % 2 == 1)	
			activities[i][0]++;
		if (activities[i][1] % 2 == 1)
			activities[i][1]++;

		for (int j = i + 1; j < num; i++)//活动算过的就不用再算了
		{
			if (activities[i][1] >= activities[j][0])//当前活动的目的点大于等于后一个的起始点,冲突
			{
				activities[i][2]++;	//当前活动冲突数加1
				activities[j][2]++;	//后一个冲突数加1
			}
		}		
	}
}

void Qsort(int a[], int low, int high)
{
	if (low >= high)
	{
		return;
	}
	int first = low;
	int last = high;
	int key = a[first];/*用字表的第一个记录作为枢轴*/

	while (first < last)
	{
		while (first < last && a[last] >= key)
		{
			--last;
		}

		a[first] = a[last];/*将比第一个小的移到低端*/

		while (first < last && a[first] <= key)
		{
			++first;
		}

		a[last] = a[first];
		/*将比第一个大的移到高端*/
	}
	a[first] = key;/*枢轴记录到位*/
	Qsort(a, low, first - 1);
	Qsort(a, first + 1, high);
}

int countTime(int activities[][3], int num)
{
	int time;
	int collision[200];

	for (int j = 0; j <= num; j++)	//根据冲突数,从小到大排列活动,冲突数越大应越先执行,可能会解救更多的活动
	{
		collision[j] = activities[j][2];	//活动数赋值
		//Qsort(collision, 0, num - 1);	
		//调用快排不行,因为排好序的activities数组是按照起始房间号排序的,
		//而这个排序是按照冲突数大小排序的,排好序就丢了原先的下标
	}

	int index[200];
	for (int i = num - 1; i >= 0; i--)
	{
		for (int j = i; j >= 0; j--)
		{
			//找到不冲突的,然后执行,执行完,相关的活动的冲突数减1 
			//如果能保存各个活动与其他活动之间的关联,分块来看应该更容易
			//当冲突数最大的活动执行时,与它不冲突的且冲突数较大的应该同时执行
			//当前活动的前一个活动的目的房间号小于它的起始房间号,或者前一个活动的起始房间号大于这个活动的目的房间号时,执行前一个活动
			if (collision[i] != -1 && collision[j] != -1)	//一旦运行过,则赋值为-1
			{
				if (activities[collision[i]][0] > activities[j - 1][1] || activities[collision[i]][1] < activities[j - 1][0])			
				{	

					//已经找到1个可以与这个冲突数最大的房间独立运行的房间,接下来要考虑可以与这2个冲突数最大的房间独立运行的其他房间
					//问题在于怎么实现,我想的是用index数组把可以同时运行的活动存放起来,但这样算法也太过复杂了

					/******************************************************/
					/******************************************************/
					/******************************************************/

				}
			}
		}
	}
}

int main()
{
	int test;	//案例数
	cin >> test;
	for (int i = 0; i < test; i++)
	{
		int num;	//每个案例中的活动数
		int activities[200][3] = {0};	//最大活动数200,第三个参数保存冲突数量
		for (int j = 0; j < num; j++)
		{
			cin >> activities[j][0] >> activities[j][1];
		}
		countCollision(activities, num);
		
	}
	return 0;
}

想法2(最大经过次数): 

注意:要使前面的房间号小于后面的房间号

#include<iostream>
#include<algorithm>
using namespace std;

void countFrequency(int room[], int activities[][2], int num)//统计房间经过次数
{
	for (int i = 0; i < num; i++)
	{
		//将上面的房间对应转化到下面,方便计算
		if (activities[i][0] % 2 == 1)
			activities[i][0]++;
		if (activities[i][1] % 2 == 1)
			activities[i][1]++;
		for (int j = activities[i][0]/2; j <= activities[i][1]/2; j++)
		{
			room[j]++;
		}
	}
}

int main()
{
	int test;	//案例数
	cin >> test;
	int *test_time = new int[test];	//存储每个案例的所需最小时间
	for (int i = 0; i < test; i++)
	{
		int num;	//每个案例中的活动数
		cin >> num;
		int activities[200][2];	//最大活动数200,起始房间,目的房间
		int max_frequency = 0;	//最大经过次数
		int room[201] = { 0 };	//全转化到下面的房间
		for (int j = 0; j < num; j++)
		{
			cin >> activities[j][0] >> activities[j][1];
			if (activities[j][0] > activities[j][1])//使前面的房间号小于等于后面的
				swap(activities[j][0], activities[j][1]);	
		}
		countFrequency(room,activities, num);
		for (int j = 1; j < 201; j++)
		{
			max_frequency = room[j] > max_frequency ? room[j] : max_frequency;
		}	
		test_time[i] = max_frequency * 10;
	}
	for (int i = 0; i < test; i++)
	{
		cout << test_time[i] << endl;
	}
	delete[] test_time;	//释放空间
	return 0;
}

 

反思:

本来以为自己想的方法已经很不错了,但是,我的问题在于把问题分散化了,放在了每个搬运活动上,而没有放在固定的东西(房间号)上,还是没有抓住重点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值