链接:https://www.nowcoder.com/acm/contest/89/A
来源:牛客网

时间限制: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<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
const int maxn=1e5+10;
#define ll long long int
int a[maxn];
int main()
{int T;scanf("%d",&T);int cs=0;while(T--){int m,n;cs++;scanf("%d%d",&m,&n);for(int i=1;i<=m;i++){scanf("%d",&a[i]);}sort(a+1,a+1+m);ll ans=0;for(int i=n;i<=m;i++){ans+=a[i];ans-=a[i-n+1];}printf("Case #%d: ",cs);printf("%lld\n",ans);}return 0;
}

第十四届浙江财经大学程序设计竞赛 A A Sad Story【贪心】相关推荐

  1. 第十四届浙江财经大学程序设计竞赛重现赛 ——A

    A.  A sad story 链接:https://www.nowcoder.com/acm/contest/89/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ ...

  2. 华为杯大学生计算机软件大赛,关于举办2018年西安电子科技大学程序设计网络赛暨第十六届“华为杯”大学生程序设计竞赛的通知...

    各学院: 程序设计是大学生运用计算机充分展示自己分析问题和解决问题能力的一个重要途径,对于培养大学生实践能力.团队意识.创新意识.顽强意志和综合素质具有显著作用和效果.为了推动这项创新性素质教育活动的 ...

  3. 第十七届中国计量大学程序设计竞赛 D Dessert Time

    第十七届中国计量大学程序设计竞赛 D Dessert Time 题目描述 The party began, the greasy uncle was playing cards, the fat ot ...

  4. 第十四届全国大学生智能车竞赛竞赛技术报告下载链接

    第十四届智能车竞赛技术报告下载链接 01 下载报告   今天上午,看到有同学询问关于十四届智能车竞赛技术报告下载的询问. 实际上,之前第十四届的技术报告在百度上有, 只是没有能够提供下载链接. 由于第 ...

  5. 第九届浙江财经大学信息学院大学生程序设计竞赛

    A:水水更健康 时间限制: 1 Sec   内存限制: 128 MB [ 提交][ 状态][ 讨论版] 题目描述 zhourist是zufeoj的Rank2.他决定向Rank1发起进攻号角.于是他要刷 ...

  6. 牛客网 第十七届中国计量大学程序设计竞赛(同步赛)(重现赛)B题 Broken Pad 暴力+思维

    题意: 给你两个01串,经过两种操作,1.直接让第一串经过操作变成目标串:2.可以点击空白处,即0的地方,使得操作串全部清空为0串,再变为目标串:最终比较两种方式,哪种需更少步骤,输出每步点击的位置. ...

  7. 浙江财经大学第十四届程序设计竞赛命题感想

    题目和题解点这里:http://www.cnblogs.com/zufezzt/p/8648892.html 24号,学校一年一度的程序设计竞赛结束了,这一次是我和两个学弟命题的,还有一些人进行了题面 ...

  8. 浙江财经大学计算机学院,第十四届省本科院校计算机系(学院)系主任(院长)论坛在我校举行...

    本网讯 5月5日至6日,第十四届浙江省本科院校计算机系(学院)系主任(院长)论坛在我校下沙校区举行.来自省内外兄弟院校的计算机学院院长.计算机系系主任.计算机领域的专家学者和业界精英共计100余人齐聚 ...

  9. 第十四届蓝桥杯大赛软件赛省赛 C/C++ 大学 B 组

    第十四届蓝桥杯大赛软件赛省赛 C/C++ 大学 B 组 注意!!!!!!!!!!这篇题解为赛时的个人做法,不代表是正确的,仅供参考. 更新:思路上应该都对,很多题都有细节错误,代码不用看了,太久没敲代 ...

最新文章

  1. 零基础怎么学习Java?
  2. HBuilder X格式化美化代码快捷键
  3. 快速排序--Python实现
  4. 去除 position:fixed 抖动的方法
  5. 业界资讯:adobe 技术开发中心 放出游戏单元
  6. android开发标签栏应该设置多少,Android开发笔记(十九)底部标签栏TabBar
  7. keras实现DCGAN生成mnist原代码
  8. [渝粤教育] 南京邮电大学 Python语言程序设计基础 参考 资料
  9. 稳压管和TVS管的工作原理
  10. 丰桥自助打印顺丰面单
  11. 阿里云企业飞天会员是什么,如何申请?
  12. [转]批处理详细教程
  13. python中的tenacity 的简单使用
  14. 4.3 CPU性能侦测
  15. 如何做机器学习模型质量保障及模型效果评测
  16. oracle cmd窗口 命令行导入*.dmp文件
  17. 2016清华计算机学院复试名单,2016清华复试名单自动化名单
  18. 【嵌入式】网络编程_pthread_fork
  19. 二维数组 string[,]
  20. C#在图片框中显示一幅图片及其信息

热门文章

  1. visio的vsd文件转eps图流程
  2. CTFHub----RCE
  3. 计算机系统启动密码怎么设置在哪里,如何设置电脑开机密码呢?教你设置系统登陆密码...
  4. 求整数的位数及各位数字之和
  5. 投:分众传媒相关文章
  6. linux安装TensorFlow-GPU版本 非常详细安装必看
  7. 【Matlab】M文件编写PID调节传递函数
  8. 快速幂取模——Pupu(HDU 3003)
  9. linux mt命令,Cheat—— 给Linux初学者和管理员一个终极命令行备忘单
  10. TCP通信中一方关闭socket,另一方被强制退出(SIGPIPE)