我是靠谱客的博主 欢喜手链,最近开发中收集的这篇文章主要介绍HDU 1050 Moving Tables,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

C - Moving Tables

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

 

 

#include<cstdio>

#include<algorithm>

#include<stdio.h>

#include<cstring>

using namespace std;

int main()

{

    int T;

    scanf("%d",&T);

    while(T--)

    {

        int n;

        scanf("%d",&n);

        int i,j,sum=0,a[408];

        memset(a,0,sizeof(a));

        for(i=0;i<n;i++)

        {

            int x,y;

            scanf("%d%d",&x,&y);

            if(x%2==0)

                x--;

            if(y%2==0)

                y--;

            int p=min(x,y);

            int q=max(x,y);

            for(j=p;j<=q;j++)

            {

                a[j]++;

                if(sum<a[j])

                    sum=a[j];

            }

        }

        printf("%dn",sum*10);

    }

    return 0;

}

 

只要两次搬运需要占用同一段走廊,那么他们就不可能同时执行,而最多的执行次数就恰恰等于重合次数最多的走廊段数,即只需将走廊分为200份,计算出每段走廊的重合次数,取其中的最大值*每次所需时间(10min)即可。


 大神思路:

题意很简单,就说有一些桌子需要从某些房间搬到另一些房间,但中间只有一条走廊,且走廊中任何一段只能同时进行一次搬运,如图中若需要把桌子从房间1搬到房间5,则不能同时进行房间2到4之间的搬运,因为搬运路线中有重叠部分。
题解:此题有两种方法:方法一:由于n太小,可以用一种最简单的解法:即每走一次就对走过的走廊加1,然后遍历一边找出最大的覆盖值;和树状数组有点相似;
方法二:先对起点排序,然后判断这些线段是否可以和其他的线段放在一排,不能放在一排单放在一排,flag值就加1;
注意:1:起点与终点的大小要先比较大小,因为可以有类似这种情况40-——30;
2:奇偶房间对称,共用一个走廊,所以要先对s和e进行奇偶判断,奇数就加一;
代码实现:
方法一:
#include<iostream>  
using namespace std;  
int main()  
{    
    int M;  
   cin>>M;  
   while(M--)  
   {  
       int i,n,k,l;              //l个需要搬动的桌子,起始n,中止k  
       cin>>l;  
       int id[201]={0};  
       while(l--)  
       {  
           cin>>n>>k;  
           if(n>k)  
           {i=n;n=k;k=i;}             //交换小值n在前  
            
           for(i=(n+1)/2;i<=(k+1)/2;i++)            //奇数偶数全加一然后除以2就是走廊号。  
               id[i]++;               
       }         
       int max=0;  
       for(i=0;i<201;i++)  
           if(max<id[i])  
              max=id[i];        //找覆盖的最大值  
       cout<<max*10<<endl;    
   }  

return 0;  
}  
方法二:
#include<stdio.h>  
#include<cstring>  
#include<algorithm>  
using namespace std;  
#pragma comment(linker,"/STACK:102400000,102400000")  
#define MAX   205  
struct line1  
{  
     int s,e;  
}line[MAX];  
int sign[MAX];//为标记可以放在一排的最后的终点位置;  
bool cmp(line1 x,line1 y)  
{  
     return(x.s<y.s);  
}//对起点进行排序  
int main()  
{  
   //freopen("input.txt","r",stdin);  
   int i,j,t,n,x,y;  
   scanf("%d",&t);  
   while(t--)  
   {  
        scanf("%d",&n);  
        memset(sign,-1,sizeof(sign));//标记要初始化为-1,因为起点可能是0;  
        for(i=0;i<n;i++)  
        {  
             scanf("%d%d",&x,&y);  
             if(x%2)x+=1;//走廊是奇偶房间公用的  
             if(y%2)y+=1;  
             line[i].s=x>y?y:x;//起点与终点的大小进行比较  
             line[i].e=x>y?x:y;  
        }  
        sort(line,line+n,cmp);//排序  
        int flag=0;//标记最大的可以排成多少排  
        for( i=0;i<n;i++)  
        {  
             for(j=0;j<n;j++)  
             {  
                  if(line[i].s>sign[j])  
                  {  
                       sign[j]=line[i].e;  
                       if(j+1>flag)flag=j+1; 
                       break;//一旦遇到这段线段可以放在某一排就跳出循环  
                  }  
             }  
        }  
        printf("%dn",flag*10);  
   }  
   return 0;  
}  

最后

以上就是欢喜手链为你收集整理的HDU 1050 Moving Tables的全部内容,希望文章能够帮你解决HDU 1050 Moving Tables所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部