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 ...
随机推荐
- 转: requestAnimationFrame,Web中写动画的另一种选择
HTML5/CSS3时代,我们要在web里做动画选择其实已经很多了: 你可以用CSS3的animattion+keyframes; 你也可以用css3的transition; 你还可以用通过在canv ...
- Pro/E 5.0安装图解教程(也适用于Creo Elements/Pro 5.0)
安装前必读:☆ 本教程适用于 32 位 proe 5.0 M010,M020,M030,M040,M050,M060 过程完全一样:☆ 本教程用于 64 位 proe 5.0 M010,M020,M0 ...
- sqlite 查询数据 不用回调
int main( void ){ sqlite3 *db=NULL; char *zErrMsg = 0; int rc; //打开数据库连接 rc = sqlite3 ...
- AIDE支持实时错误检查、代码重构、代码智能导航、生成APK
AIDE是一个Android Java集成开发环境,可以在Android系统内进行Android软件和游戏的开发.它不仅仅是一个编辑器,而是支持编写-编译-调试运行整个周期,开发人员可以在Androi ...
- cocos2dx CCLayerColor和CCLayerColor
在cocos2dx中,默认的CCLayer背景是黑色的,有些时候需要特殊的Layer,所以cocos2dx中提供了这两种Layer CCLayerColor是可以改变背景色的Layer,示例如下: C ...
- Html 语法学习笔记二
1.图像标签(<img>)和源属性(Src) 在 HTML 中.图像由 <img> 标签定义. <img> 是空标签,意思是说,它仅仅包括属性,而且没 ...
- Android布局_LinearLayout布局
一.LinearLayout 布局,类似于一个盒子 1. 主要属性有: (1)android:orientation 设置LinearLayout容器布局组件的方式:要么按行要么按列.只能取值:hor ...
- ios之TableViewCell重用机制避免反复显示问题
常规配置例如以下 当超过tableView显示的范围的时候 后面显示的内容将会和前面反复 // 这样配置的话超过页面显示的内容会反复出现 - (UITableViewCell *)tableView: ...
- js 去除字符串左右两端的空格
<script type="text/javascript"> function trim(str){ //删除左右两端的空格 return str.repl ...
- ##DAY5 UIControl及其子类
##DAY5 UIControl及其子类 #pragma mark ———————UIControl——————————— UIControl初识: 1)UIControl是有控制功能的视图(比如UI ...