hdu-1050 Moving Tables

(简单的小技巧)

Moving Tables

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


Problem Description
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. 

hdu-1050 Moving Tables_第1张图片

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. 

hdu-1050 Moving Tables_第2张图片

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
 

这个题目是个实实在在水题,看大神的解题好像说用贪心算,但是我没有用贪心也水过去了。

解题思路:仔细观察其实会发现,移动的时间最终取决于移动桌子时所经过的路径的重叠最多的次数。例如上面第二个样列中,两张桌子移动时,有一段路径是这两张桌子都要经过的。而这也是重叠最多的次数,所以总时间为2*10

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int room[402];//有400个房间
int main()
{
    int t,n,a,b;
    scanf("%d",&t);
    while(t--)
    {
        memset(room,0,sizeof(room));//记得初始化
        scanf("%d",&n);
        while(n--)
        {
            scanf("%d%d",&a,&b);
            int temp;
            if(a>b)
            {
                temp=a;
                a=b;
                b=temp;
            }
            if(a%2==0)a--;//观察房间的排列规律,对门之间的过道是两个房间共有的。这是容易弄错的地方
            if(b%2!=0)b++;
            for(int i=a;i<=b;i++)
                room[i]++;//记录每扇门所对应的过道被经过的次数
        }
        int mx=0;
        for(int i=1;i<=400;i++)
            mx=max(mx,room[i]);//选出最大值就是所求时间
        printf("%d\n",mx*10);
    }
    return 0;
}


你可能感兴趣的:(hdu-1050 Moving Tables)