AtCoder Regular Contest 121 D - 1 or 2
题目链接:点我点我
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\) 必须要缩小序列中所有数的 \('\)距离 \('\)
假设一个序列从小到大排序依次为
\]
再假设 \(i\) 之前的数都是负数,且正数的个数多于负数的个数
那么
\]
这些数之间的 \('\)距离\('\) 没法被缩小了
剩下的数还有
\]
这些数都是正数,而他们没有一个构造方法,有可能两个较小数相加变为一个较大数,也有可能不与其他数结合
其实这两种情况都是同一种情况,不与其他数结合不就是与 \(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的更多相关文章
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- AtCoder Regular Contest 094 (ARC094) CDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...
- AtCoder Regular Contest 092
AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...
- AtCoder Regular Contest 093
AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...
- AtCoder Regular Contest 094
AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...
- AtCoder Regular Contest 095
AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...
- AtCoder Regular Contest 102
AtCoder Regular Contest 102 C - Triangular Relationship 题意: 给出n,k求有多少个不大于n的三元组,使其中两两数字的和都是k的倍数,数字可以重 ...
- AtCoder Regular Contest 096
AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...
- AtCoder Regular Contest 097
AtCoder Regular Contest 097 C - K-th Substring 题意: 求一个长度小于等于5000的字符串的第K小子串,相同子串算一个. K<=5. 分析: 一眼看 ...
随机推荐
- java中switch的用法
switch关键字对于多数java学习者来说并不陌生,由于笔试和面试经常会问到它的用法,这里做了一个简单的总结: 能用于switch判断的类型有:byte.short.int.char(JDK1.6) ...
- xPath,beautifulsoup和pyquery
一.XPath from lxml import etree html = etree.parse('html源代码',etree.HTMLPaser()) 1.节点的获取 a.html.xpath( ...
- 🍎
江湖中有一本练了就能天下无敌的葵花宝典,大家都想得到它.如果有一天葵花宝典被公开了,人人都有机会练,到底是好事还是坏事呢? 这会成为一个灾难. 因为一个人拥有时,练不练是一个人的事.大家都拥有,练不练 ...
- 991. Broken Calculator
On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...
- 【ElasticSearch】shards,replica,index之间的关系
1.index 包含多个shard ,在创建index的时候可以自定义shards和replica的数量 例如: 新增一个index,手动指定shard和replica的数量 PUT demo_ind ...
- 手脱UPX3.91壳(练习)
0x01 准备 OD UPX加壳程序 可以加壳的软件 0x02 给软件加壳 我找了半天发现winhex不错,而且是没壳的可以直接加壳 1.复制一份可执行文件 将赋值好的文件用UPX3.91加壳 0x0 ...
- Intel汇编语言程序设计学习-第五章 过程-下
5.3.3 库测试程序 测试程序#1:整数I/O 该测试程序把输出文本的颜色改为蓝底黄字,然后以十六进制数显示七个数组的内容,最后提示用户输入一个有符号整数,再分别以十进制.十六进制和二进制格式重复 ...
- pr恢复工作区
当工作区操作的位置很乱时 平时如果关闭的窗口,可以在窗口中查看 也可以选择新建工作区,保存成一个自己所需工作区
- Vue中的MVVM
MVVM(Model View VueModel) View层: 视图层 在我们前端开发中,通常就是DOM层 主要的作用就是给用户展示各种信息 Model层: 数据层 数据可能是我们固定的死数据,更多 ...
- Java中浮点数的坑
基本数据类型 浮点数存在误差 浮点数有一个需要特别注意的点就是浮点数是有误差的,比如以下这段代码你觉得输出的什么结果: public class Demo { public static void m ...