题目大意:将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. HDU 5944 Fxx and string (暴力)

    题意:给定一个字符串,问有多少个三元组满足 i, j, k组成一个等比数列,并且s[i] = 'y', s[j] = 'r', s[k] = 'x',且j/i ,j/k中至少一个是整数. 析:直接暴力 ...

  2. UVa 1471 Defense Lines (二分+set优化)

    题意:给定一个序列,然后让你删除一段连续的序列,使得剩下的序列中连续递增子序列最长. 析:如果暴力枚举那么时间复杂度肯定受不了,我们可以先进行预处理,f[i] 表示以 i 结尾的连续最长序列,g[i] ...

  3. rpm安装总结(转载)

    转自:http://www.cnblogs.com/nuke/archive/2009/03/03/1402067.html 在RedHat Linux和Mandrake等兼容RedHat的发行版中, ...

  4. Ruby Time类和Date类

    Time类 更新: 2017/06/23 更新了Data/Time在model模式下的便利方法 更新: 2018/10/12 修改了%Y相关描述防止误解  年月日时分秒,时区    生成  获取当前时 ...

  5. c语言程序设计案例教程(第2版)笔记(四)—指针、分配存储空间、文件

    零散知识点: 指针类型:每个变量占用的首单元地址称为这个变量的存储地址. “&”为“取地址运算符”.格式:&  变量名.功能:返回指定变量的存储地址. “*”为“取内容运算”.格式:* ...

  6. 浅谈算法——Manacher

    字符串算法在各大高级比赛中均有用到,所以,学习好字符串算法对我们而言十分重要.那么,今天我们就给大家介绍一个快速求回文串的算法,Manacher算法,我们也习惯性叫它马拉车算法. 一.引入 首先我们要 ...

  7. 用WEKA进行数据挖掘

    学习于IBM教学文档 数据挖掘学习与weka使用 第二部 分分类和集群 分类 vs. 群集 vs. 最近邻 在我深入探讨每种方法的细节并通过 WEKA 使用它们之前,我想我们应该先理解每个模型 - 每 ...

  8. 题解报告:hdu 1236 排名

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1236 Problem Description 今天的上机考试虽然有实时的Ranklist,但上面的排名 ...

  9. Android开机自启动

    1.原理 当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为 android.intent.action.BOOT_COMPLETED. ...

  10. js中toFixed重写

    在测试原生的toFixed发现,它在个浏览器上表现不一致,并且有些值在保留小数时得到的结果并不是想要,如在chrome下测试: 所以针对toFixed方法不准的问题,我们进行方法改造: 主要思路是:对 ...