程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDOJ 1050 Moving Tables(經典貪心)

HDOJ 1050 Moving Tables(經典貪心)

編輯:關於C++

Moving Tables

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

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

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

好幾天沒做ACM Steps,睡前水一發。
這是1.3的貪心題目,運用貪心思想的經典類型題目。

題意:在一個狹窄的走廊裡將桌子從一個房間移動到另一個房間,走廊的寬度只能允許一個桌子通過。給出t,表示有t組測試數據。再給出n,表示要移動n個桌子。n下面有n行,每行兩個數字,表示將桌子從a房間移到b房間。走廊的分布圖如一圖所示,每移動一個桌子到達目的地房間需要花10分鐘,問移動n個桌子所需要的時間。
解題思路:若移動多個桌子時,所需要經過的走廊沒有重合處,即可以同時移動。若有一段走廊有m個桌子都要經過,一次只能經過一個桌子,則需要m*10的時間移動桌子。 設一個數組,下標值即為房間號。桌子經過房間時,該房間號為下標對應的數組值即加10。最後找到最大的數組值,即為移動完桌子需要的最短時間。
具體代碼如下:
#include
#include
#include
using namespace std;

int main()
{
	int t,n,count[410],i,start,end,k;
	scanf(%d,&t);
	while(t--)
	{
		scanf(%d,&n);
		memset(count,0,sizeof(count));
		while(n--)
		{
			scanf(%d%d,&start,&end);
			if(start>end)//可能出發位置比目的地房間大。 
			{            //無論大小,我們都可以看做從小的房間移動到大的房間 
				k=start;
				start=end;
				end=k;
			}
			if(start%2==0)//考慮實際情況,出發房間為偶數是減一,可參照題中給出的圖一 
			   start-=1;
			if(end%2==1)//目的地房間為奇數時加一 
			   end+=1;
			for(i=start;i<=end;++i)
			   count[i]+=10;
		}
		printf(%d
,*max_element(count,count+400));//STL中尋找數列最大值函數 
	}
	return 0;
} 


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved