Restorer Distance time limit per test:1 seconds memory limit per test:256 megabytes Problem Description You have to restore the wall. The wall consists of N pillars of bricks, the height of the i-th pillar is initially equal to hi, the height is measured in number of bricks. After the restoration all the N pillars should have equal heights.
You are allowed the following operations:
- put a brick on top of one pillar, the cost of this operation is A;
- remove a brick from the top of one non-empty pillar, the cost of this operation is R;
- move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is M.
What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?
Input The first line of input contains four integers N, A, R, M ( 1 ≤ N ≤ 1 0 5 1≤N≤10^5 1≤N≤105,0 ≤ A , R , M ≤ 1 0 4 0≤A,R,M≤10^4 0≤A,R,M≤104) — the number of pillars and the costs of operations.
The second line contains N integersh i h_i hi? ( 0 ≤ h i ≤ 1 0 9 0≤h_i≤10^9 0≤hi?≤109) — initial heights of pillars.
Output Print one integer — the minimal cost of restoration.
Sample Input 3 1 100 100
1 3 8
Sample Output 【二分|CodeForces_1355E Restorer Distance(三分)】12
题意 有n个柱子,每个柱子初始高度为ai.可进行一下三种操作:
- 将一个柱子的高度加一,代价为a
- 将一个柱子的高度减一,代价为r
- 将一个柱子的高度加一并把另一个柱子的高度减一,代价为m
题解 设最终高度为h,则可以遍历求出需要进行增加操作up次,减少操作dn次。若a+r>m,则可以将min(up,dn)次的增加操作和min(up,dn)次的减少操作变为min(up,dn)次转移操作。计算代价和,即可求出所有柱子高度变为h的最少花费。
最终高度h和最少花费的关系图形类似 y = x 2 y=x^2 y=x2的图形,三分求最优解即可。
#include
#include
#include
#include
#include
#include
#include
推荐阅读
- 二分|二分-牛客寒假集训营5-B-牛牛战队的比赛地
- #|Codeforces Round #532 (Div. 2) E. Andrew and Taxi(二分+拓扑排序)
- #|CodeForces - 1059D Nature Reserve
- online|hdu1816Get Luffy Out *
- 其他|Codeforces Round #316 (Div. 2)-D. Tree Requests-DFS+二分+hash
- 其他|Codeforces Round #277.5 (Div. 2)E题
- Codeforces|Codeforces Round #514 (Div. 2)-D. Nature Reserve
- hdu5256序列严格递增