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

  • 1.题目描述
    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.

  • 2.题目分析
    关键就是着weakness的计算公式,它计算的是所有a[i+k-1]-a[i]的值,同时a[i+k-1](最大)-a[i](最小),所以做法就是先递增排序,累加所有a[i+k-1]-a[i]的值,输出结果就是答案
  • 3.代码如下
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100;
char str1[2000], s2[2000];
int dp[105];
int ap[105], bp[105];
const int MAXN= 1000005;
int a[10005];
int main()
{
int T,d=1;
scanf("%d",&T);
while(T--)
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int res=0;
for(int i=0;i+k-1<n;i++)
{
res+=a[i+k-1]-a[i];
}
printf("Case #%d: %d\n",d++,res);
}
return 0;
}

第十四届浙江财经大学程序设计竞赛重现赛--A-A Sad Story的更多相关文章

  1. “浪潮杯”第九届山东省ACM大学生程序设计竞赛重现赛 C-Cities

    题目描述:There are n cities in Byteland, and the ith city has a value ai. The cost of building a bidirec ...

  2. 牛客网 湖南大学2018年第十四届程序设计竞赛重现赛 A game

    链接:https://www.nowcoder.com/acm/contest/125/A来源:牛客网 Tony and Macle are good friends. One day they jo ...

  3. 2018年第十届ACMICPC四川省大学程序设计竞赛

    ..拿金了 没给学校丢脸 A ....SB题啊 比赛的时候都没看 裸的一个bitset前缀和 先开一个1e4*1e4的二维bitset数组 初始第i个数组的值为1 << i (即B[i]= ...

  4. 长安大学第四届ACM-ICPC“迎新杯”程序设计竞赛-重现赛 G - 彩虹岛套娃

    题目描述 俄罗斯套娃是俄罗斯特产的木制玩具,一般由多个一样图案的空心木娃娃一个套一个组成,最多可达十多个,通常为圆柱形,底部平坦可以直立.颜色有红色,蓝色,绿色,紫色等.最普通的图案是一个穿着俄罗斯民 ...

  5. 长安大学第四届ACM-ICPC“迎新杯”程序设计竞赛-重现赛 H - 圣诞节糖果

    题目描述 圣诞节临近,彩虹岛的黑心商人

  6. 长安大学第四届ACM-ICPC“迎新杯”程序设计竞赛-重现赛 F - 打铁的箱子

    题目描述 作为彩虹岛上最擅长打铁的人,

  7. 长安大学第四届ACM-ICPC“迎新杯”程序设计竞赛-重现赛 D - 新卡片游戏

    题目描述

  8. HDU 6467 简单数学题 【递推公式 && O(1)优化乘法】(广东工业大学第十四届程序设计竞赛)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6467 简单数学题 Time Limit: 4000/2000 MS (Java/Others)    M ...

  9. HDU 6464 免费送气球 【权值线段树】(广东工业大学第十四届程序设计竞赛)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6464 免费送气球 Time Limit: 2000/1000 MS (Java/Others)    M ...

随机推荐

  1. Web开发HTTP中URI和URL的情感纠葛

    作者:郭无心链接:https://www.zhihu.com/question/21950864/answer/66779836来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  2. 个人MySQL股票数据库的建立日记

    #!/usr/bin/python# -*- coding: UTF-8 -*- import tushare as tsfrom sqlalchemy import create_engine co ...

  3. Python爬虫《爬取get请求的页面数据》

    一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...

  4. 01_Quartz基础结构

    [各种任务调度的使用场景] 论坛每天凌晨统计论坛用户的积分排名. 论坛每半个小时生成精华文章. 每隔30分钟对锁定过期的用户解锁. 每月1号统计上个月各部门的业务数据. [Quartz 简介] Qua ...

  5. GridView简单使用

    如图是效果图 今天看到看到这个代码发现一个问题  就是我的listView的布局不对  我的GridView的 android:layout_height="wrap_content&quo ...

  6. OpenSSL.Net 在生产环境中无法正常加载的原因分析与解决 z

    http://blog.csdn.net/wangjia184/article/details/6990098 http://www.openssl.org/ 在本地测试好好的代码部署到生产环境后,遇 ...

  7. 使用WindowsService为宿主实装WCF 服务

    1. 写WCF服务   创建一个接口类,与一个实现类.接口类上打上[ServiceContract]标签,需要暴露的服务方法上打上[OperationContract] (注意:增加System.Se ...

  8. 如何使用Kubernetes的configmap通过环境变量注入到pod里

    在Kubernetes官网里,有这样一篇文章,提到了Kubernetes里的一个最佳实践就是把应用代码同配置信息分开,一种方式就是使用Kubernetes 1.2里引入的configmap概念. ht ...

  9. python接口测试-项目实践(五) 实际结果与预期结果对比之 接口对比

    五 与开发接口对比 1 分别将三个接口封装成三个函数,传入接口参数,返回提取并处理后的各字段. 拼接字符串作为单独的函数. def api_1(code): 发送请求获取响应 提取响应数据 响应数据转 ...

  10. Jmeter入门17 获取时间点前后一定间隔的时间 __timeShift()

    接口获取时间点前后一定间隔的时间函数: __timeShift(时间格式, 特定时间点(缺省当前时间),时间间隔,地区格式(默认),变量名( 可不填,填写后其他地方用${变量名}引用 )) 举例: 1 ...