[USACO 1.3.2]修理牛棚

地址:http://hustoj.sinaapp.com/problem.php?id=1826

快排(用了两次,一次升序,一次降序,其实都用降序就行了)

将牛棚间间隔最大的那几个减去即可,注意所需木板的最小总长应该是有牛的牛棚的个数,就这一点让我错了好多次

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 int m,s,c,st[200];
 6 
 7 bool cmp1(int a,int b)
 8 {
 9     return a>b;
10 }
11 
12 bool cmp2(int a,int b)
13 {
14     return a<b;
15 }
16 
17 int main()
18 {
19     int i,sum=0,d[200];
20     cin>>m>>s>>c;
21     for(i=0;i<c;i++)
22     {
23         cin>>st[i];
24     }
25     sort(st,st+c,cmp2);
26     for(i=0;i<c-1;i++)
27     {
28         d[i]=st[i+1]-st[i]-1;
29     }
30     sum=st[c-1]-st[0]+1;
31     sort(d,d+(c-1),cmp1);
32     for(i=0;i<m-1 && sum>c;i++)
33     {
34         sum-=d[i];
35     }
36     if(0==sum) sum=s;
37     cout<<sum<<endl;
38     return 0;
39 }

 

转载于:https://www.cnblogs.com/tjsuhst/archive/2013/01/23/2873271.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值