Codeforces 1223C

題意比較難以描述,故略;
考慮到用k個如果成立,那麼k+1覺得成立。因而可以想到二分。考慮貢獻時,先上a,b的lcm,在上max(a,b),最後上min(a,b)
兩個2e5數的lcm有可能爆int,需要注意。

#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const int N =2e5 +10;
int p[N],x,y,a,b,n,q;
LL k,lc;
int gcd(int xx,int yy){
    return !yy ? xx : gcd (yy ,xx % yy);
}
LL lcm(int xx,int yy){
    return (LL)xx * yy / gcd(xx,yy);
}
int check(int num){
    int num1 = (LL)num / lc;
    int num2 = num / a;
    int num3 = num / b;
    LL tot = 0;
    int i = 0;
    for(;i < num1 && i < n;i ++)  tot += (LL)(x + y) * p[i];
    for(int cnt = 0  ; i < n && cnt < num2 - num1 ; i++ ,cnt++) tot += (LL)x * p[i];
    for(int cnt = 0 ; i < n && cnt < num3 - num1 ; i++ ,cnt++) tot += (LL)y * p[i];
    //cout<<num<<' '<<num1<<' '<<num2<<' '<<num3<<' '<<tot<<endl;
    return tot >= k;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("data.in","r",stdin);
	//freopen("data.out","w",stdout);
	cin>>q;
    while(q--){
        cin>>n;
        for(int i = 0 ;i < n ;i ++)  {
            cin>>p[i];
            p[i] /= 100;
        }
        sort(p,p+n);
        reverse(p,p+n);
        cin>>x>>a>>y>>b>>k;
        if(x < y) swap(x,y),swap(a,b);
        lc = lcm(a,b);
        int flag = 0 ;
        int l = 1, r = n ;
        while(l <= r){
            int mid = (l + r) / 2;
            if(check(mid)) flag = 1, r = mid - 1;
            else l = mid + 1;
        }
        if(flag) cout<<l<<endl;
        else cout<<-1<<endl;
    }
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章