1 条题解

  • 2
    @ 2023-7-1 11:45:03

    一道贪心题。我最开始想复杂了,实际上就一句话:能优惠,则优惠

    那么我们可以先按照衣服的优惠价排序,前 kk 件衣服按优惠价买。然后再将不可优惠的衣服放入另一个数组中,将这个数组按原价排序,这些衣服按原价买。

    #include <bits/stdc++.h>
    using namespace std;
    struct Clothes{
        int p,q;
    }a[50001],b[50001];
    int n,k,id,ans;
    long long m;
    bool cmp(Clothes x,Clothes y){
        return x.q<y.q;
    }
    bool comp(Clothes x,Clothes y){
        return x.p<y.p;
    }
    int main(){
        cin>>n>>k>>m;
        for (int i=1;i<=n;i++){
            cin>>a[i].p>>a[i].q;
        }
        sort(a+1,a+n+1,cmp);//按优惠价排序
        for (int i=k+1;i<=n;i++){
            b[++id]=a[i];//将不可优惠的衣服放入b数组
        }
        sort(b+1,b+id+1,comp);//按原价排序b数组
        for (int i=1;i<=k;i++){//前k件衣服按优惠价
            if (m<a[i].q){
                cout<<ans;
                return 0;
            }
            m-=a[i].q;
            ans++;
        }
        for (int i=1;i<=id;i++){//其他衣服按原价
            if (m<b[i].p){
                cout<<ans;
                return 0;
            }
            m-=b[i].p;
            ans++;
        }
        cout<<ans;
        return 0;
    }
    
    • 1

    信息

    ID
    576
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    138
    已通过
    43
    上传者