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.
#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)(4,6),所以若为奇数则每次奇数加一增大长度;或者每次输入的数字第一个数字为偶数的情形,比如(4,6)(1,3),所以若为偶数则每次偶数减一增大长度;
同时还要对每次输入的二个数字进行判判断大小,小的在前,大的在后,通过这种办法,便可以AC这道题目!!!
各位大神如果还有更好的方法希望多多指教!!!
posted on 2016-07-31 12:19 ZHOUXIAOHAO 阅读(243) 评论(0) 编辑 收藏 举报