题面

sol:(思想):开一个大根堆和一个小根堆,每次计算到下了一个加油站用掉的油时尽量用小根堆中的元素,且同时删去大根堆中的相应位置的元素,当前加油站如果足够便宜,就可以把大根堆中的元素替换掉;

(实现):显然不可以开两个堆,因为删除是瓶颈,就可以用一下双端队列,右小左大,用油是从右边弹出,更新时从左边开始弹出

#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
const long long N=;
long long n,G,B,D;
struct node{long long x,y;}a[N];
inline bool cmp(node a,node b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
struct oll{long long num,w;};
deque<oll>q;
signed main()
{
long long i,ny,np,re=0LL; oll tmp;
scanf("%lld%lld%lld%lld",&n,&G,&B,&D);
for(i=;i<=n;i++)
{
scanf("%lld%lld",&a[i].x,&a[i].y);
}sort(a+,a+n+,cmp);
if(a[].x>B||D-a[n].x>G)return *printf("-1\n");
for(i=;i<=n;i++)if(a[i].x-a[i-].x>G)return *printf("-1\n");
if(B>=D)return *printf("0\n"); if(a[n].x==D)a[n].y=; else a[++n].x=D;
a[n].y=; ny=B; q.push_back((oll){B,});
for(i=;i<=n;i++)
{
long long dd=a[i].x-a[i-].x;
while(!q.empty()&&dd>)
{
tmp=q.front(); q.pop_front();
if(tmp.num>dd)
{
ny-=dd; q.push_front((oll){tmp.num-dd,tmp.w}); break;
}dd-=tmp.num; ny-=tmp.num;
}
if(i==n)
{
while(!q.empty())
{
re=re-1LL*q.front().num*q.front().w; q.pop_front();
}break;
}
while(!q.empty()&&q.back().w>a[i].y)
{
re-=q.back().num*q.back().w; ny-=q.back().num; q.pop_back();
}q.push_back((oll){G-ny,a[i].y}); re=(long long)re+1LL*(G-ny)*a[i].y; ny=G;
}printf("%lld\n",re);
}
05-11 22:12