E. Colored Balls

https://codeforces.com/problemset/problem/792/E

最后肯定是全是x,x+1
对于颜色i,如果它分成的set数>=sqrt(ai),是一定可行的,因为ai%sqrt(a[i])<sqrt(a[i])<=ai/sqrt(a[i])
所以我们枚举最小的ai分成的set数k 1到sqrt(a[i]),这样就可以确定x,然后去check(x),如果ai%k==0,也要check(x-1)

#include <bits/stdc++.h>
#define inf 2333333333333333
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)
//by war
//2020.11.20
using namespace std;
long long n,t,x,ans;
long long a[N];
void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

long long check(long long x){
    long long tot=0;
    For(i,1,n){
        if(a[i]/x>=a[i]%x) tot+=a[i]/x-(a[i]/x-a[i]%x)/(x+1);
        else return inf;
    }
    return tot;
}

signed main(){
    in(n);
    For(i,1,n) in(a[i]);
    sort(a+1,a+n+1);
    t=sqrt(a[1]);ans=inf;
    For(i,1,t){
        x=a[1]/i;
        if(a[1]%i==0 && x>1) ans=min(ans,check(x-1));
        ans=min(ans,check(x));
    }
    o(ans);
    return 0;
}
原文地址:https://www.cnblogs.com/war1111/p/14013080.html