codeforce—Squares and not squares补11-30training
Problem - D - Codeforces
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.
安和鲍里亚有 n 堆糖果, n 是偶数。糖果堆中有 ai 颗糖果,数字为 i 。
安喜欢某个整数的平方的数字,而波利亚不喜欢任何整数的平方的数字。在一次移动过程中,玩家可以选择某一堆糖果,并向其中添加一颗糖果(这颗糖果是新的,不属于其他任何一堆糖果)或移除一颗糖果(如果这堆糖果中至少有一颗糖果)。
求要使 n / 2 堆中的糖果数正好是某个整数的平方,且 n / 2 堆中的糖果数正好不是某个整数的平方所需的最少移动次数。
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.
输入
第一行包含一个偶数整数 n ( 2 ≤ n ≤ 200 000 ) - 糖果堆的数量。
第二行包含一系列整数 a1, a2, ..., an ( 0 ≤ ai ≤ 109 ) - 每堆糖果的数量。
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.
输出
输出要使恰好 n / 2 堆包含的糖果数是某个整数的平方,且恰好 n / 2 堆包含的糖果数不是任何整数的平方所需的最少步骤数。如果条件已满足,则输出 0.
Examples
Input
412 14 30 4
Output
2
Input
60 0 0 0 0 0
Output
6
Input
6120 110 23 34 25 45
Output
3
Input
10121 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.
注意
在第一个例子中,你可以分两步满足条件。在每一步棋中,你都要向第二层棋子堆中添加一颗糖果。之后第二层棋子的大小变为 16 。这样博里亚和安就会有两堆糖果数量是整数的平方的棋子(第二和第四堆)和两堆糖果数量不是整数的平方的棋子(第一和第三堆)。
在第二个示例中,您应该在任意三堆中添加两颗糖果。
题解
初步题意就是对对进行每次加一或者减一的操作,使得能够开平方为整数的堆数和不能开平方为整数的堆数相等,代码实现是很简单的
- 我们对两种堆数进行数量统计,如果相等就输出0
- 如果开平方为整数的堆数没有另一种堆数多,那我们只对两者相减后除二数量的非开方整数堆进行修改,值得注意的是:这个数可能是向上取,也可能是向下取,我们只需要修改次数最小的
- 如果开平方为整数的堆数比另一种堆数多,我们也是取两者相减后除二数量的开方整数堆进行修改,对于一般数我们只需要修改一次就可以,对于0这个数要特殊处理,要修改两次,所以优先修改非0数,最后注意数据范围
代码如下
#include<bits/stdc++.h>
using namespace std;
int a[200005];
bool check(int a){
return int(sqrt(a))*int(sqrt(a))==a;
}
int main() {
int n;cin>>n;
for(int i = 0;i < n;i ++)cin>>a[i];
vector<long long> v1;
vector<long long>v2;
for(int i = 0;i < n;i ++){
if(!check(a[i]))
v1.push_back (min((pow(int(sqrt(a[i]))+1,2) - a[i]),( a[i] - pow(int(sqrt(a[i])),2))));
else
v2.push_back(a[i]);
}
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end(),greater<int>());
if(v1.size()==v2.size())cout<<0;
else{
long long ans = 0;
if(v2.size() > v1.size())
{
for(int i = 0;i <(v2.size() - v1.size()) / 2;i++){
if(v2[i]!= 0)
ans += 1;
else
ans += 2;
}
cout<<ans;
}
else{
long long ans = 0;
for(int i = 0;i < (v1.size()-v2.size()) / 2;i++)
ans += v1[i];
cout<<ans;
}
return 0;
}
}

浙公网安备 33010602011771号