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 ...
随机推荐
- Navicat Premium 12 安装 与 激活
官方简体中文下载网址:https://www.navicat.com.cn/download/navicat-premium 安装的过程....(都是中文)所以略过. 开始破解....( ...... ...
- WebApiHelper
/// <summary> /// 根据token过滤 /// </summary> /// <param name="list"></p ...
- BZOJ 1706: [usaco2007 Nov]relays 奶牛接力跑 倍增Floyd
题不难,但是一开始把读入看错了,调了半天qaq~ Code: #include <bits/stdc++.h> #define N 300 #define setIO(s) freopen ...
- c语言if-else的效率比较
闲着没事测试下if-else的执行效率 测试环境:Mac pro i7 2.3Ghz ...编译器gcc 4.9,代码没有进行优化-O0: 测试代码:c代码1: int main(){ int n=1 ...
- CF427D
CF427D SA的奇技淫巧,其实就是板子. 题意: 给定两个字符串,求最短的满足各只出现一次的连续公共字串 解析: 一般情况下,SA都是用来求最长公共前缀的,好像和这道题所求的最短公共子串没有任何关 ...
- mysql数据库的索引
什么是索引 索引就是一种优化查询的数据结构: 为什么要加索引 因为创建索引可以大大提高系统的查询性能. 怎么提高查询性能的 简单的理解:一张数据量比较大的表格如果没有添加任何索引,那我们在执行查询的时 ...
- js判断是否联网
// navigator.onLine if (navigator.onLine){ //正常工作 console.log("在线状态............................ ...
- Linux设备驱动程序 之 open和release
open方法 open方法提供给驱动程序以初始化的能力,在大部分驱动程序汇总,open应该完成以下工作: 1. 检查特定设备的错误,如设备为准备就绪或者硬件问题: 2. 如果设备是首次打开,则对其进行 ...
- LeetCode 44. 通配符匹配(Wildcard Matching)
题目描述 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完 ...
- (转载)完成端口(CompletionPort)详解 - 手把手教你玩转网络编程系列之三
转自:http://blog.csdn.net/piggyxp/article/details/6922277 前 言 本系列里完成端口的代码在两年前就已经写好了,但是由于许久没有写东西了,不知该如何 ...