题目链接:点我点我

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. WinDBg定位asp.net mvc项目异常崩溃源码位置

    项目介绍:asp.net mvc + angular +iis(windows)+windows server 系统莫名崩溃 最近有个系统默认奇妙崩溃50x,服务整体变成无响应,当运维告知我只有重启应 ...

  2. 共享内存多进程key value操作

    这个方法的优势是多进程,劣势也很明显,只允许操作key为uint32 value为uint32的数据.   https://www.cnblogs.com/dearplain/p/11578588.h ...

  3. 获取全局描述符表GDT的内容

    /stdfx.h文件 //Ring0环的程序 //测试环境VS2005 #ifndef _WIN32_WINNT // Allow use of features specific to Window ...

  4. 路由器逆向分析------MIPS系统网络的配置(QEMU)

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/69378333 MIPS系统网络的配置  使用QEMU 模拟正在运行的MIPS系统并 ...

  5. 基于Xposed Hook实现的Android App的协议算法分析小工具-CryptoFucker

    本文博客地址:https://blog.csdn.net/QQ1084283172/article/details/80962121 在进行Android应用的网络协议分析的时候,不可避免涉及到网络传 ...

  6. 使用SSH端口做端口转发以及反向隧道

    目录 SSH做本地端口转发 SSH做反向隧道(远程端口转发) 用autossh建立稳定隧道 SSH开启端口转发需要修改 /etc/ssh/sshd_config配置文件,将 GatewayPorts修 ...

  7. Linux-鸟菜-2-主机规划与磁盘分区

    Linux-鸟菜-2-主机规划与磁盘分区 开机流程: 1. BIOS:開機主動執行的韌體,會認識第一個可開機的裝置: 2. MBR:第一個可開機裝置的第一個磁區內的主要開機記錄區塊,內含開機管理程式: ...

  8. 3 Java概述

    java三大版本 javase:标准版(桌面程序,控制台开发) javame:嵌入式开发(手机,家电)目前陨落 javaee:企业级开发(web端..) JDK和JRE 定义 JDK是开发工具包 Jr ...

  9. windows下使用dos命令手工与ntp服务器同步系统时间

    管理员模式的命令窗口 net stop w32time &w32tm /unregister &w32tm /register &net start w32time & ...

  10. Pycharm集成码云,图文手把手教学!

    Pycharm集成码云 码云(http://gitee.com)是开源中国推出的代码托管平台,支持 Git 和 SVN,提供免费的私有仓库托管 可以通过码云保管你的代码,每次修改完代码提交,就是一个版 ...