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 ...
随机推荐
- java Timer和TimerTask(简单的使用)
Timer 是一个定时工具 TimerTask 是一个实现了Runnable接口抽象类,代表可以被Timer执行的任务 (1)Timer.schedule(TimerTask task,Date ti ...
- Elasticsearch: Index template
Index template定义在创建新index时可以自动应用的settings和mappings. Elasticsearch根据与index名称匹配的index模式将模板应用于新索引.这个对于我 ...
- 01_初识redis
1.redis和mysql mysql是一个软件,帮助开发者对一台机器的硬盘进行操作. redis是一个软件,帮助开发者对一台机器的内存进行操作 汽车之家,如果硬盘挂掉了,页面还能访问1个月 关键字: ...
- CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)
赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...
- Appium Python核心API
adb命令模拟按键事件 :http://blog.sina.com.cn/s/blog_68f262210102vc1b.html
- prevAll([expr]) 查找当前元素之前所有的同辈元素
prevAll([expr]) 概述 查找当前元素之前所有的同辈元素 可以用表达式过滤.大理石构件维修 参数 exprStringV1.2 用于过滤的表达式 示例 描述: 给最后一个之前的所有div加 ...
- O(1)快速乘与O(log)快速乘
//O(1)快速乘 inline LL quick_mul(LL x,LL y,LL MOD){ x=x%MOD,y=y%MOD; return ((x*y-(LL)(((long d ...
- Python基础之深浅copy
1. 赋值 lst1 = [1, 2, 3, ["a", "b", "c"]] lst2 = lst1 lst1[0] = 11 print ...
- AtCoder AGC001D Arrays and Palindrome (构造)
补一下原来做过的AtCoder思维题的题解 题目链接: https://atcoder.jp/contests/agc001/tasks/agc001_d 先特判一些小的情况. 原题就相当于每个回文串 ...
- jquery转换js
刚离职,一直忙于弄简历,整理面试题.今天得空吧前几天学习复习的jq基础知识整理一下,长时间不用还真的忘记了.所有在深入学习中也不要忘记复习之前的知识.做巩固,老话说的好打好根基才能盖好房.基础知识过后 ...