I - 如若重新来过
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
4
12 14 30 4
2
6
0 0 0 0 0 0
6
6
120 110 23 34 25 45
3
10
121 56 78 81 45 100 1 0 54 78
0
#include <cstdio> #include <iostream> #include <cmath> #include <algorithm> #define INF 1e-8 using namespace std; int main() { int n,m,t=0,w=0; int a[200005],b[200005]; cin>>n; for(int i=0;i<n;i++){ cin>>m; int ss=sqrt(m*1.0); if(m!=ss*ss){ ss=min(m-ss*ss,(ss+1)*(ss+1)-m); a[t++]=ss; } else { if(m==0) b[w++]=2; else b[w++]=1; } } sort(a,a+t); sort(b,b+w); long long sum=0; if(w==t) sum=0; else if(w>t){ int q=w-t; for(int i=0;i<q/2;i++) sum+=b[i]; } else{ int q=t-w; for(int i=0;i<q/2;i++) sum+=a[i]; } cout<<sum<<endl; }