898E. Squares and not squares(暴力)

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
Copy
4
12 14 30 4
Output
2
Input
Copy
6
0 0 0 0 0 0
Output
6
Input
Copy
6
120 110 23 34 25 45
Output
3
Input
Copy
10
121 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.

题意:给出n个数,任意一个数一次可以加一或减一,求最少几次操作后有一半是完全平方数另一半的非完全平方数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	int n;
	scanf("%d",&n);
	priority_queue<int,vector<int>,greater<int> >p1,p2;
	for(int i=0;i<n;i++){
		int x;
		scanf("%d",&x);
		int y=sqrt(x);
		if(y*y==x){
		   if(y==0) p1.push(2);
		   else p1.push(1);	
		}
		else p2.push(min((y+1)*(y+1)-x,x-y*y));
	}
	ll ans=0;
	while(p1.size()>n/2){ //将完全平方数换成非完全平方数 
		ans+=p1.top();
		p1.pop();
	}
	while(p2.size()>n/2){//将非完全平方数换成完全平方数 
		ans+=p2.top();
		p2.pop();
	}
	printf("%lld\n",ans);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值