Codeforces Round #451 (Div. 2) - E. Squares and not squares (贪心)

此编程挑战要求玩家通过最少的操作调整糖果堆数量,使一半堆的数量为平方数,另一半不是。涉及算法优化和数学逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.



题意:

让这些数平均分成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 long
const 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值