思路
对于每缺一单位的土,我们有两种选择
1.买1单位土
2.从别的地方转移过来一单位的土
同理,每多一单位的土
1.移除1单位土
2.从别的地方转移出去一单位的土
在这两种方案中要取最优的方案
可以用两个大根堆堆顶来记录,q1表示多土的花盆 q2表示缺土的花盆
根据贪心来选择最优的方案
代码
// Problem: Landscaping
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/problem/24444
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
#define debug(a) cout<<#a<<":"<<a<<"\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=100010;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);
int n;
ll x,y,z,ans;
priority_queue<ll> q1,q2; //q1表示多土的花盆 q2表示缺土的花盆
void solve(){
cin>>n>>x>>y>>z;
rep(i,1,n){
int a,b;cin>>a>>b;
if(a<b){ //缺土
rep(j,1,b-a){
if(q1.empty()||i*z-q1.top()>=x){
ans+=x;
// debug(ans);
q2.push(i*z+x);
}
else{
ll t=q1.top();q1.pop();
ans+=i*z-t;
// debug(ans);
q2.push(i*z*2-t);
}
}
}
else{
rep(j,1,a-b){
if(q2.empty()||i*z-q2.top()>=y){
ans+=y;
// debug(ans);
q1.push(i*z+y);
}
else{
ll t=q2.top();q2.pop();
ans+=i*z-t;
// debug(ans);
q1.push(i*z*2-t);
}
}
}
}
cout<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
// int t;cin>>t;while(t--)
solve();
return 0;
}

京公网安备 11010502036488号