https://www.acwing.com/problem/content/description/147/
先按照过期时间排序;
定义小根堆(权重)
小根堆代表前q.size(),卖出的商品
遍历每一个商品,如果当前的商品过期时间>q.size() 把商品入堆
反之,比较堆顶元素,若堆顶元素值比商品值小。pop掉,商品入堆
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int n;
typedef pair<int,int> pll;
priority_queue<int,vector<int> , greater<int> > q;
pll store[10000+10];
int main(){
while(cin>>n){
for(int i=0;i<n;i++) cin>>store[i].second>>store[i].first;
sort(store,store+n);
for(int i=0;i<n;i++){
if(store[i].first>q.size()) q.push(store[i].second);
else if(q.top()<store[i].second){
q.pop();
q.push(store[i].second);
}
}
int ans=0;
while(!q.empty()){
ans+=q.top();
q.pop();
}
cout<<ans<<endl;
}
return 0;
}
posted on
浙公网安备 33010602011771号