Technocup 2020 - Elimination Round 1 C. Save the Nature

链接:https://codeforces.com/contest/1223/problem/C
题意:题意好难描述呐,就是给你n张票,你可以改变他们的顺序,然后有x,a,y,b你必须按顺序选一些票让他达到k,只能选a,b的整数倍,能得x%,y%的钱,问你最少需要多少张票。
(虽然这场div2被鸽了,晚上十一点打到一点的比赛,但是作为一个owl,还是打的不亦乐乎。)
解题思路:从一开始想贪心做但是写了一半回头看题发现并不能做到,然后看了一眼广大群友,有人说二分,顿时恍然大悟,把刚自己删掉的贪心有撤销了,把他放到check函数里二分。。。。然鹅中途竟然因为该死的变主参数导致debug了半天,哇,长达一个小时我就在那里ctrl shift +n ,ctrl shift +k,非常酷,巨大的debug经验,在外部函数里想变参量一定要重新设参数。


#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+9;
typedef long long ll;
ll p[maxn];
int n;
ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
bool cmp(ll a,ll b)
{
    return a>b;
}
ll x,y,a,b,k,temp;
bool check(int n)
{
// cout<<"mid=="<<n<<endl;
        ll ans=0;
        int num1=n/temp;
// if(a>b)swap(a,b);
        int num2=n/a-num1;
        int num3=n/b-num1;
        int xx=x,yy=y;
// cout<<"yx="<<y<<" "<<x<<endl;
// cout<<"num "<<num1<<" "<<num2<<" "<<num3<<endl;
        if(yy>xx)
        {
            swap(num2,num3);
            swap(xx,yy);
        }
// cout<<"yx="<<y<<" "<<x<<endl;
// cout<<"num "<<num1<<" "<<num2<<" "<<num3<<endl;
        for(int i=0;i<n;i++)
        {
            if(num1)
            {
                ans=ans+p[i]*(xx+yy);
                num1--;
// cout<<"debug="<<p[i]<<" "<<x<<" "<<y<<endl;

                continue;
            }
            if(num2)
            {
                ans+=p[i]*xx;
                num2--;
// cout<<"x=="<<p[i]<<" "<<x<<endl;

                continue;
            }
            if(num3)
            {
                ans+=p[i]*yy;
// cout<<"y=="<<p[i]<<" "<<y<<endl;
                num3--;
                continue;
            }
        }
// cout<<"ans=="<<ans<<endl;
        if(ans>=k)
        {
// cout<<"***"<<endl;
            return true;
        }
// cout<<"qqq"<<endl;
        return false;
}

int main()
{
// cout<<90*100/gcd(100,90)<<endl;
    int q;
    cin>>q;
    while(q--)
    {
        memset(p,0,sizeof p);
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>p[i];
            p[i]/=100ll;
        }
        cin>>x>>a;
        cin>>y>>b;
        cin>>k;
        temp = a*b/gcd(a,b);
        sort(p,p+n,cmp);
// cout<<"temp=="<<temp<<endl;
// int ans=0;

        int l=0,r=n;int ans=-1;
        while(l<=r)
        {
            int mid=l+r>>1;

            if(check(mid)) {
                ans=mid,    r=mid-1;
            }
            else{
                l=mid+1;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务