题目链接:点我点我

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. C#如何优雅的多表读取

    关键词:C#.SqlDataReader.IDataReader.NextResult().Read(). Load().Dapper.多表,方便索引和搜索 最近有个需求,需要读一下模具系统的模具信息 ...

  2. 软件篇-01-为Jetson TX2扫清科研的障碍

    建议用vryL,GUI界面,功能更全,支持订阅. https://github.com/ShieldQiQi/vryL​github.com 下面的内容不用看了,除非你更喜欢命令行界面.   To r ...

  3. Mysql Char 和 Varchar的区别

    CHAR和VARCHAR都是字符串类型,它们的具体区别为: 长度大小区别: CHAR(M)定义的列的长度为固定的,M取值可以为0-255之间: VARCHAR(M)定义的列的长度为可变长,M取值可以为 ...

  4. 【JVM】Java8 和 Java7中JVM内存模型有什么区别

    规范和实现 针对Java虚拟机的实现有专门的<Java虚拟机规范>,在遵守规范的前提下,不同的厂商会对虚拟机进行不同的实现. 就好比开发的过程中定义了接口,具体的接口实现大家可以根据不同的 ...

  5. 在Android的App中动态的加载Java类

    原文的地址:http://yenliangl.blogspot.com/2009/11/dynamic-loading-of-classes-in-your.html 我正在编写一个应用程序能够加载别 ...

  6. hdu1556 线段树段更新(简单题)

    题意: N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个 ...

  7. Python中数据类型的转换

    bytes<-->str a="hello" #str字符型 #str转换为bytes类型 b=a.encode("utf-8") 或 b=byte ...

  8. Windows核心编程 第五章 作业(下)

    5.4 查询作业统计信息 前面已经介绍了如何使用 Q u e r y I n f o r m a t i o n J o b O b j e c t函数来获取对作业的当前限制信息.也可以使用它来获取关 ...

  9. Windows PE 第十二章 PE变形技术

    PE变形技术 这章东西太多,太细了.这里我只记录了一些重点概念.为后面学习做铺垫. PE变形:改变PE结构之后,PE加载器依然可以成功加载运行我们的程序. 一 变形常用技术: 结构重叠技术.空间调整技 ...

  10. 【python】Leetcode每日一题-最长公共子序列

    [python]Leetcode每日一题-最长公共子序列 [题目描述] 给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度.如果不存在 公共子序列 ,返回 0 . ...