[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 }

 

posted @ 2013-01-23 15:49  tjsuhst  阅读(206)  评论(0编辑  收藏  举报