5043. 【NOI2017模拟4.4】保持平衡

来源:互联网 发布:信义安所见乎翻译 编辑:程序博客网 时间:2024/05/21 07:14

Description

博爱路上种起了一棵棵的大树,但是有一些地方的树超过了负荷,有一些地方的树的数量又不够。
我们不妨把博爱路看做一条数轴,数轴有n个点,从1到n编号,第i个位置原来现在有ai棵树,这个位置的需求是bi棵树。ai,bi都是0到10的整数。由于你需要是这个位置的树的数量保持平衡,所以你需要移除或者搬一些树过来。
我们怎么使树的数量平衡呢?
首先,你可以从某个位置i移动一棵树到位置j,这时,你需要的运费是|i-j|*z元。
其次,你可以从商店买一棵树,需要支付x元,这时商店会把树配送到任意位置。
还有就是,你可以叫别人收购在任意位置一棵树,需要支付y元运费。
问使得树的数量平衡最小需要支付多少钱?

Input

第一行包含4个整数,n,x,y,z。
接下来n行,每一行包含两个整数,ai和bi。

Output

输出一个整数,表示最小花费。

Sample Input

4 100 200 1
1 4
2 3
3 2
4 0

Sample Output

210
样例解释:从4位置运3棵树到1位置,花费3*3=9。从3位置运1颗树到2位置,花费1*1=1。
叫别人收购4位置的一棵树,花费200。

题解

既然是贪心专题,自然用贪心,又因为是算最小,就可以用堆维护;
维护两个堆,一个记录供应,另一个记录需求,每次需要时,就取堆顶(贪心,符合堆的性质),再将撤销操作加入另一个堆(-i*z-sum(最优操作)),即可(每次记得比较堆顶与x,y)。

var        n,x,y,z,i,a,b,tot,tott,tt,sum:longint;        ans:int64;        d,dd:array[0..200000]of int64;procedure up(x:longint);begin        while (x>1)and(d[x]<d[x div 2]) do        begin                tt:=d[x];                d[x]:=d[x div 2];                d[x div 2]:=tt;                x:=x div 2;        end;end;procedure down(x:longint);var        y:longint;begin        while ((x*2<=tot)and(d[x]>d[x*2]))or((x*2+1<=tot)and(d[x]>d[x*2+1])) do        begin                y:=x*2;                if (d[y]>d[y+1])and(y+1<=tot) then                        inc(y);                tt:=d[x];                d[x]:=d[y];                d[y]:=tt;                x:=y;        end;end;procedure up1(x:longint);begin        while (x>1)and(dd[x]<dd[x div 2]) do        begin                tt:=dd[x];                dd[x]:=dd[x div 2];                dd[x div 2]:=tt;                x:=x div 2;        end;end;procedure down1(x:longint);var        y:longint;begin        while ((x*2<=tott)and(dd[x]>dd[x*2]))or((x*2+1<=tott)and(dd[x]>dd[x*2+1])) do        begin                y:=x*2;                if (dd[y]>dd[y+1])and(y+1<=tott) then                        inc(y);                tt:=dd[x];                dd[x]:=dd[y];                dd[y]:=tt;                x:=y;        end;end;begin        readln(n,x,y,z);        for i:=1 to n do        begin                readln(a,b);                dec(a,b);                while a>0 do                begin                        if tot>0 then                        begin                                if i*z+d[1]<y then                                begin                                        sum:=i*z+d[1];                                        d[1]:=d[tot];                                        dec(tot);                                        down(1);                                end                                else                                        sum:=y;                        end                        else                                sum:=y;                        inc(tott);                        dd[tott]:=-i*z-sum;                        up1(tott);                        inc(ans,sum);                        dec(a);                end;                while a<0 do                begin                        if tott>0 then                        begin                                if i*z+dd[1]<x then                                begin                                        sum:=i*z+dd[1];                                        dd[1]:=dd[tott];                                        dec(tott);                                        down1(1);                                end                                else                                        sum:=x;                        end                        else                                sum:=x;                        inc(tot);                        d[tot]:=-i*z-sum;                        up(tot);                        inc(ans,sum);                        inc(a);                end;        end;        writeln(ans);end.
原创粉丝点击