HDOJ_1050 Moving Tables 解题报告

 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课件里也介绍了也是贪心的方 法,他只是找出最大重复乘以10即
可,这个证明我还不是能证出来,只是能体会到做法的合理性。
以下是我的写的贪心法代码:
Code:
  1. #include  
  2. #include   
  3.   
  4. using namespace std;  
  5. const int Max(210);  
  6.   
  7. struct node   
  8. {  
  9.     int s;  
  10.     int e;  
  11. }data[Max];  
  12.   
  13. int cmp(const void *a,const void *b)  
  14. {  
  15.     return ((struct node*)a)->s-((struct node*)b)->s;  
  16. }  
  17. int main()  
  18. {  
  19.     int  n, test;  
  20.   
  21.     cin>>test;  
  22.     while(test--)    
  23.     {  
  24.         cin>>n;  
  25.         for(int i = 1; i <= n; i ++)  
  26.         {  
  27.             cin>>data[i].s>>data[i].e;  
  28.             if(data[i].s>data[i].e)   
  29.                 swap(data[i].s,data[i].e);  
  30.         }  
  31.           
  32.         qsort(&data[1], n, sizeof(data[1]), cmp);     
  33.         int flag[Max];  
  34.         memset(flag,0,sizeof(flag));  
  35. //      for(i=1;i<=n;i++)  
  36. //          cout<  
  37.         int pre, sum = 0, step = n;  
  38.         while(step)  
  39.         {  
  40.             for(i = 1; i <= n; i ++)  
  41.             {  
  42.                 if(! flag[i])   
  43.                 {  
  44.                     pre = i; break;   
  45.                 }  
  46.             }  
  47.             flag[pre] = 1;   
  48.             step --;  
  49.             for(i=pre+1;i<=n;i++)  
  50.             {  
  51.                 if(! flag[i])  
  52.                 {                           
  53.                     if(data[i].s % 2 == 0 && data[i].s - 1 > data[pre].e)  
  54.                     {  
  55.                         flag[i] = 1; pre = i; step --;       
  56.                     }  
  57.                     if(data[i].s%2==1&&data[i].s>data[pre].e)  
  58.                     {  
  59.                         flag[i] = 1; pre = i; step --;           
  60.                     }  
  61.                 }  
  62.                   
  63.             }  
  64.             sum ++;  
  65.         }  
  66.         cout<
  67.     }  
  68.     return(0);  
  69. }  
 
以下是杭电ACM课件介绍的代码:
Code:
  1. #include    
  2. using namespace std;   
  3. int main()   
  4. {   
  5.     int t,i,j,N,P[200];  
  6.     int s,d,temp,k,min;   
  7.     cin>>t;   
  8.     for(i=0;i
  9.     {   
  10.         for(j=0;j<200;j++)   
  11.             P[j]=0;   
  12.         cin>>N;   
  13.         for(j=0;j
  14.         {   
  15.             cin>>s>>d;   
  16.             s=(s-1)/2;   
  17.             d=(d-1)/2;      
  18.             if(s>d)   
  19.             {   
  20.                 temp=s;   
  21.                 s=d;   
  22.                 d=temp;  
  23.             }   
  24.             for(k=s;k<=d;k++)   
  25.                 P[k]++;   
  26.         }   
  27.         min=-1;   
  28.         for(j=0;j<200;j++)   
  29.             if(P[j]>min)   
  30.                 min=P[j];   
  31.             cout<
  32.     }   
  33.     return 0;   
  34. }   
 

你可能感兴趣的:(HDOJ)