E. Colored Ballstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere 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)
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 #define Maxn 510 8 #define INF 0x7fffffff 9 #define LL long long 10 11 int mymin(int x,int y) {return x<y?x:y;} 12 int mymax(int x,int y) {return x>y?x:y;} 13 14 int a[Maxn],n; 15 16 bool check(int x) 17 { 18 for(int i=1;i<=n;i++) 19 { 20 bool p=0; 21 if(a[i]%x==0||a[i]%(x+1)==0||a[i]/x!=a[i]/(x+1)) p=1; 22 if(!p) return 0; 23 } 24 return 1; 25 } 26 27 int main() 28 { 29 // int T; 30 // scanf("%d",&T); 31 // while(T--) 32 { 33 scanf("%d",&n); 34 for(int i=1;i<=n;i++) scanf("%d",&a[i]); 35 int mn=INF,ans=0; 36 for(int i=1;i<=n;i++) mn=mymin(mn,a[i]); 37 for(int i=mn;i>=1;) 38 { 39 if(i<ans) break; 40 int x=mn/i,r=mn/(x+1); 41 if(check(i)) ans=mymax(ans,i); 42 i=r; 43 } 44 for(int i=1;i<=mn;) 45 { 46 if(mn/i<ans) break; 47 int x=mn/i,r=mn/x; 48 if(check(x)) ans=mymax(ans,x); 49 else if(check(x-1)) ans=mymax(ans,x-1); 50 i=r+1; 51 } 52 LL sm=0; 53 for(int i=1;i<=n;i++) 54 { 55 if(a[i]%(ans+1)==0) sm+=a[i]/(ans+1); 56 else 57 { 58 int xx=a[i]/ans,yy=a[i]%ans; 59 // xx-=yy; 60 sm+=xx-(xx-yy)/(ans+1); 61 } 62 // sm+=(a[i]/(ans+1))+(a[i]%(ans+1)!=0); 63 } 64 printf("%I64d ",sm); 65 } 66 return 0; 67 }
2017-04-19 10:02:16