Codeforces 898E - Squares and not squares 【优先队列贪心】


E. Squares and not squares


time limit per test  2 seconds

memory limit per test      256 megabytes


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

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

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

Input

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

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

Output

Output minimal number of steps requiredto make exactly n / 2 piles contain number of candies that is a square of some integer andexactly n / 2 piles contain number of candies that is not a square of any integer. Ifcondition is already satisfied output 0.

Examples

Input

4
12 14 30 4

Output

2

Input

6
0 0 0 0 0 0

Output

6

Input

6
120 110 23 34 25 45

Output

3

Input

10
121 56 78 81 45 100 1 0 54 78

Output

0

Note

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

In second example you should add twocandies to any three piles.

 



【题意】


给你n个数,现在你可以进行一系列操作,每次操作可以对任意一个数加一或者减一,使得进行操作后n个数中恰好有一半是平方数还有一半是非平方数。


【思路】


显然,如果初始数中平方数的个数大于非平方数,我们需要选择一些非平方数改成非平方数,反之同理。


那么我们需要做的便是转化的最优方案。


显然对于任意平方数我们只要加1就能把它变成非平方数(0需要加2次)。


对于每个非平方数,把它变为平方数的最小操作数便是它与相邻两个平方数差值的较小值。


对于每个数判断一下,操作数放在从小到大的优先队列里即可。


#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 1000005;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-9;

int n,x;

priority_queue<int,vector<int>,greater<int> >q1,q2;

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        int cnt=sqrt(x*1.0);
        if(cnt*cnt==x)                      //平方数
        {
            if(cnt==0) q1.push(2);
            else q1.push(1);
        }
        else q2.push(min((cnt+1)*(cnt+1)-x,x-cnt*cnt)); //非平方数
    }
    ll ans=0;
    while(q1.size()>n/2)              //选择最优的几个平方数变为非平方数
    {
        ans+=q1.top();
        q1.pop();
    }
    while(q2.size()>n/2)              //选择最优的几个非平方数变为平方数
    {
        ans+=q2.top();
        q2.pop();
    }
    printf("%I64d\n",ans);
}





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值