Description

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]).

Input

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

Output the minimum value of integer S.

Sample Hint

x[1]=-5, y[1]=4,

x[2]=10, y[2]=10.

比赛中唯一让人感到美好的题目
按从小到大排序
定义一个区间大小为最大减最小
那么题目就是要把n个数分为k个区间,使和最小
贪心,一开始只分出1~n一个区间,后面将k-1个相邻差最大的值减去
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,k,c[],d[];
long long ans;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{int i;
cin>>n>>k;
for (i=;i<=n;i++)
{
scanf("%d",&c[i]);
}
sort(c+,c+n+);
ans=c[n]-c[];
for (i=;i<n;i++)
d[i]=c[i+]-c[i];
sort(d+,d+n,cmp);
for (i=;i<=k-;i++)
{
ans-=d[i];
}
cout<<ans;
}

hihoCoder 1595 : Numbers的更多相关文章

  1. hihoCoder挑战赛31

    #1595 : Numbers 时间限制:8000ms 单点时限:1000ms 内存限制:256MB 描述 给定n个整数常数c[1], c[2], ..., c[n]和一个整数k.现在需要给2k个整数 ...

  2. hihoCoder #1349 Nature Numbers

    题目大意 考虑自然数构成的序列 $a$:$01234567891011\dots$,序列下标从 $0$ 开始,即 $a_0 =0, a_1 = 1$ . 求 $a_n$($0\le n\le 10^{ ...

  3. hihoCoder 1432 : JiLi Number(吉利数)

    hihoCoder #1432 : JiLi Number(吉利数) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 Driver Ji l ...

  4. [HihoCoder] Highway 高速公路问题

    Description In the city, there is a one-way straight highway starts from the northern end, traverses ...

  5. hihocoder 1873 ACM-ICPC北京赛区2018重现赛 D Frog and Portal

    http://hihocoder.com/problemset/problem/1873 时间限制:1000ms 单点时限:1000ms 内存限制:512MB 描述 A small frog want ...

  6. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  9. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

随机推荐

  1. C语言作业--函数

    一.PTA实验作业 题目1: 400-499 中4出现的次数 1. 本题PTA提交列表 2. 设计思路 一.main函数 1.函数声明int fun(int x) 2.定义变量i,k,i表示输入的值, ...

  2. TensorFlow问题“The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.”

    出现的问题: 在使用TensorFlow跑官方教程例子时报以下warning: 虽程序能正常跑出结果,但作为一名强迫症患者对此很是不爽,于是查找资料找到隐藏该warning的解决办法. 解决办法: 在 ...

  3. Scrum 冲刺 第三日

    Scrum 冲刺 第三日 目录 要求 项目链接 燃尽图 问题 今日任务 明日计划 成员贡献量 要求 各个成员今日完成的任务(如果完成的任务为开发或测试任务,需给出对应的Github代码签入记录截图:如 ...

  4. "一不小心就火了"团队采访

    团队采访 一. 采访团队 团队:一不小心就火了 采访形式:线上问答 二.采访内容 你们是怎么合理地具体分配组员里的工作的?有些团队会出现个别组员代码任务很重,个别组员无所事事的情况,你们有什么有效的方 ...

  5. Telnet、SSH和VNC 区别

    Telnet Telnet是进行远程登录的标准协议,它是当今Internet上应用最广泛的协议之一.它把用户正在使用的终 端或计算机变成网络某一远程主机的仿真终端,使得用户可以方便地使用远程主机上的软 ...

  6. ESP8266 wifi 模块配置,Wechat+APP控制实现

    首先刷入安信可的AiCloud 2.0 SDK文件,AiCloud 2.0具体信息参见AiCloud 1.0 和AiCloud 2.0对比 APP见如下二维码下载. 1.安信可AiCloud 2.0 ...

  7. awk sed tr替换换行符为逗号,并合并为一行

    在群里看到的.记录以备用.  sed 帮助命令:http://man.linuxde.net/sed 文件里有如下行,我想将每行的回车符替换为逗号,并将所有行合并到一行,用awk或sed怎么写啊TOP ...

  8. Python内置函数(4)——min

    英文文档: min(iterable, *[, key, default]) min(arg1, arg2, *args[, key]) Return the smallest item in an ...

  9. python 单例模式的四种创建方式

    单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...

  10. RxJava系列7(最佳实践)

    RxJava系列1(简介) RxJava系列2(基本概念及使用介绍) RxJava系列3(转换操作符) RxJava系列4(过滤操作符) RxJava系列5(组合操作符) RxJava系列6(从微观角 ...