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 ...
随机推荐
- 【转】深入理解Spring的两大特征(IOC和AOP)
原文链接:https://blog.csdn.net/gloomy_114/article/details/68946881 众所周知,Spring的核心特性就是IOC和AOP,IOC(Inversi ...
- unity 用代码控制动画的播放的进度
https://answers.unity.com/questions/1225328/imported-animated-object-and-slider-tutorial.html using ...
- webstorm 2016.3 注册方法
用license server 的方式吧,activation code 的方式没有找到方法. license server 里写http://idea.iteblog.com/key.php 用li ...
- intellij idea gradle 导入 spring 问题记录
环境: windows 7 oracle jdk 1.8 intellij idea 2019.3.1 spring-framework 5.1.22.RELEASE 步骤: 1: 下载解压sprin ...
- code命令用vscode打开项目代码
1. 打开vscode, 使用Command + shift + p, 输入shelll 选择
- BZOJ 4070: [Apio2015]雅加达的摩天楼 根号分治+spfa
此题卡Dijkstra... Code: #include <bits/stdc++.h> #define N 30005 #define M 4000000 #define ll lon ...
- const 与指针 的用法
请找出下面程序中有哪些错误: 1 2 3 4 5 6 7 8 9 10 11 12 13 int main() { int i=10; int j=1; const int *p1; ...
- Mac下Tomcat安装&配置&80默认端口设置
序言: 在学习Tomcat时, 部署虚拟服务主机时,遇到了无响应的情况.原以为是应为Tomcat默认端口8080在调整至(进行端口转发设置)默认端口80会和Mac自带Apache起冲突.但是也有同学使 ...
- 记一次 用 ssh 反向代理解决的远程操作效率问题
公司在异地有一个项目,项目在内网有一个linux 集群开发人员通过 xshell 进行操作,但是开发过程中还需要公司开发人员进行远程操作,原来采用的方案是向日葵,需求能实现但是限于网络环境向日葵实在是 ...
- MFC消息反射机制
消息反射机制要解决什么问题呢? 消息反射机制主要是为了控件而实现的.每当控件需要某些资讯(比如,绘制自身背景的画刷,显示字体的颜色等等)时,都会频繁地向其父窗口发送通告消息(notification ...