Codeforces Round #451 (Div. 2)

来源:互联网 发布:一般约束的最优化问题 编辑:程序博客网 时间:2024/06/15 18:46

E. Squares and not squares
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ann and Borya have n piles with candies and n is even number. There are ai candies in pile with number i.

Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this candy is new and doesn't belong to any other pile) or remove one candy (if there is at least one candy in this pile). 

Find out minimal number of moves that is required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer.

Input

First line contains one even integer n (2 ≤ n ≤ 200 000) — number of piles with candies.

Second line contains sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 109) — amounts of candies in each pile.

Output

Output minimal number of steps required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer. If condition is already satisfied output 0.

Examples
input
412 14 30 4
output
2
input
60 0 0 0 0 0
output
6
input
6120 110 23 34 25 45
output
3
input
10121 56 78 81 45 100 1 0 54 78
output
0
Note

In first example you can satisfy condition in two moves. During each move you should add one candy to second pile. After it size of second pile becomes 16. After that Borya and Ann will have two piles with number of candies which is a square of integer (second and fourth pile) and two piles with number of candies which is not a square of any integer (first and third pile).

In second example you should add two candies to any three piles.



题意:

让这些数平均分成2堆,一堆平方数,一堆不是。输出最小的变化。


POINT:

【平方数】变【不是平方数】,除了0,都知道+1就行。0要+2,特殊考虑。

【不是平方数】变【平方数】,上下比较取小。

然后换一换就行。

唯一的坑点是long long。作为E题也太简单了。


#include <iostream>#include <stdio.h>#include <math.h>#include <algorithm>using namespace std;#define  LL long longconst LL maxn = 200000+444;LL besq[maxn];int main(){LL n;scanf("%lld",&n);n=n/2;LL zero=0;LL cnt=0;LL sq=0;LL fsq=0;for(LL i=1;i<=2*n;i++){LL a;scanf("%lld",&a);LL b=(LL)(sqrt(a));if(b*b==a){sq++;}else{LL now=min(a-b*b,(b+1)*(b+1)-a);besq[++cnt]=now;fsq++;}if(a==0){zero++;}}if(sq<n){sort(besq+1,besq+1+cnt);LL ans=0;for(LL i=1;i<=n-sq;i++){ans+=besq[i];}printf("%lld\n",ans);}else if(sq==n){printf("0\n");}else{LL now=sq-zero;if(sq-n<now){printf("%lld\n",sq-n);}else{printf("%lld\n",now+(sq-n-now)*2);}}return 0;}



原创粉丝点击