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. 如何在jenkins上新建一个项目及其简单配置

    1.首先,点击[新建]进入选择页面,如下图(一般选择"构建一个自由风格的软件项目")     2.填好项目名称后,点击ok,跳转至如下页面,可以在这个页面进行项目的配置(包括拉源码 ...

  2. 基于协程的Python网络库gevent

    import gevent def test1(): print 12 gevent.sleep(0) print 34 def test2(): print 56 gevent.sleep(0) p ...

  3. border 三角形 有边框的 东西南北的 气泡三角形

    链接地址:http://www.cnblogs.com/blosaa/p/3823695.html

  4. 16-TypeScript装饰器模式

    在客户端脚本中,有一个类通常有一个方法需要执行一些操作,当我们需要扩展新功能,增加一些操作代码时,通常需要修改类中方法的代码,这种方式违背了开闭的原则. 装饰器模式可以动态的给类增加一些额外的职责.基 ...

  5. Centos6.7的在虚拟机virulBox下的lamp平台的搭建

    实验环境: linux:小甲鱼带你学C语言,带你飞的提供的体积比较小的centos6.7和virtualBox mysql,apahce,php是燕十八在Linux基础进阶中提供的安装方式: 结果,安 ...

  6. 验证码进阶(TensorFlow--基于卷积神经网络的验证码识别)

    本人的第一个深度学习实战项目,参考了网络上诸多牛人的代码,在此谢过,因时间久已,不记出处,就不一一列出,罪过罪过. 我的数据集是我用脚本在网页上扒的,标签是用之前写的验证码识别方法打的.大概用了400 ...

  7. Win10下, TortoiseGit安装及配合Gitee使用完整版

    Windows10下, TortoiseGit的安装及使用, 并配合Gitee码云使用! 1) 安装TortoiseGit 官网, 32位, 64位, 自选 https://tortoisegit.o ...

  8. python利用递归函数输出嵌套列表的每个元素

    1.先用 for 循环取. for item in l: if isinstance(item ,list): for newitem in item: print(newitem) else: pr ...

  9. maven入门(6)maven的生命周期

    1. 三套生命周期     Maven拥有三套相互独立的生命周期,它们分别为clean,default和site. 每个生命周期包含一些阶段,这些阶段是有顺序的,并且后面的阶段依赖于前面的阶段,用户和 ...

  10. global文件中的application_start方法中做: 定时器

    <%@ Application Language="C#" %> <%@ import Namespace="System.Data" %&g ...