HDU杭电1050

 本人第二次写博客,分享一下心得,不足之处还请各位大神海涵以及提出不足之处,在此谢过

题目描述:

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
 
 
题目大意既是:一个公式的400个房间位于同一楼层,现今要进行对指定房间的桌子进行移动,然而桌子的宽度和楼层之间的走廊宽度相当(意思就是如果二组指定的房间序号有交集,便只能先移动完一组房间后才可以移动下一组房间),每次移动桌子需要10分钟,最后输出所花的最少时间(即最优解)
题目分析:提到最优解,便首先考虑是否和贪心算法有关,显然在此,此题便是联系的是贪心算法。
首先;附上WA代码,本人一开始看到题想都没想就觉得只要存在交集便要分开移动桌子,否则便可以同时移动,所以仅仅考虑房间号之间的关系,先是我的WA代码:
WA代码:

#include<stdio.h>

int main(){

int p,q,n,sum,m,i,j,k,a[10000][10000],temp;

scanf("%d",&n);

while(n--){

sum=0;

scanf("%d",&m);

for(i=0;i<m;i++){

scanf("%d%d",&a[i][0],&a[i][1]);

if(a[i][0]>a[i][1]){

temp=a[i][0];

a[i][0]=a[i][1];

a[i][1]=0;

}

}

for(i=0;i<m-1;i++){

for(j=i+1;j<m;j++){

if(a[i][1]<=a[j][0]||a[i][0]>=a[j][1]){

sum+=0;

}

else{

sum+=10;

}

}

}

printf("%d\n",sum);

}

return 0;

WA了好几次以后,重新看了一下题,发现自己有个地方没想到,因此重新换了个做法,正式贪心,先附上AC代码,在对此进行分析

AC代码:

#include<stdio.h>

int main()

{

    int i,j,k,m,n,x,y,t,max;

    scanf("%d",&m);

    for(i=1;i<=m;i++)

    {

        scanf("%d",&n);

        int a[100000]={0};

        for(j=1;j<=n;j++)

        {

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

            if(x>y)

            {

                t=x;

                x=y;

                y=t;

            }

            if(x%2==0)

                x--;

            if(y%2!=0)

                y++;    

            for(k=x;k<=y;k++)

                    a[k]++;

                                    

        }

        max=a[1];

        for(j=2;j<=400;j++)

            if(a[j]>max)

                max=a[j];

        printf("%d\n",max*10);        

    }

}

第一种方法中存在很多未考虑的因素,仅仅考虑连续的二次,并未真正的对所有的输入数据进行判断;所以运行时输出数据并不准确,同时sum依据第一种方法不好继续相加;(在此需要自己通过随便几组数据发现)

第二种方法则对每次走过的地方进行计数,取最多的数乘以10分钟即为正确答案

!!!!重点是要判断当每次输入的第二个数字为奇数时的情形,因为有可能会与下一组数字重复,比如(1,3)(46),所以若为奇数则每次奇数加一增大长度;或者每次输入的数字第一个数字为偶数的情形,比如(4,6)(1,3),所以若为偶数则每次偶数减一增大长度;

同时还要对每次输入的二个数字进行判判断大小,小的在前,大的在后,通过这种办法,便可以AC这道题目!!!

 

各位大神如果还有更好的方法希望多多指教!!!

转载于:https://www.cnblogs.com/ZHOUXIAOHAO/p/5722770.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值