HDOJ_1050 Moving Tables 解题报告

本文介绍了HDOJ中编号为1050的Moving Tables问题,详细解析了题意,并提供了求解思路和算法实现,帮助读者理解并解决该编程挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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<iostream>  
  2. #include <algorithm>  
  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<<data[i].s<<'/t'<<data[i].e<<endl;  
  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<<sum * 10<<endl;  
  67.     }  
  68.     return(0);  
  69. }  
 
以下是杭电ACM课件介绍的代码:
Code:
  1. #include <iostream>   
  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<t;i++)   
  9.     {   
  10.         for(j=0;j<200;j++)   
  11.             P[j]=0;   
  12.         cin>>N;   
  13.         for(j=0;j<N;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<<min*10<<endl;   
  32.     }   
  33.     return 0;   
  34. }   
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值