题目链接:点我点我

Problem Statement

Snuke has a blackboard and NN candies.
The tastiness of the ii-th candy is aiai.

He will repeat the operation below until he has no more candy.

  • Choose one or two of his candies and eat them (of course, they disappear). Then, write on the blackboard the total tastiness of the candies he has just chosen.

Snuke wants to minimize X−YX−Y, where XX and YY are the largest and smallest values written on the blackboard, respectively.
Find the minimum possible value of X−YX−Y.

Constraints

  • All values in input are integers.
  • 1≤N≤50001≤N≤5000
  • −109≤ai≤109−109≤ai≤109

Input

Input is given from Standard Input in the following format:

NN
a1a1 a2a2 ⋯⋯ aNaN

Output

Print the minimum possible value of X−YX−Y, where XX and YY are the largest and smallest values written on the blackboard, respectively.


Sample Input 1

3
1 2 4

Sample Output 1

1
  • One optimal sequence of operations is to eat the candies with the tastinesses of 11 and 22 in the first operation, and then eat the candies with the tastiness of 44 in the second operation.

Sample Input 2

2
-100 -50

Sample Output 2

0
  • It is optimal to eat both candies with the tastiness of −100−100 and −50−50 in the first operation.

Sample Input 3

20
-18 31 -16 12 -44 -5 24 17 -37 -31 46 -24 -2 11 32 16 0 -39 35 38

Sample Output 3

13

题意

给出 n 个数,每个数最多可以和另一个数结合(相加)而变成一个新数,当然也可以不操作,问最后序列中最大数-最小数的最小值是多少


题解

这个题目的官方题解给的太好了

首先很容易想到,要想最小化 \(maxx-minn\) 必须要缩小序列中所有数的 \('\)距离 \('\)

假设一个序列从小到大排序依次为

\[a_1,a_2,a_3......a_i,a_{i+1},......a_n
\]

再假设 \(i\) 之前的数都是负数,且正数的个数多于负数的个数

那么

\[a_1+a_n,a_2+a_{n-1}.....a_{i-1}+a_{n-i+2}
\]

这些数之间的 \('\)距离\('\) 没法被缩小了

剩下的数还有

\[a_i,a_{i+1},a_{i+2}.....a_{j}
\]

这些数都是正数,而他们没有一个构造方法,有可能两个较小数相加变为一个较大数,也有可能不与其他数结合

其实这两种情况都是同一种情况,不与其他数结合不就是与 \(0\) 结合嘛。

所以算法复杂度 \(O(N^2)\)


以下代码采用 O(N2logN) 的方法,时间与正解相差 40 倍

const int N=3e5+5;

    ll n, m, _;
int i, j, k;
//ll a[N];
vector<ll> v; ll calc(int sz)
{
int l = 0, r = sz - 1;
ll maxx = -1e18, minn = 1e18;
while(r >= l){
if(l == r){
minn = min(minn, v[l]);
maxx = max(maxx, v[l]);
break;
}
else{
minn = min(minn, v[r] + v[l]);
maxx = max(maxx, v[r] + v[l]);
}
r--;
l++;
}
return maxx - minn;
} signed main()
{
//IOS;
while(~sd(n)){
rep(i, 0, n - 1) sll(_), v.pb(_);
sort(all(v));
ll minn = calc(n);
rep(i, 1, n - 1){
v.pb(0);
sort(all(v));
minn = min(minn, calc(n + i));
}
pll(minn);
v.clear();
}
//PAUSE;
return 0;
}

AtCoder Regular Contest 121 D - 1 or 2的更多相关文章

  1. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  2. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

  3. AtCoder Regular Contest 092

    AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...

  4. AtCoder Regular Contest 093

    AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...

  5. AtCoder Regular Contest 094

    AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...

  6. AtCoder Regular Contest 095

    AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...

  7. AtCoder Regular Contest 102

    AtCoder Regular Contest 102 C - Triangular Relationship 题意: 给出n,k求有多少个不大于n的三元组,使其中两两数字的和都是k的倍数,数字可以重 ...

  8. AtCoder Regular Contest 096

    AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...

  9. AtCoder Regular Contest 097

    AtCoder Regular Contest 097 C - K-th Substring 题意: 求一个长度小于等于5000的字符串的第K小子串,相同子串算一个. K<=5. 分析: 一眼看 ...

随机推荐

  1. Java代理简述

    1.什么是代理? 对类或对象(目标对象)进行增强功能,最终形成一个新的代理对象,(Spring Framework中)当应用调用该对象(目标对象)的方法时,实际调用的是代理对象增强后的方法,比如对功能 ...

  2. Workerman:PHP的socket框架

    hi,我们今天来讲讲Workerman,什么是Workerman呢? 看看官网上的介绍 Workerman是一款开源高性能异步PHP socket框架.支持高并发,超高稳定性,被广泛的用于手机app. ...

  3. 群晖 创建nfs 共享文件夹 k8s 使用

    1) 打开控制面板 2) 打开共享文件夹 3) 新增共享文件夹 4) 基本信息配置 2) 3) 4) 5) 点完确定,应该会退出,继续选中刚才创建的,点编辑 2) 3) 5)返回主页面,点击file ...

  4. Python基础之容易忘记的地方

    (1)编译型与解释型语言区别: 编译型:一次性,把所有代码编译成机器能识别的二进制码,再运行 代表语言:c,c++ 优点: 执行速度块 缺点: 开发速度慢,调试周期长 解释型:代码从上到下一行一行解释 ...

  5. 使用Viper读取Nacos配置(开源)

    使用Viper读取Nacos配置(开源) 一.前言 目前Viper支持的Remote远程读取配置如 etcd, consul:目前还没有对Nacos进行支持,本文中将开源一个Nacos的Viper支持 ...

  6. 通过钉钉网页上的js学习xss打cookie

    做完了一个项目,然后没啥事做,无意看到了一个钉钉的外部链接: 题外话1: 查看源码,复制其中的代码: try { var search = location.search; if (search &a ...

  7. 解决Android加固多进程ptrace反调试的思路整理

    本文博客链接:http://blog.csdn.net/qq1084283172/article/details/53613481 一.Android多进程反调试的原理代码 当ptrace附加目标进程 ...

  8. 一个不错的过TP思路,转载CSDN

    也许大家也是研究腾讯游戏的爱好者,对腾讯的游戏都有过这样的体会  例如OD与CE无法进行如以下操作: 无法附加进程, 无法打开进程, 游戏进程被隐藏无法在工具中查看到,内存无法读取代码  内存修改后游 ...

  9. Linux-鸟菜-6-文件与目录的 默认权限、隐藏权、特殊权限

    Linux-鸟菜-6-文件与目录的 默认权限.隐藏权.特殊权限 除了基本r,w,x权限外,在Linux还可以设定其他系统隐藏属性,可以用chattr来设定,和lsattr来查看,但注意一点,CentO ...

  10. 【编译原理】求First和Follow

    写这篇博客的原因,是因为考试前以为自己已经将这个问题弄清楚了,但是,考试的时候,发现自己还是不会,特别是求follow集合.虽然考试结束了,希望屏幕前的你,可以真正理解这个问题. 码字和做视频都不容易 ...