Codeforces-231C-To Add or Not to Add(二分搜索)
来源:互联网 发布:装修网站 知乎 编辑:程序博客网 时间:2024/05/16 14:53
点击打开题目链接
该题可以这么认为:经过一系列操作后,出现次数最多的数一定是原数组中的数,所以将原数组排序后,枚举每一个元素,计算数组中有多少个数能转换成该元素,计算过程就用二分实现了。
#include <iostream>#include <cstdio>#include <algorithm>#define LL __int64using namespace std;const int INF=1e5+5;LL num[INF],sum[INF];LL Cal(LL l,LL r) //计算num[l]变成num[r]需要加多少个数{ LL temp=sum[r]-sum[l-1]; temp=num[r]*(r-l+1)-temp; return temp;}LL erfen(LL s,LL k) //二分计算该数组中能转换出多少个num[s]{ LL l(1),r(s); while(l<r) { LL mid=(l+r)/2; LL temp=Cal(mid,s); if(temp>k) l=mid+1; else r=mid; } return s-l+1;}int main(){ LL n,k; while(~scanf("%I64d%I64d",&n,&k)) { for(LL i=1;i<=n;i++) scanf("%I64d",&num[i]); sort(num+1,num+n+1); sum[0]=0; for(LL i=1;i<=n;i++) //计算前i个元素和(包括第i个元素) sum[i]=sum[i-1]+num[i]; LL mx(0),res; for(LL i=1;i<=n;i++) //枚举数组中每一个元素 { LL temp=erfen(i,k); if(mx<temp) { mx=temp; res=num[i]; } } printf("%I64d %I64d\n",mx,res); } return 0;}
- Codeforces-231C-To Add or Not to Add(二分搜索)
- codeforces 231 C To Add or Not to Add 贪心
- CodeForces 231C To Add or Not to Add
- codeforces 143 C.To Add or Not to Add (YY 二分 处理一下前缀和)
- Codeforces Round #143 (Div. 2) C. To Add or Not to Add —— 二分
- To Add or Not to Add CodeForces
- CodeForces Round #143(231C) - To Add or Not to Add
- codeforces 231C To Add or Not to Add (脑洞题)
- CodeForces 231CTo Add or Not to Add
- Codeforces Round #143 (Div. 2) C. To Add or Not to Add 胡搞
- Codeforces Round #143 (Div. 2) C. To Add or Not to Add
- Add Talents, Not Skills, to Your Team
- 警告: git command could not be found. Please create an alias or add it to yo
- perl---use lib or BEGIN{ } to add directory to @INC
- checkbox_checkedChanged not work you have to add autopostback to true
- Add CheckBox to TreeView
- add text to pic
- Add Url To Favorites
- 程序员的野心:让GPU像CPU一样运行
- JS 控件 jQuery扩展函数
- 远程项目 eclipse 原生 ,远程run ,远程debug
- 计算字体大小及尺寸
- hdu 2089 不要62 数位dp
- Codeforces-231C-To Add or Not to Add(二分搜索)
- Android源码学习之如何使用eclipse+NDK【二】
- HDU 4639 Hehe
- tcpdump命令
- 判断数据表中记录的主键值是否存在外键约束
- java正则表达式不写^和$,表示匹配目标串的部分(包括本身)
- POJ--2182--Lost Cows
- 入驻csdn
- 杭电4501-小明系列故事——买年货