题目大意:将n个数分解成若干组,如4 = 2+2, 7 = 2+2+3,保证所有组中数字之差<=1。

首先我们能想到找一个最小值x,然后从x+1到1枚举并check,找到了就输出。这是40分做法。

能不能优化?我们发现,若k合法,那么x%k==0或x%(k+1)==0或x%(k-1)==0。

所以枚举倍数就行了,利用贪心找到了就输出。

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
int n;
ll a[],mn=0x3f3f3f3f;
bool cmp(ll x,ll y)
{
return x<y;
}
void check(ll k)
{
if(!k)return ;
ll ret = ;
for(int i=;i<=n;i++)
{
if(a[i]<k-)return ;
int cnt = a[i]/k+(a[i]%k!=);
if(a[i]%k==)
{
ret+=cnt;
continue;
}
if(k*cnt-a[i]>cnt)return ;
ret+=cnt;
}
printf("%I64d\n",ret);
exit();
return ;
}
int main()
{
// freopen("C.in","r",stdin);
// freopen("C.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
mn=min(mn,a[i]);
}
for(int i=;i<=mn;i++)
{
check(mn/i+);
check(mn/i);
check(mn/i-);
}
fclose(stdin);
fclose(stdout);
return ;
}

CF792E Colored Balls的更多相关文章

  1. CF792E Colored Balls【思维】

    题目传送门 考试的时候又想到了小凯的疑惑,真是中毒不浅... 设每一个数都可以被分成若干个$k$和$k+1$的和.数$x$能够被分成若干个$k$和$k+1$的和的充要条件是:$x%k<=floo ...

  2. Codeforces554 C Kyoya and Colored Balls

    C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...

  3. 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 ...

  4. 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 ...

  5. Kyoya and Colored Balls(组合数)

    Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. 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 ...

  7. 554C - Kyoya and Colored Balls

    554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...

  8. 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 ...

  9. Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

随机推荐

  1. ava Double: 四舍五入并设置小数点位数

    public static void main(String[] args) { // 1. 先乘后四舍五入, 再除; double d = 62.31060027198647; double d2 ...

  2. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】

    要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...

  3. 洛谷 P2770 航空路线问题【最大费用最大流】

    记得cnt=1!!因为是无向图所以可以把回来的路看成另一条向东的路.字符串用map处理即可.拆点限制流量,除了1和n是(i,i+n,2)表示可以经过两次,其他点都拆成(i,i+n,1),费用设为1,原 ...

  4. 前缀和小结 By cellur925

    这篇主要是来介绍前缀和的QAQ. 前缀和有一维的和二维的,一维的很容易理解,高中数学必修5第二章数列给出了前n项和的概念,就是前缀和.一维的我们在这里简单说一句. 一维前缀和 预处理:在输入一个数列的 ...

  5. Python3中使用urllib的方法详解(header,代理,超时,认证,异常处理)_python

    我们可以利用urllib来抓取远程的数据进行保存哦,以下是python3 抓取网页资源的多种方法,有需要的可以参考借鉴. 1.最简单 import urllib.request response = ...

  6. JDK与JRE、JVM三者间的关系及JDK的安装部署

    JDK与JRE.JVM三者间的关系及JDK的安装部署 一.JDK与JRE.JVM三者间的关系 JDK(Java Development Kit)是针对Java开发员的产品,是整个Java的核心,包括了 ...

  7. Hdu 5352 MZL's City (多重匹配)

    题目链接: Hdu 5352 MZL's City 题目描述: 有n各节点,m个操作.刚开始的时候节点都是相互独立的,一共有三种操作: 1:把所有和x在一个连通块内的未重建过的点全部重建. 2:建立一 ...

  8. ACM_查找ACM(加强版)

    查找ACM(加强版) Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为一个acmer,应该具备团队合作能力和分析问题能力.给你 ...

  9. 在Windows上部署Zabbix客户端

    将Zabbix for Windows客户端拷贝到windows系统的c盘,修改配置文件的相关配置项后,打开cmd窗口执行: # 安装服务 c:\zabbix\bin\win32\zabbix_age ...

  10. google breakpad 使用初步总结

    项目地址:https://code.google.com/p/google-breakpad/    访问不了请挂VPN 这是一个由google主导的开源项目,官方介绍为:An open-source ...