HDU 1050 Moving Tables (两种思路)

来源:互联网 发布:ubuntu阻塞脚本自启动 编辑:程序博客网 时间:2024/05/18 13:08

Moving Tables

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


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
102030
 

Source
Asia 2001, Taejon (South Korea)


方法一:
这是几百年前WJL告诉我的方法:开一个数组记录次数,最大的便是。

[html] view plain copy
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include <algorithm>  
  5. #include <math.h>  
  6. using namespace std;  
  7. const int N = 406;  
  8. int main()  
  9. {  
  10.     int T;  
  11.     scanf("%d",&T);  
  12.     while(T--)  
  13.     {  
  14.   
  15.         int n;  
  16.         scanf("%d",&n);  
  17.         int ans=0;  
  18.         int flag[N];  
  19.         int a,b;  
  20.         memset(flag,0,sizeof flag);  
  21.         for(int i=1;i<=n;i++)  
  22.         {  
  23.             scanf("%d %d",&a,&b);  
  24.             if(a>b) swap(a,b);  
  25.             if(a%2==0) a--;  
  26.             if(b%2==1) b++;  
  27.             for(int j=a;j<=b;j++)  
  28.             {  
  29.                 flag[j]++;  
  30.                 if(ans<flag[j]) ans=flag[j];  
  31.             }  
  32.         }  
  33.         printf("%d\n",ans*10);  
  34.     }  
  35.   
  36.     return 0;  
  37. }  

方法二:
自己想的,反复贪心,写起来很麻烦。思路就是运行N次的“今年暑假不AC”那题,N×10就是答案。

[html] view plain copy
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <algorithm>  
  4. #include <queue>  
  5. #include <string.h>  
  6. using namespace std;  
  7. struct node  
  8. {  
  9.     int be;  
  10.     int end;  
  11. }a[202];  
  12. bool cmd(node a,node b)  
  13. {  
  14.     return a.be< b.be;  
  15. }  
  16. int main()  
  17. {  
  18.     int t;  
  19.     while(~scanf("%d",&t))  
  20.     {  
  21.         while(t--)  
  22.         {  
  23.             int n;  
  24.             scanf("%d",&n);  
  25.             memset(a,0,sizeof a);  
  26.             for(int i=1;i<=n;i++)  
  27.             {  
  28.                 scanf("%d %d",&a[i].be,&a[i].end);  
  29.                 if(a[i].be>a[i].end) swap(a[i].be,a[i].end);  
  30.                 if(a[i].be%2==0) a[i].be--;  
  31.                 if(a[i].end%2==1) a[i].end++;  
  32.             }  
  33.             int flag[202];  
  34.             memset(flag,0,sizeof flag);  
  35.             sort(a+1,a+1+n,cmd);  
  36.             int i;  
  37.             int begin;  
  38.             for(i=0;;i++)  
  39.             {  
  40.                 begin=0;  
  41.                 int q=0;  
  42.                 for(int j=1;j<=n;j++)  
  43.                 {  
  44.                     if(flag[j])  
  45.                         continue;  
  46.                     q++;  
  47.                     if(begin<=a[j].be)  
  48.                     {  
  49.                         begin=a[j].end;  
  50.                         flag[j]=1;  
  51.                     }  
  52.                 }  
  53.                 if(q==0)  
  54.                 {  
  55.                  //   printf("1");  
  56.                     break;  
  57.                 }  
  58.             }  
  59.             printf("%d\n",i*10);  
  60.         }  
  61.     }  
  62.   
  63.     return 0;  
  64. }  
0 0