sicily9162. RAZLIKA
9162. RAZLIKA
限制条件
时间限制: 2 秒, 内存限制: 256 兆
题目描述
Mirko's newest math homework assignment is a very difficult one! Given a sequence, V, of N integers,
remove exactly K of them from the sequence. Let M be the largest difference of any two remaining
numbers in the sequence, and m the smallest such difference. Select the K integers to be removed from
V in such a way that the sum M + m is the smallest possible. Mirko isn't very good at math, so he has
asked you to help him!
输入格式
The first line of input contains two positive integers, N (3 ≤ N ≤ 1 000 000) and K (1 ≤ K ≤ N - 2).
The second line of input contains N space-separated positive integers – the sequence V (-5 000 000 ≤
Vi ≤ 5 000 000).
输出格式
The first and only line of output must contain the smallest possible sum M + m.
样例输入
5 2
-3 -2 3 8 6
样例输出
7
题目来源
2013年每周一赛第10场/COCI 2013.1
题意:给你一串数字,叫你先删除k个然后再找出剩下的点中最长距离和最小距离和最小
看一下题的数据量3 ≤ N ≤ 1 000 000,吓死人,这些数据要在2s内完成,绝对不能两层循环以上
刚开始还没什么想法,后来隔了几天在想一下。先排好序。然后删除的时候绝对不能使从中间的数删除的
只有从两边删除。因为如果是从中间删除我一定可以找到从两边删除会比他更优。所以就从头到尾循环
每次更新最长和最短距离之和就ok了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int array[1000006];
int p[1000006];
int Min(int a,int b)
{
return a>b?b:a;
}
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
for(int i=0;i<n;i++)
scanf("%d",&array[i]); sort(array,array+n);
for(int i=0;i<n-1;i++)
p[i] = array[i+1]-array[i];
int c = n-k;
int l;
int last = -1;
int min = 20000002;
for(int j=0;j<k;j++)
{
l = array[c+j-1] - array[j];
if(last>=j && last<c+j-1)
{
if(p[last]>=p[c+j-2])
last = c+j-2;
}
else
{
int var = 20000001;
for(int k=j;k<c+j-1;k++)
if(p[k]<=var)
{
last = k;
var = p[k];
}
}
min = Min(min,l+p[last]);
} printf("%d\n",min);
}
return 0;
}
sicily9162. RAZLIKA的更多相关文章
- Varnost slovenskih GSM omrežij III
V torek smo pisali tudi o tem, da Si.Mobil v svojem omrežju dovoli uporabo A5/0 (nešifriranega preno ...
随机推荐
- RedHat升级内核成功
升级前 uname -aLinux localhost.localdomain 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x8 ...
- android和Vitamio使用比较
在开始接触udp组播的时候先使用的Vitamio,播放时候声音卡顿 画面也会出现卡顿,后来又使用了VLC,画面挺好,,但是声音卡顿.最后不断测试发现是由于设备底层驱动处理视频部分有问题,导致程序播出的 ...
- 教你看懂C++类库函数定义之三---_stdcall
一切从一个C++ 类库头文件开始,现在在做一个C++的项目,期间用到一个开源的界面库DUILib(类似MFC),这个东西还不错能很容易的写出漂亮的界面,比如QQ的界面,可以去下载下来研究研究,地址:h ...
- Evernote Clearly :: Firefox 附加组件
Evernote Clearly :: Firefox 附加组件 Evernote Clearly 10.1.1.2 作者: Evernote Evernote Clearly 可使博客贴文.文章和网 ...
- nbtstat 查询IP地址对应的计算机名称
使用命令nbtstat -a ipaddress即可,例如:nbtstat -a 192.168.1.2.
- AngularJS Directive 学习笔记
指令 Directive 指令要点 大漠老师的教学节点 解析最简单的指令 hello: 匹配模式 restrict 解析最简单的指令 hello: template.tempmlateUrl.$tem ...
- sql update left join 更新,字段内容分隔符提取
UPDATE a SET [Province] = parsename(replace([FullName],'-','.'),2) from [dbo].[T_B_Emp] a left join ...
- AsyncSocket的长连接使用
使用背景:需要跟服务器长期保持连接进行即时通讯:还有在跟智能硬件建立实时链接进行同步智能硬件的状态等,最近我就做项目就碰到需要实时更新智能硬件的状态(比如智能硬件的电量,以及其它工作状态),跟智能硬件 ...
- ##DAY4 事件的基本概念、触摸的基本概念、响应者链、手势
##DAY4 事件的基本概念.触摸的基本概念.响应者链.手势 #pragma mark ———————事件的基本概念 ——————————— 事件的基本概念: 1)事件是当用户的手指触击屏幕及在屏幕 ...
- codeforces 622C. Optimal Number Permutation 构造
题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间. ...