【USACO 1.3】Barn Repair
贪心,去掉最大的min(m,c)-1个间隔
/******************************************* TASK: barn1 LANG: C++ Created Time: 2016/9/9 14:36:17 *********************************/ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[201],d[201]; int main(){ freopen("barn1.in","r",stdin); freopen("barn1.out","w",stdout); int m,s,c; cin>>m>>s>>c; for(int i=0;i<c;i++)cin>>a[i]; sort(a,a+c); int ans=a[c-1]-a[0]+1; for(int i=1;i<c;i++) d[i]=a[i]-a[i-1]-1; sort(d+1,d+c); for(int i=1;i<min(m,c);i++) ans-=d[c-i]; cout<<ans<<endl; }
┆凉┆暖┆降┆等┆幸┆我┆我┆里┆将┆ ┆可┆有┆谦┆戮┆那┆ ┆大┆始┆ ┆然┆
┆薄┆一┆临┆你┆的┆还┆没┆ ┆来┆ ┆是┆来┆逊┆没┆些┆ ┆雁┆终┆ ┆而┆
┆ ┆暖┆ ┆如┆地┆站┆有┆ ┆也┆ ┆我┆ ┆的┆有┆精┆ ┆也┆没┆ ┆你┆
┆ ┆这┆ ┆试┆方┆在┆逃┆ ┆会┆ ┆在┆ ┆清┆来┆准┆ ┆没┆有┆ ┆没┆
┆ ┆生┆ ┆探┆ ┆最┆避┆ ┆在┆ ┆这┆ ┆晨┆ ┆的┆ ┆有┆来┆ ┆有┆
┆ ┆之┆ ┆般┆ ┆不┆ ┆ ┆这┆ ┆里┆ ┆没┆ ┆杀┆ ┆来┆ ┆ ┆来┆