标签:str blank names amp hint strong pac variables ret
You are given n constant integers c[1], c[2], ..., c[n] and an integer k. You are to assign values to 2k integer variables, x[1], x[2], ..., x[k], y[1], y[2], ..., y[k], such that,
(1) For each 1 ≤ i ≤ k, x[i] ≤ y[i] holds,
(2) For each 1 ≤ i ≤ n, exists at least one such j (1 ≤ j ≤ k) that x[j] ≤ c[i] ≤ y[j].
Please find the minimum value of S=(y[1]+y[2]+...+y[k])-(x[1]+x[2]+...+x[k]).
First line contains two integers n, k. (1 ≤ n, k ≤ 100000)
Following n lines contain integers c[1], c[2], ..., c[n]. (-1000000000 ≤ c[i] ≤ 1000000000)
Output the minimum value of integer S.
x[1]=-5, y[1]=4,
x[2]=10, y[2]=10.1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int n,k,c[100001],d[100001]; 7 long long ans; 8 bool cmp(int a,int b) 9 { 10 return a>b; 11 } 12 int main() 13 {int i; 14 cin>>n>>k; 15 for (i=1;i<=n;i++) 16 { 17 scanf("%d",&c[i]); 18 } 19 sort(c+1,c+n+1); 20 ans=c[n]-c[1]; 21 for (i=1;i<n;i++) 22 d[i]=c[i+1]-c[i]; 23 sort(d+1,d+n,cmp); 24 for (i=1;i<=k-1;i++) 25 { 26 ans-=d[i]; 27 } 28 cout<<ans; 29 }
标签:str blank names amp hint strong pac variables ret
原文地址:http://www.cnblogs.com/Y-E-T-I/p/7617870.html