1. 程式人生 > >HDU1050: Moving Tables(搬桌子)

HDU1050: Moving Tables(搬桌子)

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1050

Moving Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40291    Accepted Submission(s): 13273


Problem DescriptionThe 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.

InputThe input consists of T test cases. The number of test cases ) (T is given in the first line of the input. 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 N+3-rd line, the remaining test cases are listed in the same manner as above.

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

Sample Input3 4 10 20 30 40 50 60 70 80 2 1 3 2 200 3 10 100 20 80 30 50
Sample Output10 20 30
Source
以下題意及解體思路轉載自部落格:https://blog.csdn.net/qq_26891045/article/details/51519759

題意:在一個狹窄的走廊裡將桌子從一個房間移動到另一個房間,走廊的寬度只能允許一個桌子通過。給出t,

   表示有t組測試資料。再給出n,表示要移動n個桌子。n下面有n行,每行兩個數字,表示將桌子從a房間

   移到b房間。走廊的分佈圖如一圖所示,每移動一個桌子到達目的地房間需要花10分鐘,問移動n個桌子

   所需要的時間。

解題思路:若移動多個桌子時,所需要經過的走廊沒有重合處,即可以同時移動。若有一段走廊有m個桌子都

     要經過,一次只能經過一個桌子,則需要m*10的時間移動桌子。設一個數組,下標值即為房間號。

     桌子經過房間時,該房間號為下標對應的陣列值即加1。最後找到最大的陣列值,最大值*10即為移動完桌子

     需要的最短時間。

可以做的優化:

可以把一對面對面相鄰的房間(例如room3、room4)看成一個房間,所以總共400個房間可以看成200個房間。

陣列的下標為1到200,1表示room1-room2,2表示room3-room4

程式碼:

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int Count[210];

int main()
 {
	int T,n,i,j,temp;
	int from,to;
	
	cin>>T;
	while(T--)
	{
		int max=0;
		int ans;
		memset(Count,0,sizeof(Count)); 
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>from>>to;
			if(from>to)swap(from,to);	
			if(from%2==1)
			{
				from=(from+1);
			} 
			if(to%2==1)
			{
				to=(to+1);
			}
			from=from/2;
			to=to/2;
			for(j=from;j<=to;j++)Count[j]++; 
			
		}
		for(j=1;j<=200;j++)
		{
			if(Count[j]>max)max=Count[j];
		}
		cout<<max*10<<endl;	
	
	
	} 
			
 	return 0;
 } 

遇到的坑:

一開始寫完程式碼後一直編譯失敗,後來是因為把一個數組名設定成了count  (應該是c++的一個內建函式)導致的。

低階錯誤,下次要注意