Moving Tables

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


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
  1 /*
  2 //代码一:   ---wrong answer(按照1051题的思路做的,但是不知道哪错了-------求指教)
  3 #include<cstdio>
  4 #include<algorithm>
  5 using namespace std;
  6 
  7 struct act
  8 {
  9     int s;
 10     int t;
 11 }a[201];
 12 bool visit[201];
 13 
 14 bool cmp(act k1,act k2)
 15 {
 16     return k1.s<k2.s;
 17 }
 18 
 19 int main()
 20 {
 21     int T,i,j,n,st,count,t1,t2;
 22     bool flag;
 23     scanf("%d",&T);
 24     while(T--)
 25     {
 26         scanf("%d",&n);
 27         for(i=0;i<n;++i)
 28         {
 29             scanf("%d%d",&t1,&t2);
 30             t1=(t1+1)/2;//由于房间是奇偶分开的,所以先把他们换成连续的自然数,即n=(n+1)/2.这样对门的就变成一个编号了---很神奇
 31             t2=(t2+1)/2;
 32             if(t1>t2)
 33             {
 34                 t1^=t2;
 35                 t2^=t1;
 36                 t1^=t2;
 37             }
 38             a[i].s=t1;
 39             a[i].t=t2;
 40         }
 41         sort(a,a+n,cmp);
 42         memset(visit,false,sizeof(visit));
 43         st=0;
 44         count=0;
 45         visit[0]=true;
 46         while(st<n)
 47         {
 48             ++count;
 49             for(i=st+1,j=st,flag=true;i<n;++i)
 50             {
 51                 if(a[j].t<a[i].s)
 52                 {
 53                     visit[i]=true;
 54                     j=i;
 55                 }
 56                 else
 57                 {
 58                     if(flag)
 59                     {
 60                         st=i;
 61                         flag=false;
 62                     }
 63                 }
 64             }
 65             if(flag)
 66                 break;
 67         }
 68         printf("%d\n",count*10);
 69     }
 70     return 0;
 71 }
 72 */
 73 
 74 //代码二:另一种思路----只用找到每个编号的最大重叠数量即可
 75 #include <cstdio>
 76 #include<cstring>
 77 int a[201];
 78 
 79 int main()
 80 {
 81     int T,s,t,j,max,n;
 82     scanf("%d",&T);
 83     while(T--)
 84     {
 85         max=0;
 86         memset(a,0,sizeof(a));
 87         scanf("%d",&n);
 88         while(n--)
 89         {
 90             scanf("%d%d",&s,&t);
 91             if(s>t)
 92             {
 93                 s^=t;
 94                 t^=s;
 95                 s^=t;
 96             }
 97             s=(s+1)/2;        //很神奇的转化-----把对门的都变成一个编号,因为边界对门也不能重叠
 98             t=(t+1)/2;
 99             for(j=s;j<=t;j++)
100             {
101                 ++a[j];
102                 if(a[j]>max)
103                     max=a[j];
104             
105             }
106         }
107         printf("%d\n",max*10);
108     }
109     return 0;
110 }
111  

 

 
posted on 2012-08-23 16:54  可笑痴狂  阅读(1880)  评论(0编辑  收藏  举报