A sad story

链接:https://ac.nowcoder.com/acm/problem/15329
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

The Great Wall story of Meng Jiangnv’s Bitter Weeping happened during the Qin Dynasty (221BC- 206BC). Meng jiangnv was a beauty in the Qin Dynasty, and she lived happily with her husband. At that time, Emperor Qin Shihuang (the first emperor of Qin) announced to build the Great Wall. And the officials suddenly broke in their happy life and took Meng’s husband away to build the wall. Because of the missing for her husband, she decided to set off to look for her husband. After a long journey, finally she reached the foot of the Great Wall at the present Shanhaiguan Pass. Upon her arrival, a bad news came to her, however, her husband had already died of exhaustion and was buried into the Great Wall! Meng could not help crying. She sat on the ground and cried and cried. Suddenly with a tremendous noise, a 400 kilometer-long (248-mile-long) section of the wall collapsed over her bitter wail.
Today, Qin Shihuang gets N stones. The height of the ith stone is Ai. He will use all these stones to rebuild the Great Wall. In order to make the Great Wall more sturdy, the prime minister Li Si proposes a formula to calculate the “weakness” of the reconstructed Great Wall
在这里插入图片描述
The Bi is the height of the ith stone in the reconstructed Great Wall, and the K is provided by Li Si.
For example, Qin Shihuang gets 5 stones. The height of these stones are [5,3,2,4,1], and the K is 2. There are 120 different ways to rebuild the Great Wall. The following figures show the two solutions:
在这里插入图片描述
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.

The second line contains N integers A1, A2, … , AN, where Ai is the height of the ith stone that QinShiHuang gets.
• 1 ≤ T ≤ 50.
• 1 ≤ N ≤ 103.
• 1 ≤ K ≤ N.
• 1≤ Ai ≤104.

输出描述:

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”.
示例1
输入
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<bits/stdc++.h>
using namespace std;

int T;
int N,K;
int a[1005];

int main(){
    cin>>T;
    int casen=1;
    while(casen<=T){
        cin>>N>>K;
        for(int i=1;i<=N;i++){
            cin>>a[i];
        }
        sort(a+1,a+N+1);
        int ans=0;
        for(int i=K;i<=N;i++){
            ans+=a[i]-a[i-K+1];
        }
        cout<<"Case #"<<casen<<": "<<ans<<endl;
        casen++;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值