Codeforces 898E Squares and not squares (贪心)

 

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

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 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.

 

 

题目链接:http://codeforces.com/problemset/problem/898/E

 

题意:有n个数(n为偶数),求使得其中有n/2个平方数和n/2个非平方数的最小步骤,操作的方式为以下两种:①给某一堆增加一个②给某一堆减少一个。

思路:首先求出n个数中的平方数和非平方数的个数,并且把记录非平方数与其最近的平方数之差,平方数变成非平方数需要 1步 或者2步(0时两步);

两种实现方式:

 

#include <bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 2e5 + 10;
int n,kd,kc;
int a[N],c[N],d[N];


int main(){
    scanf("%d",&n);
    for(int i = 0; i < n; i ++) cin >> a[i];
    for(int i = 0; i < n; i ++){
        int x = sqrt(a[i]);
        if(x * x == a[i]) c[kc ++] = a[i];
        else d[kd ++] = min(a[i] - x*x,(x+1)*(x+1)-a[i]);
    }

    if(kd == kc) printf("0\n");
    else if(kc > kd){
        LL cnt = 0;
        sort(c,c+kc);
        for(int i = 0, j = kc-1; i < (kc - kd) / 2; i ++, j --){
            if(c[j] >= 1) cnt += 1;
            else if(c[j] == 0) cnt += 2;
        }
        cout << cnt << endl;
    }
    else{
        LL cnt = 0;
        sort(d,d+kd);
        for(int i = 0; i < (kd - kc) / 2; i ++){
            cnt += d[i];
        }
        cout << cnt << endl;
    }
    return 0;
}

 

 

 

 

 

 

 

 

#include <bits/stdc++.h>
using namespace std;

int n,num;
vector<int > vcta,vctb;

int main(){
    scanf("%d",&n);
    for(int i = 0; i < n; i ++){
        scanf("%d",&num);
        int x = sqrt(num);
        if(x * x == num) vcta.push_back(1 + (x == 0));
        vctb.push_back(min(num - x*x,(x+1)*(x+1)-num));
    }
    int en = n/2;
    if(vcta.size() > n/2) vctb = vcta, en = vcta.size() - en;
    sort(vctb.begin(),vctb.end());
    long long cnt = 0;
    for(int i = 0; i < en; i ++) cnt += vctb[i];
    cout << cnt << endl;
    return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值