hdu--2111--Saving HDU(贪心)

#include
#include
#include
using namespace std;
struct gem{
    int w,v;
    gem():w(0),v(0){};
    bool operator<(const gem &a)const{
        return this->w>a.w;
    }
};
int main()
{
    int v,n;
    while(cin>>v &&v!=0){
        cin>>n;
        vector vec;
        for(int i=0;ii){
            gem temp;
            cin>>temp.w>>temp.v;
            vec.push_back(temp);
        }
        sort(vec.begin(),vec.end());
        int sum=0;
        int t=0;
        for(auto it=vec.begin();it!=vec.end();++it){
            if((sum+(*it).v)<=v){
                sum+=(*it).v;
                t+=(*it).v*(*it).w;
            }else{
                t+=(v-sum)*(*it).w;break;
            }            
        }
        cout<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/evidd/p/7251894.html

你可能感兴趣的:(hdu--2111--Saving HDU(贪心))