Codeforces Round #451 (Div. 2) E. Squares and not squares
来源:互联网 发布:淘宝店铺付费引流 编辑:程序博客网 时间:2024/06/03 20:01
题目大意
每次给一个数加一或减一,问最小操作次数使一半是完全平方数一半不是。
题解
显然只需要将多的一部分变成少的一部分,贪心选需要操作次数最少的。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int read(){ char ch=getchar();int f=0; while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') {f=f*10+(ch^48);ch=getchar();} return f;}struct data{ int num; int step; int type;}a[500005];bool cmp(data x,data y){ if(x.type==y.type) { return x.step<y.step; } else return x.type<y.type;}int n;int main(){ n=read(); for(int i=1;i<=n;i++) { a[i].num=read(); int temp=sqrt(a[i].num); if(temp*temp==a[i].num) { a[i].type=0; if(a[i].num==0) a[i].step=2; else a[i].step=1; } else { a[i].type=1; a[i].step=min(a[i].num-temp*temp,(temp+1)*(temp+1)-a[i].num); } } int pla=0; sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) { if(a[i].type==1) { pla=i-1; break; } } if(a[n].type==0) pla=n; int pla2=n-pla; //cout<<pla<<" "<<pla2<<endl; if(pla>pla2) { long long ans=0; for(int i=1;i<=n;i++) { ans+=a[i].step; pla--;pla2++; if(pla==pla2) break; } cout<<ans; } else if(pla<pla2) { long long ans=0; for(int i=pla+1;i<=n;i++) { ans+=a[i].step; pla++;pla2--; if(pla==pla2) break; } cout<<ans; } else cout<<"0";}
阅读全文
0 0
- Codeforces Round #451 (Div. 2) E. Squares and not squares
- Codeforces Round #451 (Div. 2) E. Squares and not squares (贪心)
- codeforce898E Squares and not squares (round #451 div 2)
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares详解
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares
- Codeforces Round #332 (Div. 2)-D. Spongebob and Squares
- Codeforces Round #332 (Div. 2)D. Spongebob and Squares
- Codeforces Round #337 (Div. 2)-B. Vika and Squares
- Codeforces Round #243 (Div. 1)——Sereja and Squares
- Codeforces Round #337 (Div. 2) A Pasha and Stick B Vika and Squares
- Codeforces Round #332 (Div. 2)D. Spongebob and Squares(数学方程,暴力)
- Codeforces Round #332 (Div. 2) 599D Spongebob and Squares(数学)
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares 公式推导
- Codeforces Round #337 (Div. 2) B. Vika and Squares (技巧乱搞)
- Codeforces Round #337 (Div. 2) 610B Vika and Squares(脑洞)
- Codeforces Round #332 (Div. 2)-D Spongebob and Squares(枚举+递推)
- Codeforces 314E Sereja and Squares
- 【Codeforces Round 332 (Div 2)D】【数学 公式推导】Spongebob and Squares 正方形数量恰好为x个的所有大矩形
- JVM的GC机制<一>新生代GC
- redis.conf 配置文件详解
- mysql百万级数据量根据索引优化查询速度
- ROM和RAM
- 日语总结(初上1~5)
- Codeforces Round #451 (Div. 2) E. Squares and not squares
- FreeBASIC学习笔记——第07章 常用内部函数
- 最小树形图 && LCA && Tarjan && 最/次短路
- Mac OS X 常用软件(备忘)
- Android界面编程之QuickContactBadge(一幅图片关联到手机上的一个联系人)
- mysql左链接 left join
- 排序相减
- 土地利用变化研究进展1
- 软件工程:计算模式的变迁