空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
![](https://i-blog.csdnimg.cn/blog_migrate/cfdcd5eed4e2dcc89f20c08499461f95.png)
![](https://i-blog.csdnimg.cn/blog_migrate/5437dc8b0578434012c39882f534bb1d.png)
The weakness of left figure and right figure are 4 and 11, respectively.
Now, Li Si wants to know the minimum value of “weakness”. Li Si is too old to calculate the answer quickly, so he asks you for help.
输入描述:
The first line contains an integer T, where T is the number of test cases. T test cases follow. For each test case, the first line contains two integers N and K, where N is the number of stones and K is a variable which provided by Li Si.
• 1 ≤ T ≤ 50.
输出描述:
For each test case, print one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the minimum value of “weakness”.
输入
2
5 2
1 2 3 4 5
5 3
1 3 2 2 7
输出
Case #1: 4
Case #2: 7
备注:
For the first case, one of the best ways is [1,2,3,4,5], weakness = (2−1)+(3−2)+(4−3)+(5−4) = 4.
For the second case, one of the best ways is [7,3,2,2,1], weakness = (7−2)+(3−2)+(2−1) = 7.
#include<iostream>
#include<cstring>
#include<algorithm>
bool cmp(int x,int y)
{
return x>y;
}
using namespace std;
int main()
{
int t;
cin>>t;
int n,k;
int a[1000];
for(int j=0;j<t;j++)
{
cin>>n>>k;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n,cmp);//对数组进行降序排序
int weak=0;
for(int i=0;i<n&&i+k-1<n;i++)//每k个取最大-最小
{
weak+=a[i]-a[i+k-1];
}
cout<<"Case #"<<j+1<<": "<<weak<<endl;
}
return 0;
}