20250503 UVA1316 / B3872 / P1230题解
数据范围更大版本:P2949(这题用如下思路无法通过)。
贪心:
- 按价格从大到小排序,每一件商品尽量晚卖出去。
- 对于商品
(p_i,d_i) ,尽量在第d_i 天卖出去。 - 如果
d_i 已经被占用,则从后往前找到第一个可以卖出的天卖出。
#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;
}