【CF contest/792/problem/E】
E. Colored Ballstime limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n boxes with colored balls on the table. Colors are numbered from 1 to n. i-th box contains ai balls, all of which have color i. You have to write a program that will divide all balls into sets such that:
- each ball belongs to exactly one of the sets,
- there are no empty sets,
- there is no set containing two (or more) balls of different colors (each set contains only balls of one color),
- there are no two sets such that the difference between their sizes is greater than 1.
Print the minimum possible number of sets.
InputThe first line contains one integer number n (1 ≤ n ≤ 500).
The second line contains n integer numbers a1, a2, ... , an (1 ≤ ai ≤ 109).
OutputPrint one integer number — the minimum possible number of sets.
Examplesinput3
4 7 8output5input2
2 7output4NoteIn the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color.
【分析】
先%一下大颓果。。
这种题自己想分配方法都要对拍一下验证啊不然很容易错。。
假设你分成x和x+1,对于某个A来说,判断是否成立的条件是A mod x <= [A / x] 或者 A mod (x+1)==0嘛。。
后面那个直接判断就好了,考虑前面那个代表什么。
就是A-[A/x]*x<=[A/x]
即A/(x+1)<=[A/x] 即 [A/(x+1)]<[A/x]
就是说A/(x+1)和A/x要不一样。
我们去取最小的A,不同的A/x 只有2根号n个,枚举然后for一遍判断就好了。
然后你求出x最大多少,算ans。【我就是这里错了TAT
若ans%(x+1)==0 ,直接分成x+1份。
否则先分成x份,A%x放入每堆中,剩下可能还有很多堆x,每(x+1)堆x的可以变成x堆(x+1)的,所以减掉 剩下的/(x+1),求出答案即可。
时间(根号A * n)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 510
#define INF 0x7fffffff
#define LL long long int mymin(int x,int y) {return x<y?x:y;}
int mymax(int x,int y) {return x>y?x:y;} int a[Maxn],n; bool check(int x)
{
for(int i=;i<=n;i++)
{
bool p=;
if(a[i]%x==||a[i]%(x+)==||a[i]/x!=a[i]/(x+)) p=;
if(!p) return ;
}
return ;
} int main()
{
// int T;
// scanf("%d",&T);
// while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
int mn=INF,ans=;
for(int i=;i<=n;i++) mn=mymin(mn,a[i]);
for(int i=mn;i>=;)
{
if(i<ans) break;
int x=mn/i,r=mn/(x+);
if(check(i)) ans=mymax(ans,i);
i=r;
}
for(int i=;i<=mn;)
{
if(mn/i<ans) break;
int x=mn/i,r=mn/x;
if(check(x)) ans=mymax(ans,x);
else if(check(x-)) ans=mymax(ans,x-);
i=r+;
}
LL sm=;
for(int i=;i<=n;i++)
{
if(a[i]%(ans+)==) sm+=a[i]/(ans+);
else
{
int xx=a[i]/ans,yy=a[i]%ans;
// xx-=yy;
sm+=xx-(xx-yy)/(ans+);
}
// sm+=(a[i]/(ans+1))+(a[i]%(ans+1)!=0);
}
printf("%I64d\n",sm);
}
return ;
}
2017-04-19 10:02:16
【CF contest/792/problem/E】的更多相关文章
- 【cf contest 1119 H】Triple
题目 给出 \(n\) 个三元组\(\{ a_i,b_i,c_i \}\)和\(x,y,z\): 将每个三元组扩展成(\(x\)个\(a_i\),\(y\)个\(b_i\),\(z\)个\(c_i\) ...
- 【cf contest 1119 G】Get Ready for the Battle
题目 你有\(n\)个士兵,需要将他们分成\(m\)组,每组可以为0: 现在这些士兵要去攻打\(m\)个敌人,每个敌人的生命值为\(hp_i\) : 一轮游戏中一组士兵选定一个攻打的敌人,敌人生命值- ...
- 【CF 676B Pyramid of Glasses】模拟,递归
题目链接:http://codeforces.com/problemset/problem/676/B 题意:一个n层的平面酒杯金字塔,如图,每个杯子的容量相同.现在往最顶部的一个杯子倒 t 杯酒,求 ...
- 【CF Round 434 A. k-rounding】
Time limit per test1 second memory limit per test 256 megabytes input standard input output standard ...
- 【CF Round 429 B. Godsend】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【CF Manthan, Codefest 17 B】Marvolo Gaunt's Ring
[链接]h在这里写链接 [题意] 给你n个数字; 让你在其中找出三个数字i,j,k(i<=j<=k); 使得p*a[i]+q*a[j]+r*a[k]最大; [题解] /* 有一个要 ...
- 【CF Manthan, Codefest 17 A】Tom Riddle's Diary
[链接]h在这里写链接 [题意] 在这里写题意 [题解] /* Be careful. 二重循环枚举 */ [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/st ...
- 【luogu P4137 Rmq Problem / mex】 题解
题目链接:https://www.luogu.org/problemnew/show/P4137 求区间内最大没出现过的自然数 在add时要先判断会不会对当前答案产生影响,如果有就去找下一个答案. # ...
- 【CF edu 30 A. Chores】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
随机推荐
- 【计蒜客】是男人就过 8 题--Pony.AI 题 A. A String Game 后缀自动机+SG函数
[题目]A. A String Game [题意]给定目标串S和n个子串Ti,Alice和Bob轮流选择一个子串操作,必须且只能在子串末尾添加一个字符使得新串也是S的子串,不能操作即输,求胜利者.|S ...
- 计算1到N中各个数字出现的次数 --数位DP
题意:给定一个数n,问从1到n中,0~9这10个数字分别出现了多少次.比如366这个数,3出现了1次,6出现了2次. 题解:<剑指offer>P174:<编程之美>P132 都 ...
- Spring Data JPA笔记
1. Spring Data JPA是什么 Spring Data JPA是Spring Data大家族中的一员,它对对持久层做了简化,用户只需要声明方法的接口,不需要实现该接口,Spring Dat ...
- IDEA常见错误
1. inspects a maven model for resolution problems 在添加Maven依赖的时候,报了inspects a maven model for resolut ...
- Django之前端插件定制之表头
什么是插件? 插件只是辅助,是开发过程中的一个阶段.一般项目一期会用各种插件,迅速将功能.界面搭出来,二期时就改成自己的代码了.大点的公司都有自己的js库,自己开发类似jquery的库. 那接下来就写 ...
- 【IDEA】与Eclipse "Link with Editor"等价功能设置
Link With Editor是Eclipse内置功能中十分小巧,但却异常实用的一个功能.这个开关按钮 (Toggle Button) 出现在各式导航器视图 ( 例如 Resource Explor ...
- Qualcom QMI系列-基本知识介绍(转)
1 引言1.1 编写目的 介绍Qualcom QMI 基本知识,API使用,设计原理,基于QMI的RemoteEfs(NV)分析1.2 阅读建议 高通平台入门1.3 参考资料 ...
- tp 框架 利用反射实现对象调用方法
<?php class Person{ public $name="xiaoming"; function say(){ echo "i am ".$th ...
- C/C++——[03] 注释
C/C++源程序中被注释的内容不能被编译,被认为是不属于程序的一部分. C/C++的注释有两种写法: 多行注释:以 “ /*”开头,以“ */”结尾: #include <stdio.h> ...
- Lynx以纯文本的形式下载网页
Lynx是一款基于命令行的web浏览器 [root@test88 ~]# yum install lynx -y [root@test88 ~]# lynx www.baidu.com 以纯文本的形式 ...