Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals)

C. Bamboo Partition
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is ai meters high.

Vladimir has just planted n bamboos in a row, each of which has height 0 meters right now, but they grow 1 meter each day. In order to make the partition nice Vladimir can cut each bamboo once at any height (no greater that the height of the bamboo), and then the bamboo will stop growing.

Vladimir wants to check the bamboos each d days (i.e. d days after he planted, then after 2d days and so on), and cut the bamboos that reached the required height. Vladimir wants the total length of bamboo parts he will cut off to be no greater than k meters.

What is the maximum value d he can choose so that he can achieve what he wants without cutting off more than k meters of bamboo?

Input

The first line contains two integers n and k (1 ≤ n ≤ 1001 ≤ k ≤ 1011) — the number of bamboos and the maximum total length of cut parts, in meters.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the required heights of bamboos, in meters.

Output

Print a single integer — the maximum value of d such that Vladimir can reach his goal.

Examples
input
3 4
1 3 5
output
3
input
3 40
10 30 50
output
32
Note

In the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days, and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters.


官方题解:


second fact:式子a/b向上取整的所有取值个数与sqrt(a)是同阶的。因此, 对于固定的a值,其复杂度为sqrt(a);

然后,对于每一个a[i],求出它的所有的取值,如上图所示。对于重复的取值,我们保留一个即可。

最后,所求得的d值需要满足不等式的K值要求。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e2+17;
const int M=1e5+5;
const ll inf=1e12;
int a[N];
set<int>b;
int ceil(int x,int r){
    return (x+r-1)/r;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    ll k;
    int n;
    cin>>n>>k;
    for(int i=0;i<n;++i){
        cin>>a[i];
        k+=a[i];
        for(int j=1;j*j<=a[i];++j){
            b.insert(j);
            b.insert(ceil(a[i],j));
        }
    }
    ll ans=0;
    set<int> ::iterator it;
    for(it=b.begin();it!=b.end();++it){  
        int l=*it;  
        ll tmp=0;
        for(int j=0;j<n;++j){
            tmp+=ceil(a[j],l);
        }
        ll d=k/tmp;
        if(d>=l&&d>ans){ 必须有l*tmp<=k,故需要d=k/tmp>=l满足条件。
            ans=d;
        }
    }
    cout<<ans<<'\n';
    return 0;
}


猜你喜欢

转载自blog.csdn.net/u011721440/article/details/76675103
今日推荐