USACO 1.3.2 Barn Repair

来源:互联网 发布:易吧进销存软件下载 编辑:程序博客网 时间:2024/05/17 05:10

USACO 1.3.2  Barn Repair

     题意:有一定数量的牛棚,每个牛棚都是紧挨着的,但不是每个牛棚里都有牛,现在要给这些牛棚挡上门,门板的长度是任意的,但是数目一定,问这些数量的木板的最小长度和是多少?

     开始没读懂题,要是用一块板子挡住多个牛棚,牛怎么出来,哎,其实就是在板子数量有限的情况下,可能会一起挡住空牛棚,看是怎么挡浪费的最少。就是把所有连续的空牛棚按照大小排序,忽略掉第一个有牛的牛棚之前的和最后一个有牛的牛棚之后的,然后根据木板数量和一共有几个连续有牛的牛棚的数量确定最后留下的浪费牛棚块数,然后把这些块的长度和牛数加和即可。

     细节+代码:

 

    

/*   ID: 15257142   LANG: C   TASK: barn1  */#include<stdio.h>#include<string.h>int count[300],a[300],b[300];int qsort(int count[],int low,int high){ //不会用sort()函数的无奈。int low1,high1,temp1;low1 = low;high1 = high;temp1 = count[low1];while(low1<high1){while(low1<high1&&count[high1]>=temp1)high1--;count[low1] = count[high1];while(low1<high1&&count[low1]<temp1)low1++;count[high1] = count[low1];}count[low1] = temp1;if(low1-1>low)qsort(count,low,low1-1);if(low1+1<high)qsort(count,low1+1,high);return 0;}int main(){freopen("barn1.in","r",stdin);   freopen("barn1.out","w",stdout);int n,gate,cow,i,f,t,k,sum;memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(count,0,sizeof(count));scanf("%d%d%d",&n,&gate,&cow);for(i = 0;i<=cow-1;i++)scanf("%d",&a[i]);for(i = 0;i<=cow-1;i++)b[a[i]-1] = 1;f = 0;t = 0;k = -1;for(i = 0;i<=gate-1;i++){if(b[i]==1&&f==0)f = 1;if(b[i]==0&&b[i-1]==0)count[k]++;if(b[i]==0&&b[i-1]==1&&f==1){ //第一头牛之前的空棚不算。k++;count[k]++;}if(b[i]==1&&i>=1&&b[i-1]==0)t++;if(i==0&&b[i]==1)t++;}if(b[gate-1]==0) //判断最后的牛棚是否有牛,这个空棚不算在内。k--;qsort(count,0,k);sum = 0;for(i = 0;i<=t-n-1;i++)sum = sum+count[i];printf("%d\n",sum+cow);fclose(stdin);   fclose(stdout);return 0;}


 

 

原创粉丝点击