B. Grow The Tree Codeforces Round #594 (Div. 2)
Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacher’s Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.
The tree looks like a polyline on the plane, consisting of all sticks. The polyline starts at the point (0,0)
. While constructing the polyline, Alexey will attach sticks to it one by one in arbitrary order. Each stick must be either vertical or horizontal (that is, parallel to ??or ?? axis). It is not allowed for two consecutive sticks to be aligned simultaneously horizontally or simultaneously vertically. See the images below for clarification.
Alexey wants to make a polyline in such a way that its end is as far as possible from (0,0). Please help him to grow the tree this way.
Note that the polyline defining the form of the tree may have self-intersections and self-touches, but it can be proved that the optimal answer does not contain any self-intersections or self-touches.
Input
The first line contains an integer ?
n
(1≤?≤100000) — the number of sticks Alexey got as a present.
The second line contains ?
n
integers ?1,…,??
(1≤??≤10000) — the lengths of the sticks.
Output
Print one integer — the square of the largest possible distance from (0,0)
to the tree end.
Examples
inputCopy
3
1 2 3
outputCopy
26
inputCopy
4
1 1 2 2
outputCopy
20
Note
The following pictures show optimal trees for example tests. The squared distance in the first example equals 5⋅5+1⋅1=26 .
And in the second example 4⋅4+2⋅2=20
解题思路:为了达到题目要求的离远点最远,长要尽可能地长,宽要尽可能地短,将棍棒长度进行排序,对半分,长度为后一半棍棒长度和,宽度为前一半棍棒的长度和。棍棒顺序为一横一竖,不允许多个横着或多个竖着。
AC代码:
#include <iostream>
#include <algorithm>
using namespace std;
long long a[];
int main()
{
long long n,lenx=,leny=;
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
sort(a,a+n+);
for(int i=;i<=n;i++)
{
if(i<=n/)
lenx+=a[i];
else
leny+=a[i];
} cout<<lenx*lenx+leny*leny<<endl;
return ;
}
B. Grow The Tree Codeforces Round #594 (Div. 2)的更多相关文章
- Codeforces Round #594 (Div. 2) B. Grow The Tree 水题
B. Grow The Tree Gardener Alexey teaches competitive programming to high school students. To congrat ...
- C - Ilya And The Tree Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...
- Codeforces Round #594 (Div. 2)
传送门 C. Ivan the Fool and the Probability Theory 题意: 给出一个\(n*m\)的方格,现在要给方格中的元素黑白染色,要求任一颜色最多有一个颜色相同的格子 ...
- Codeforces Round #594 (Div. 1) D. Catowice City 图论
D. Catowice City In the Catowice city next weekend the cat contest will be held. However, the jury m ...
- Codeforces Round #594 (Div. 1) C. Queue in the Train 模拟
C. Queue in the Train There are
- Codeforces Round #594 (Div. 1) D2. The World Is Just a Programming Task (Hard Version) 括号序列 思维
D2. The World Is Just a Programming Task (Hard Version) This is a harder version of the problem. In ...
- Codeforces Round #594 (Div. 2) A. Integer Points 水题
A. Integer Points DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS to ...
- Codeforces Round #594 (Div. 1) A. Ivan the Fool and the Probability Theory 动态规划
A. Ivan the Fool and the Probability Theory Recently Ivan the Fool decided to become smarter and stu ...
- Codeforces Round #594 (Div. 1)
Preface 这场CF真是细节多的爆炸,B,C,F都是大细节题,每道题都写了好久的说 CSP前的打的最后一场比赛了吧,瞬间凉意满满 希望CSP可以狗住冬令营啊(再狗不住真没了) A. Ivan th ...
随机推荐
- 开发中少不了的Fun -- js判断设备
判断是否是移动设备 var ua = window.navigator.userAgent.toLowerCase(); if(ua.indexOf('mobile')!== -1){ return ...
- wavefronts
https://www.g-truc.net/post-0597.html https://michaldrobot.com/2014/04/01/gcn-execution-patterns-in- ...
- 如何实现swipe、tap、longTap等自定义事件
前言 移动端原生支持touchstart.touchmove.touchend等事件,但是在平常业务中我们经常需要使用swipe.tap.doubleTap.longTap等事件去实现想要的效果,对于 ...
- css居然有根据容器宽度自动截取长度加省略号功能,强大!!
作者:☆威廉古堡♂ 项目中最初的做法(js截取): //字符长度截取包括中英文混合 function subStr(str, len) { str = str.toString(); var newL ...
- Git 命令行解决冲突
git add filename 将本地工作区文件加入缓存区 git commit filename -m '提交文件注释' git status 查看当前工作区状态 git fetch ori ...
- @Import注解的作用
在@Import注解的参数中可以填写类名,例如@Import(Abc.class),根据类Abc的不同类型,spring容器有以下四种处理方式: 1. 如果Abc类实现了ImportSelector接 ...
- vue 日期格式化过滤器
formateDate日期格式化,写在公共的js中: export function formateDate(date, fmt){ if ('/(y+)/'.test(fmt){ fmt = fmt ...
- idea 高效找出全部未被使用的代码
不得不说 idea 真的很强大,认真花一些时间,好好研究研究 idea 可以让你编写代码更加的高效,并且 idea 时不时会给你一些惊喜的,比如今天要分享的这个,就非常的惊喜: 背景 前几天,忽然又一 ...
- jQuery系列(一):jQuery介绍
1.为什么要使用jQuery (1)什么是jQuery jQuery 是 js 的一个库,封装了我们开发过程中常用的一些功能,方便我们调用,提高开发效率. js库是把我们常用的功能放到一个单独的文件中 ...
- quartz.net 执行后台任务
... https://www.cnblogs.com/zhangweizhong/category/771057.html https://www.cnblogs.com/lanxiaoke/cat ...