hdu Acmsteps Moving tables解题报告

来源:互联网 发布:韶关市始兴县网络问政 编辑:程序博客网 时间:2024/05/16 12:58

题目:

 
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



问题描述:大意是移动桌子,其过道是只能通过一张桌子的,且一旦开始搬一张桌子,都要花费10分钟,那么面对海量的数据时,400个房间需要的时间便费事了,需要计算出能搬完桌子的最短的时间;
问题分析:问题之所以难以解决,是因为,过道是只能通过一张桌子,然而这样的问题,我们是无法具象出来,我们如何用数据表示过道的占用于空暇两种状态,考虑好这个问题,我们便可以开始思考解决问题的解决方案。我们如何表示过道,理所当然,第一想到的是我们可以建立一个数组,用0表示空闲,1表示当前已占用,那么,我们就可以开始抽象现实。第二个要考虑的问题是,最短时间,如何计算最短的时间,我们的了解规则,过道不占用共同的过道,便可以一起进行,那么说,我们可以根据这个分类数字,分出最小类数的组合。开始考虑...

昨天我考虑的是抽象现实,现在,我却又换了种思路,我觉得问题应该从问题出发,我们如何处理最小的问题,我们理解关键矛盾所在,走廊的限制,那么我们是否可以通过考虑用数据表示走廊,然后用走廊来表示其限制,限制出来了,即是不可同时搬的走廊段,通过矛盾与所求关系,所求最小值是否可以出来?是的,在看了些问题的解决心得之后,我看到了重叠二字,寻找最多的走廊重叠段,可以试着去证明重叠数最大的走廊段的数字代表最小值,已知矛盾的体现是走廊的重叠,那么,不同的走廊段之间的矛盾是不冲突的,即是说,在其他走廊上的路线产生的重叠数是与最大处是不冲突的,因为冲突时是靠路线的重叠来体现的。这是一种思路,即是,从矛盾与问题的解决的直接关系解决,另一种我没去看,听说是贪心算法,应该是根据矛盾建立新的选择规则,即是选择最大可同时搬的数目。在此不多说。


解决方式:
具象走廊,用数组表示,每次重叠数值加一;数据输入结束后寻找最大重叠数。实质上,此种思路是反着来的贪心算法,计算矛盾的最大数,另一种思路是正着来,计算可以同时搬的最大数目,这题如果时间不是固定的,那么这道题目大体就是一道动态规划题目,在此不再赘述,有兴趣可以证一下这道题的充分必要条件,可以猜测与矛盾的走廊数必然有联系。

//0MS  228K  381B 
#include<stdio.h>
int main()
{
 int n,max=0;
 scanf("%d",&n);
 while(n--)
 {
  int m,corridor[201]={0};
  scanf("%d",&m);
  while(m--)
  {
   int a,b,i;
   scanf("%d %d",&a,&b);
   if(a>b)
   {
    a=a^b;
    b=a^b;
    a=a^b;
   }
   a=(a+1)>>1;
   b=(b+1)>>1;
   for(i=a;i<=b;i++)
    corridor[i]++;
   for(i=1;i<=200;i++)
   {
    if(max<corridor[i])
     max=corridor[i];
   }
  }
  printf("%d\n",max*10);
 }
 return 0;
}

遇到问题多思考,总会有多种解决办法的,加油!



0 0
原创粉丝点击