突击战 (uva 11729)贪心

思路:就是把J大的放在前面。为什么这样贪心呢?

看看这个图

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct node{
    int x, y;
    bool operator < (const node &b) const{
        return y>a.y;
    }
};
int n, t, x, y;

int main(){
        while(cin>>n, n){
            vector<node>v;
            for(int i=0;i<n;++i){
                cin>>x>>y;      v.push_back((ndoe){x, y});
            }
            sort(v.begin(), v.end());
            
            int res=0, ans=0;
            for(int i=0;i<n;++i){
                res+=v[i].x;
                ans=max(ans, res+v[i].y);
            }
            cout<<"Case "<<++t<<": "<<ans<<endl;
        }
}

猜你喜欢

转载自www.cnblogs.com/ALINGMAOMAO/p/10713553.html