20250503 UVA1316 / B3872 / P1230题解

· · 题解

数据范围更大版本:P2949(这题用如下思路无法通过)。

贪心:

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

const int N=1e4+2;
bool da[N];

struct sp{
    int p,d;
}s[N];

bool cmp(sp a,sp b){
    return a.p>b.p;
}

int main(){
    int n;
    while(cin>>n){
        memset(da,0,sizeof da);
        for(int i=1; i<=n; i++) cin>>s[i].p>>s[i].d;
        sort(s+1,s+1+n,cmp);int ans=0;
        for(int i=1; i<=n; i++){
            for(int j=s[i].d; j>=1; j--){
                if(!da[j]){
                    da[j]=1;
                    ans+=s[i].p;
                    break;
                }
            }
        }
        cout<<ans<<"\n";
    }
    return 0;
}