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. 分析: 一眼看 ...
随机推荐
- Proxy.newProxyInstance源码探究
JDK动态代理案例实现:实现 InvocationHandler 接口重写 invoke 方法,其中包含一个对象变量和提供一个包含对象的构造方法: public class MyInvocationH ...
- 1-OSI七层模型
网络功能:数据传输 ISO(国际标准化组织) OSI七层模型---->网络通信工作流程的标准化 OSI七层模型 应用层:提供用户服务,具体功能由特定的程序而定. 表示层:数据的压缩优化,加密. ...
- 【SpringMVC配置失效】Springboot2.x拦截器配置不无生效
一.环境 maven springboot版本2.x <parent> <groupId>org.springframework.boot</groupId> &l ...
- PE文件加节感染之Win32.Loader.bx.V病毒分析
一.病毒名称:Win32.Loader.bx.V 二.分析工具:IDA 5.5.OllyDebug.StudPE 三.PE文件加节感染病毒简介 PE病毒感染的方式比较多,也比较复杂也比较难分析,下面就 ...
- 洛谷P1307 数字反转
题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2). 输入输出格式 输入格式: 输入 ...
- Python 第二章-列表和元组
第二章-列表和元组 2.0 在Python中,最基本的数据结构是序列(sequence).序列中的每个元素被分配一个序列号-即元素的位置, 也称为索引.第一个索引是0,第二个是1,以此类推. ...
- POJ2060最小路径覆盖
题意: 有n个任务,如果时间来得及干完某些任务后还可以接着干别的任务,给一个任务清单,问最少派出去多少人能完成所有任务. 思路: 比较简单的追小路径覆盖问题了,在DAG中找到 ...
- 去除腾讯视频logo水印
打开F12,然后再console中输入下面代码,即可. document.querySelectorAll(".txp_waterMark_pic").forEach(functi ...
- 7 IDEA连接数据库
IDEA连接数据库 连接成功后,选择数据库 查看数据库/表的内容就双击数据库 修改数据库--要点击DB才能保存 出现问题 错误描述 Server returns invalid timezone. G ...
- 计算机网络参考模型,IP地址及MAC地址查看方法,数据包封装过程
分层思想 首先,计算机网络参考模型,是基于分层思想而出现的.分层思想,就是将复杂流程分解为几个功能单一的子过程. 优点: 可以让整个流程更加清晰, 让复杂问题简单化, 更容易发现问题,并真对性的解决问 ...