NC50995 Supermarket(set+贪心)

题目比较水,但是为了给我的tag加点量就水一篇题解

这题一个暴力的贪心就是按照利润排序,然后对于大的利润去找能满足条件的最晚的一天

这样的复杂度比较高,找的过程可以可以用set自带的二分去做,这样就是nlogn了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int N=2e5+10;
const int inf=1e9;
const int mod=1e9+7;
struct node{
    int a,b;
    bool operator <(const node &t) const{
        return a>t.a;
    }
}s[N];
int st[N];
set<int> m1;
int main(){
    ios::sync_with_stdio(false);
    int i;
    int n;
    while(cin>>n){
        ll ans=0;
        int mx=0;
        m1.clear();
        for(i=1;i<=n;i++){
            cin>>s[i].a>>s[i].b;
            mx=max(mx,s[i].b);
        }
        for(i=1;i<=mx;i++)
            m1.insert(-i);
        sort(s+1,s+1+n);
        for(i=1;i<=n;i++){
            int x=s[i].b;
            if(m1.empty())
                break;
            auto it=m1.lower_bound(-x);
            if(it!=m1.end()){
                ans+=s[i].a;
                m1.erase(it);
            }
            else{
                continue;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
View Code
没有人不辛苦,只有人不喊疼
原文地址:https://www.cnblogs.com/ctyakwf/p/14394514.html