Luogu P3545 [POI 2012] HUR-Warehouse Store(反悔贪心)
题面:P3545 [POI 2012] HUR-Warehouse Store
思路
考虑贪心,试图尽可能满足当天顾客。这么想的问题在于:在部分情况下,如果不去满足当天顾客,省下的商品可以满足更多的顾客。
改进的想法是:如果不能满足当天顾客,就找出之前需求商品最多的顾客,如果放弃他可以满足今天的需求就放弃他。可行性在于,如果放弃再满足,满足的人数不会下降,可用的商品也会增多,满足贪心原理。这种做法叫做“反悔贪心”。
代码:(十年OI一场空,不开long long
见祖宗)
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#define int long long
namespace just{
struct CMP{
bool operator()(pair<int,int> a,pair<int,int> b){
return a.second<b.second;
};
};
std::priority_queue<pair<int,int>,vector<pair<int,int>>,CMP> heap;
const int N=250003;
int n,a[N],b,an;
bitset<N> vis;
void monika(){
cin >> n;
for(int i=1;i<=n;i++){
cin >> a[i];
}
for(int i=1;i<=n;i++){
cin >> b;
an+=a[i];
if(an>=b){
heap.push({i,b});
an-=b;
vis[i]=1;
}else{
if(heap.size()>0){
auto t=heap.top();
if(t.second>b){
heap.pop();
an+=t.second-b;
heap.push({i,b});
vis[i]=1;
vis[t.first]=0;
}
}
}
}
cout << heap.size() << '\n';
for(int i=1;i<=N;i++){
if(vis[i])cout << i << ' ';
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
just::monika();
return 0;
}