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 ...
随机推荐
- ORM高阶补充:only, defer,select_related
Queryset官方文档:https://docs.djangoproject.com/en/1.11/ref/models/querysets/ 1.需求1:只取某n列 1.方法1:values 2 ...
- CF732D Exams 二分 贪心
思路:二分+贪心 提交次数:10次以上 错因:刚开始以为二分(边界,$+1or-1$)写错了,调了半天,后来才发现是$ck()$写错了.开始只判了最后是否小于零,而应该中间一旦小于零就$return\ ...
- Go程序的一生是怎样的?
Go 程序是怎样跑起来的 原创: 饶全成 码农桃花源 刚开始写这篇文章的时候,目标非常大,想要探索 Go 程序的一生:编码.编译.汇编.链接.运行.退出.它的每一步具体如何进行,力图弄清 Go 程序 ...
- Flask-websocket实现聊天功能
群聊无昵称 原生js代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- js scroll动画
知识点 1.window.scrollTo (x,y):可以把内容滚动到指定位置 scroll scroll:卷动意思(书卷) 从上到下移动 1.window.onscroll 窗口滚动事件 ...
- vue中解决three.js出现内存泄漏丢失上下文问题
在跳转页面时添加以上代码即可. 在spa项目中,跳转页面并不会清楚已经创建的webgl实例,需要手动清楚.
- 微信小程序 保存图片
微信小程序 保存图片 注: 此处使用的是小程序 wepy框架, 原生或其他的请注意转换写法 <div class="handle"> <button class= ...
- Devops(一):CentOS7 安装Maven3.6.1详解
https://yq.aliyun.com/topic/78?spm=5176.8290451.656547.7.rMYhAF https://yq.aliyun.com/activity/155?u ...
- 【MyBatis】【SQL】没有最快,只有更快,从一千万条记录中删除八百万条仅用1分9秒
这次直接使用delete from emp where cdate<'2018-02-02',看看究竟会发生什么. Mapper里写好SQL: <?xml version="1. ...
- Oracle 对某张表中的某一列进行取余,将结果集分为多个集合
比如分为 5个集合,那么就用某一列和5 取余 ,分别可以取 余数为 0.1.2.3.4 的结果集,那么就把集合分为5个小的集合了 1.取余数为 0 的集合 select * from (select ...