codeforces 231C To Add or Not to Add (脑洞题)
来源:互联网 发布:举而不坚,坚而不久知乎 编辑:程序博客网 时间:2024/05/29 18:56
【题目链接】 https://cn.vjudge.net/problem/32741/origin
【题解】 题意为从给定的长度为n的数列的一些数加1,(可以多次,只要加的总数不超过K),使得此序列中能够有一个出现次数最多的数,输出这个数出现的次数和这个数的值,如果有两个数出现的次数相同,选择数值较小的那个,比如样例
Input
5 36 3 4 0 2
Output
3 4
加的总数不超过3(当然,可以用不完,比如5,5,5,不管k是多少都不用加),所以就可以给3加1变为4,给2加2变为4,此时K刚好用完,而4也是这个数列里面出现次数最多的(3次),所以答案就为3,4.希望我说的够明白。
这道题不好想,但是想明白了就很简单,具体看代码,为了输出较小的那一个,应该从小到大排序。
【AC代码】
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#include<math.h>using namespace std;const int N=1e5+10;__int64 m,n,a[N];int main(){ while(~scanf("%I64d%I64d",&m,&n)) { for(int i=1;i<=m;i++) scanf("%I64d",&a[i]); sort(a+1,a+m+1); int index=1;//下标 __int64 t=0;前面有多少数需要加1, __int64 sum=0;//加的数的总合 __int64 cnt = 1;//标记出现次数 __int64 num = a[1];//用来标记出现次数最多的那个数 for(int i=2;i<=m;i++) { __int64 d=a[i]-a[i-1]; t++; sum+=t*d;//这里就是说,第一个数加上d变为和第二个数一样大,现在前两个数要变的和第三个数一样大,就要用第三个数与第二个数的差乘以前面需要变化的数的个数 while(sum>n) { sum-=a[i]-a[index]; index++; t--; } if(t+1>cnt) { cnt = t+1; num = a[i]; } } printf("%I64d %I64d\n",cnt,num); } return 0;}
记得要用I64d 输出,否则会WA哦~
阅读全文
0 0
- codeforces 231 C To Add or Not to Add 贪心
- CodeForces 231C 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 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
- 241 Different Ways to Add Parentheses(C代码版)
- 第二周:( LeetCode241 ) Different Ways to Add Parentheses(c++)
- 217. Contains Duplicate Add to List(C++)
- File "<stdin>" , line 1 错误
- 智能小车开发记录一(20170714)
- 大神手把手教你:(Python)序列数据的One Hot编码
- Element (center) is obsolete. Its use is discouraged in HTML5 documents
- MFC中软件开启等待界面的实现方法
- codeforces 231C To Add or Not to Add (脑洞题)
- 贪心总结
- 设计模式-装饰器模式
- javascript模块化之CommonJS、AMD、CMD、UMD、ES6
- Android studio中如何用快捷键快速调出try catch
- 基于 WebRTC 创建一款多人联机游戏
- “GANs之父”Goodfellow 38分钟视频亲授:如何完善生成对抗网络?(上)
- C#找出接口的所有实现类并遍历执行这些类的公共方法
- 陈耀硕:黄金投资常见28个专用术语