5 3 1 3 5 8 11
7
#include<stdio.h> #include<string.h> #define N 10010 int a[N]; void qsort(int a[],int l,int r) { int x=a[l],i=l,j=r; if(l>=r) return ; while(i<j) { while(i<j && a[j]>=x) { j--; } a[i]=a[j]; while(i<j && a[i]<=x) { i++; } a[j]=a[i]; } a[i]=x; qsort(a,l,i-1); qsort(a,i+1,r); } int main() { int n,m,i,j; int position[N]; int distance[N-1]; while(~scanf("%d%d",&n,&m)) { for(i=0;i<=n-1;i++) { scanf("%d",&position[i]); } qsort(position,0,n-1); for(i=1;i<=n-1;i++) { distance[i]=position[i]-position[i-1]-1; } qsort(distance,1,n-1); if(m>=n) { printf("%d\n",n); continue; } int nline=1; int totallength=position[n-1]-position[0]+1; int devide=n-1; while((nline<m) && (distance[devide]>0)) { nline++; totallength-=distance[devide]; devide--; } printf("%d\n",totallength); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/sh_tomorrow/article/details/48028989