结构体的排序

#include<bits/stdc++.h>
using namespace std;

struct node{
    int p,w,v;
    friend bool operator<(node a,node b){
        if(a.v==b.v&&a.p==b.p)
        return a.w<b.w;
        if(a.v==b.v)
        return a.p<b.p;
        return a.v>b.v;

    }
};
vector<node> G;
int main(){
    int p,w,v;
    int n,m,k,i,j,ans;
    while(cin>>n>>m>>k){
        G.clear();
        for(i=0;i<n;i++){
            cin>>p>>w>>v;
            G.push_back((node){p,w,v});
        }
        sort(G.begin(),G.end());
        ans=0;
        for(i=0;i<G.size();i++){
            if(G[i].p<=k&&G[i].w<=m){
                k-=G[i].p;
                m-=G[i].w;
                ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务