反悔贪心
2026-09-11 19:13:23
发布于:北京
4阅读
0回复
0点赞
先按时间从小到大排序,再怎么排序都可以。因为优先队列会维护当前选择最优 代码如下:
#include<bits/stdc++.h>
using namespace std;
struct s{
int w,t;
}a[1000];
bool cmp(s x,s y){
if(x.t!=y.t)return x.t<y.t;
return x.w<y.w;
}
signed main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].t;
}
for(int i=1;i<=n;i++){
cin>>a[i].w;
}
sort(a+1,a+n+1,cmp);
priority_queue<int,vector<int>,greater<int>>pq;
int ans=0;
for(int i=1;i<=n;i++){
//cout << a[i].t<<" "<<a[i].w<<endl;
if(pq.size()<a[i].t){
pq.push(a[i].w);
ans+=a[i].w;
}
else{
if(a[i].w>pq.top()){
ans-=pq.top();
pq.pop();
pq.push(a[i].w);
ans+=a[i].w;
}
}
}
cout << ans;
return 0;
}
这里空空如也







有帮助,赞一个