CF792E Colored Balls【思维】
考试的时候又想到了小凯的疑惑,真是中毒不浅...
设每一个数都可以被分成若干个$k$和$k+1$的和。数$x$能够被分成若干个$k$和$k+1$的和的充要条件是:
$x%k<=floor(x/k)$
又因为$k$一定小于这个数列中最小的那个数,可以轻易想到的一个朴素的方法就是从$1$到$A_{min}$枚举所有可能的$k$,判断是否满足情况,并更新答案。
注意到$k$越大,答案越优,所以从大到小进行枚举,找到答案就退出。
我们现在来优化他:
可以想到,当$k<=\sqrt{x}$,上述不等式一定成立。
所以只需要判断$k$在$(\sqrt{x},x]$范围内是否满足就可以了。
可是$x$在$1e9$的范围内,还是会超时呢。
其实我们枚举到了很多无用的$k$,因为要保证$A_{min}$也可以分成若干个$k$和$k+1$的和,所以实际上有效的$k$是:$A_{min}$,$A_{min}/2$,$A_{min}/3$...诸如此类的数...
我们可以枚举集合个数($A_{min}$可以被拆成多少个数),然后通过集合个数来算$k$
枚举范围就从$(\sqrt{x},x]$变成了$(1,\sqrt{x}]$
在代码里,我特判了一下$1$的情况(其实是因为考试稳妥)
还有一些细节问题都放在注释里了
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
#define N 505
#define ll long long
int n;
int a[N];
ll ans;
int rd()
{
int f=,x=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=(x<<)+(x<<)+(c^);c=getchar();}
return f*x;
}
int res=-;
bool check(int k,int ret,int id)
{//如果余数为0 有一次将k调整成k-1的机会
for(int i=;i<=n;i++)
{
int p=a[i]/k,q=a[i]%k;
if(ret&&q>p) return ;
if(!ret)
{
if(q>p)
{
k--;
ret=;
p=a[i]/k,q=a[i]%k;
}
if(q>p) return ;
}
}
res=k;
return ;
}
int main()
{
n=rd();
for(int i=;i<=n;i++)
a[i]=rd();
sort(a+,a+n+);
if(a[]==)
{
for(int i=;i<=n;i++)
{
if(a[i]&)
{
ans+=(a[i]-)>>;
ans++;
}
else ans+=(a[i]>>);
}
printf("%lld\n",ans+);
return ;
}
for(int i=;i<=int(sqrt(a[]))+;i++)
{//枚举集合个数 (对于最小的数)
int k=a[]/i;//集合大小 k和k+1
int ret=a[]%i;//如果是整除 就不能确定是k-1和k 还是k和k+1
//如果有余数 肯定是k和k+1(k还不够)
//如果余数为0 有一次将k调整成k-1的机会
if(check(k,ret,i))
break;
}
//printf("%d\n",res);
for(int i=;i<=n;i++)
ans+=(a[i]+res)/(res+);
printf("%lld\n",ans);
return ;
}
/*
2
948507270 461613425
*/
Code
CF792E Colored Balls【思维】的更多相关文章
- CF792E Colored Balls
题目大意:将n个数分解成若干组,如4 = 2+2, 7 = 2+2+3,保证所有组中数字之差<=1. 首先我们能想到找一个最小值x,然后从x+1到1枚举并check,找到了就输出.这是40分做法 ...
- Codeforces554 C Kyoya and Colored Balls
C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- codeforces 553A . Kyoya and Colored Balls 组合数学
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合
C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Kyoya and Colored Balls(组合数)
Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))
C. Kyoya and Colored Balls Kyoya Ootori has a bag with n colored balls that are colored with k diffe ...
- 554C - Kyoya and Colored Balls
554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
- Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
随机推荐
- mySql数据重复数据去重
1.问题来源:数据中由于并发问题,数据存在多次调用接口,插入了重复数据,需要根据多条件删除重复数据: 2.参考博客文章地址:https://www.cnblogs.com/jiangxiaobo/p/ ...
- Vue 事件监听实现导航栏吸顶效果(页面滚动后定位)
Vue 事件监听实现导航栏吸顶效果(页面滚动后定位) Howie126313 关注 2017.11.19 15:05* 字数 100 阅读 3154评论 0喜欢 0 所说的吸顶效果就是在页面没有滑动之 ...
- luogu 1373 小a和uim之大逃离 dp
有取模操作,所以直接维护模意义下的差即可. Code: #include <bits/stdc++.h> #define M 16 #define N 801 #define ll lon ...
- Linux 系统设置命令之ulimit
定义 ulimit 用于限制 shell 启动进程所占用的资源,支持以下各种类型的限制:所创建的内核文件的大小.进程数据块的大小.Shell 进程创建文件的大小.内存锁住的大小.常驻内存集的大小.打开 ...
- JVM GC之垃圾收集算法
1.垃圾收集概念 GC目的 分配内存,为每个新建的对象分配空间 确保还在使用的对象的内存一直还在,不能把有用的空间当垃圾回收了 释放不再使用的对象所占用的空间 我们把还被引用的对象称为活的,把不再被引 ...
- wgs84 转百度经纬度坐标
/** * wgs84 转百度地图坐标 * @param $lng * @param $lat * @return array */ function toBaiduLocation($lng,$la ...
- [51nod1666] 最大值
题面 题解 毒瘤题浪费我大好青春 容易知道, 如果\(l\)和\(r\)位数不一样, 直接选形似\(99..99\)的数, 输出答案即可 \(l\)和\(r\)位数一样的话, 当位数确定的时候, 由于 ...
- DIV盒子模型介绍 div用法
- Echarts4+EchartsGL 3D迁徙图(附源码)
最近遇到些Echarts迁徙图问题,在实现二维地图的迁徙图后开始开发3D迁徙图,在网上一查,发现3D版本迁徙图资料较少,自己研究并借鉴一些资料后写了一个小demo,希望能帮大家少走些弯路,共同学习. ...
- ctf密码学------密文解码python脚本(凯撒解密)
题目来源实验吧 分析题意,说是困在栅栏中,所以将字符栅栏解密看看有什么,利用工具CTFcraktools 得到三条密文 然后说是密码是凯撒,在将四栏依次凯撒解码,寻找可能的key,这里很显然,在尝试第 ...