codeforces 231 C To Add or Not to Add 贪心
来源:互联网 发布:cacti的linux镜像 编辑:程序博客网 时间:2024/06/05 11:55
做法:进行从小到大排序,这样可以使最接近的几个数都聚在一起,然后用(i-j+1)*a[i]-sum;sum-=a[j++];这样可以以i-j+1为重复次数,算出重复最多的数,然后以为是从小到大排列的,又可以找出最小数。
#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <utility>#define LL long long#define LMT 100003//化分离数为块,贪心题using namespace std;int a[LMT];int main(){ int n,k; LL sum=0; pair<int,int> ans; scanf("%d%d",&n,&k); for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); ans.first=0;ans.second=0; for(int i=0,j=0;i<n;i++) { sum+=a[i]; while((i-j+1LL)*a[i]-sum>k)sum-=a[j++]; if(ans.second<i-j+1LL) ans=make_pair(a[i],i-j+1LL); } printf("%d %d\n",ans.second,ans.first); return 0;}
- codeforces 231 C To Add or Not to Add 贪心
- CodeForces 231C 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 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
- codeforces 143 C.To Add or Not to Add (YY 二分 处理一下前缀和)
- 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
- 学习 Linux LVM
- USB启动
- putty自动登录Ubuntu服务器设置
- 访问aspx页面时提示“无法找到该页”的解决方法
- IOS 消息推送---服务端.p12证书的生成
- codeforces 231 C To Add or Not to Add 贪心
- 存储过程分页
- 带标签的 break 和 continue (Java)
- SceneControl+AE+鼠标滚轮缩放
- 解决singleton Bean依赖prototype Bean不能实时更新的问题
- 背景减除法综述
- 关于SIGPIPE导致的程序退出
- IOS消息推送------推送证书的安装与调试
- 使用BeanUtils方法拷贝不上问题