USACO:Barn Repair

简单贪心 细节注意点即可

/*
ID: Jang Lawrence
PROG: barn1
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int m,s,c;
int a[1111];
int main()
{
    #ifndef  DEBUG
freopen("barn1.in","r",stdin);
  freopen("barn1.out","w",stdout);
  #endif
  scanf("%d%d%d",&m,&s,&c);
  if(m>c-1) m=c-1;
  else m-=1;
  for(int i=1;i<=c;++i)
  scanf("%d",a+i-1);
  sort(a,a+c);
  int ans=a[c-1]-a[0]+1;
  for(int i=c-1;i>=1;--i)
  a[i]=a[i]-a[i-1]-1;
sort(a+1,a+c);
for(int i=1;i<=m;++i)
ans-=a[c-i];
printf("%d\n",ans);
    return 0;
}


你可能感兴趣的:(USACO:Barn Repair)