Codeforces 898E Squares and not squares (贪心)
2018-01-15 22:29
381 查看
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
output
input
output
input
output
input
output
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
,c
,d
;
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;
}
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
6120 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
,c
,d
;
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;
}
相关文章推荐
- codeforces 699C Vacations (贪心)
- Codeforces 226D The table 贪心
- CodeForces 377B-Preparing for the Contest(二分+贪心)
- Codeforces 462C C. Appleman and Toastman【贪心】
- codeforces 74B Train 贪心 简单模拟
- CodeForces 596B Wilbur and Array 贪心
- 【codeforces 733D】【贪心 乱搞】D. Kostya the Sculptor【给你n个长方形,让你找出2个或1个长方体,使得他们拼接成的长方体的内接圆半径最大】
- CodeForces 699C Vacations 贪心
- Codeforces 691D Swaps in Permutation【并查集+优先队列+贪心】
- Codeforces 600C Make Palindrome 【贪心 找字典序最小回文串】
- Codeforces - 299C. Weird Game - 贪心、博弈
- Codeforces 831D Office Keys 二分+贪心
- codeforces-232【A贪心、思维、图】
- 【codeforces 672 C】【计算几何+贪心】Recycling Bottles【有两个人和一个垃圾桶,n个瓶子,把每个瓶子捡起来然后扔到垃圾箱中,每人最多带一个瓶子,求最短总距离】
- codeforces - 508B - Anton and currency you all know(贪心)
- Codeforces 509C. Sums of Digits 贪心 + 模拟
- CodeForcesdiv1:995C - Leaving the Bar(随机算法+贪心)
- Codeforces-734B-Anton and Digits(贪心)
- Codeforces 479D Long Jumps【map标记+贪心】
- codeforces 472C Design Tutorial: Make It Nondeterministic 贪心