Codeforces-792E Colored Balls(贪心/数学)
2017-03-31 20:03
357 查看
E. Colored Balls
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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.
Input
The first line contains one integer number n (1 ≤ n ≤ 500).
The second line contains n integer numbers a1, a2, ...
, an (1 ≤ ai ≤ 109).
Output
Print one integer number — the minimum possible number of sets.
Examples
input
output
input
output
题解:设最少的球是有a[1]个,那么所有set的容量一定不会超过a[1]+1(任意两个set之间容量差不能超过1),只要枚举1~a[1]+1即可,只不过a[1]<=1e9,因此正常的暴力枚举是没办法过的。设s=sqrt(a[1]),那么要枚举[1,s]是很容易的,但是要枚举[s,s^2]就需要转化一下:
设k∈[s,s^2]为每个set的容量,那么x=a[1]/k就表示第一个球需要用x个set装,可以想到x∈[1,s],于是我们只要再枚举x就等于枚举完k。
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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.
Input
The first line contains one integer number n (1 ≤ n ≤ 500).
The second line contains n integer numbers a1, a2, ...
, an (1 ≤ ai ≤ 109).
Output
Print one integer number — the minimum possible number of sets.
Examples
input
3 4 7 8
output
5
input
2 2 7
output
4
题解:设最少的球是有a[1]个,那么所有set的容量一定不会超过a[1]+1(任意两个set之间容量差不能超过1),只要枚举1~a[1]+1即可,只不过a[1]<=1e9,因此正常的暴力枚举是没办法过的。设s=sqrt(a[1]),那么要枚举[1,s]是很容易的,但是要枚举[s,s^2]就需要转化一下:
设k∈[s,s^2]为每个set的容量,那么x=a[1]/k就表示第一个球需要用x个set装,可以想到x∈[1,s],于是我们只要再枚举x就等于枚举完k。
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int MX = 505; int n, a[MX]; bool Check1(int x, LL& cnt) { if (x <= 0) return false; cnt = 0; for (int i = 1; i <= n; i++) { if (a[i] % x <= a[i] / x) { //每个set装x个球,剩余的球不能超过set的个数 cnt = cnt + (LL)ceil(a[i] * 1.0 / (x + 1)); } else return false; } return true; } int main() { //freopen("in.txt", "r", stdin); while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); LL cnt = 0, ans = 1LL << 60; int sq = (int)sqrt(a[1]) + 1; //相当于枚举第一个球由sqrt(a[1])~a[1]个set装 for (int i = 1; i <= sq; i++) { int now = a[1] / i; if (Check1(now, cnt)) ans = min(ans, cnt); if (a[1] % i == 0) if (Check1(now - 1, cnt)) ans = min(ans, cnt); if (ans != 1LL << 60) break; } //相当于枚举每个set装1~sqrt(a[1])个球 for (int i = sq + 1; i >= 1; i--) { if (Check1(i, cnt)) ans = min(ans, cnt); if (ans != 1LL << 60) break; } printf("%I64d\n", ans); } return 0; }
相关文章推荐
- codeforces 792E —— Colored Balls (贪心,不定方程,数学)
- codeforces - 276D【贪心+数学】
- 【CodeForces 792E】Colored Balls
- (CF 792E Colored Balls) 思维题 贪心
- 【CodeForces 792E】Colored Balls
- 【CodeForces 792E】Colored Balls
- codeforces 491B New York Hotel(贪心,数学)
- Codeforces 891A Pride (dp & 数学 & 贪心 & 暴力)
- CodeForces-450E Jzzhu and Apples 【数学+贪心构造】
- CodeForces - 803C 数学贪心
- 【Codeforces 792E】 Colored Balls 【分块思想】
- Codeforces 626C Block Towers「贪心」「二分」「数学规律」
- Codeforces 549G Happy Line [贪心] [数学]
- 【codeforces 680 D】【数学+贪心+DFS】【求一个不超过 m 的最大体积 X, 每次选一个最大的 x 使得 x3 不超过当前剩余体积。问在能选最多个数的情况下,X 最大是多少】
- CodeForces 712C Memory and De-Evolution(贪心+数学)
- Codeforces 444A DZY Loves Physics(贪心,数学)
- Codeforces 659C Tanya and Toys【贪心】
- CodeForces - 798C Mike and gcd problem (贪心+思维)
- codeforces 777 BGame of Credit Cards(贪心)
- Codeforces 599D Spongebob and Squares(枚举 + 数学)