T541183题解

· · 个人记录

\color{000000}{T541183} 题解

这题很简单,只需要将所有敌人按暴力值排个序,将还没花完的能量依次减去,最后排回来即可。

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+5;
struct dellar{
    string o;
    int p;
    int dsize;
    bool flag=false;
}a[MAX];
int n,m;
bool cmp1(dellar x,dellar y){
    return x.p<y.p;
}
bool cmp2(dellar x,dellar y){
    return x.dsize<y.dsize;
}
int main(){
    cin>>n>>m;
    if(n==0){
        cout<<0;
        exit(0);
    }
    for(int i=0;i<n;i++)cin>>a[i].o>>a[i].p,a[i].dsize=i;
    sort(a,a+n,cmp1);
    int i=0;
    while(m-a[i].p>=0 && i<n){
        m-=a[i].p;
        a[i].flag=true;
        i++;
    }
    cout<<i<<endl;
    sort(a,a+n,cmp2);
    for(int i=0;i<n;i++){
        if(a[i].flag)cout<<a[i].o<<endl;
    }
    return 0;
}

如果你也要提交题解,欢迎在下方留言!