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. 【Alpha版本】冲刺阶段 - Day6 - 乘风

    今日进展 袁逸灏:1.实现了碰撞的判定:2.代码规范化:3.解决了项目基本代码.(7h) 刘伟康:补充了上次未完成的任务,即检查代码规范,增加AS规范并整理上传至码云.除此之外,学习了部分 Andro ...

  2. alpha-咸鱼冲刺day8

    一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 正在进行页面整合.然后还有注册跟登陆的功能完善-- 四,问题困难 数据流程大概是搞定了.不过语法不是很熟悉,然后还有各种判定. ...

  3. Alpha冲刺置顶随笔

    项目名称:城市安全风险管控系统 小组成员: 张梨贤.林静.周静平.黄腾飞 Alpha冲刺随笔 Alpha冲刺Day1:http://www.cnblogs.com/linlkg/p/7896980.h ...

  4. Linux下硬盘分区

    1  fdisk -l查看硬盘及分区信息 我的系统(Archlinux)下的命令效果如下: 由上面的图片可以得知该系统只挂载了1个硬盘,命名为sda,其有2个主分区,sda1和sda2,至于为什么这么 ...

  5. GPUImage滤镜效果翻译

    #import"GPUImageBrightnessFilter.h"//亮度 #import"GPUImageExposureFilter.h"//曝光 #i ...

  6. Linux 磁盘和文件管理系统 文件打包解压备份 VIM、VI编辑器

  7. php的api及登录的权限验证

    类,库,接口(APi),函数,这些概念都是根据问题规模的大小来界定的.一个很小的问题肯定没有必要写成一个库,只需要写几句话就行了. 但是比如一个登录验证,这个功能很强大,很通用,可能前台后台都需要用到 ...

  8. emqtt 试用(三)mqtt 知识

    一.概念 MQTT 协议客户端库: https://github.com/mqtt/mqtt.github.io/wiki/libraries 例如,mosquitto_sub/pub 命令行发布订阅 ...

  9. GIT入门笔记(5)- 创建版本库

    版本库又名仓库,英文名repository,可以简单理解成一个目录, 这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻 ...

  10. 新概念英语(1-25)Mrs. Smith's Kitchen

    What colour is the electric cooker? Mrs. Smith's Kitchen is small. There is a refrigerator in the ki ...