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的更多相关文章

  1. 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 ...

随机推荐

  1. 转: javascript模块加载框架seajs详解

    javascript模块加载框架seajs详解 SeaJS是一个遵循commonJS规范的javascript模块加载框架,可以实现javascript的模块化开发和模块化加载(模块可按需加载或全部加 ...

  2. android HTTP发送及MD5加密收集

    发送部分: public void MyFunction{ HttpClient httpclient = new DefaultHttpClient(); //你的URL HttpPost http ...

  3. 部署nginx+rsyslog补丁

    nginx 配置: user nginx; worker_processes 1; syslog local5 nginx; error_log /var/log/nginx/nginx_error. ...

  4. delphi关于文件操作集锦

        关于文件操作集锦 取得该快捷方式的指向EXE关键词:快捷方式 LNK unit Unit1; interface usesWindows, Messages, SysUtils, Varian ...

  5. JAVA訪问URL

    JAVA訪问URL: package Test; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...

  6. 注册表:无法打开 XXX 由于某个错误无法打开该密钥。详细信息:拒绝访问

    错误原因:没有注册表用户权限. 正确添加用户权限的步骤如下:(跟着图片步骤) 右击该项,权限: 选中想要添加为当前所有者的用户后,点击应用.如果没用户显示,可以从“其他用户或组”中添加进来. 权限添加 ...

  7. Objective-c 内存管理

    与 C 有一点类似,oc  需要使用 alloc 方法申请内存.不同的是,c 直接调用 free 函数来释放内存,而 oc 并不直接调用 dealloc 来释放.整个  oc 都使用对象引用,而且每一 ...

  8. 建造者模式->代码示例

    <?php interface Builder{ public function head(); public function body(); public function foot(); ...

  9. GPL协议

    GPL-General Public License 它是自由软件许可(Free Software Licence),保证最终使用者能够自由的引用,学习,拷贝,甚至修改软件.在理解这之前需要了解什么是 ...

  10. ubuntu vim终端编辑命令

    一. VIM高亮 进入vim后,在普通模式下输入如下命令,开启php代码高亮显示   :syntax enable   :source $VIMRUNTIME/syntax/php.vim   二. ...