题意:
让这些数平均分成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;
}