zoukankan      html  css  js  c++  java
  • [三分]E. Restorer Distance

    E. Restorer Distance

    Description

    You have to restore the wall. The wall consists of NN pillars of bricks, the height of the ii-th pillar is initially equal to hihi, the height is measured in number of bricks. After the restoration all the NN 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 AA;
    • remove a brick from the top of one non-empty pillar, the cost of this operation is RR;
    • move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is MM.

    You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes 00.

    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 NN, AA, RR, MM (1N1051≤N≤105, 0A,R,M1040≤A,R,M≤104) — the number of pillars and the costs of operations.

    The second line contains NN integers hihi (0hi1090≤hi≤109) — initial heights of pillars.

    Output

    Print one integer — the minimal cost of restoration.

    Examples

    Input

    3 1 100 100
    1 3 8

    Output

    12

    正确解法:

    有一堆砖,长度为hi。搬砖使他们的长度都相同。

    hi++ 的代价为A,hi--的代价为R,把一个砖的一个搬到另一个砖的代价为M

    M=min(M,A+R);

    求最小代价。

    仔细想想,假设最终砖的长度为x。

    x增加的时候,代价会变多,x减少的时候,代价也会变多。

    这就出现了三个峰,求最小代价。

    三分。(学过但这是第一次用....)

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <algorithm>
     5 #include <set>
     6 #include <queue>
     7 #include <stack>
     8 #include <string>
     9 #include <cstring>
    10 #include <vector>
    11 #include <map>
    12 //#include <unordered_map>
    13 #define mem( a ,x ) memset( a , x ,sizeof(a) )
    14 #define rep( i ,x ,y ) for( int i = x ; i<=y ;i++ )
    15 #define lson  l ,mid ,pos<<1
    16 #define rson mid+1 ,r ,pos<<1|1
    17 using namespace std;
    18 typedef long long ll ;
    19 typedef pair<int ,int> pii;
    20 typedef pair<ll ,int> pli;
    21 const int inf = 0x3f3f3f3f;
    22 const ll mod=998244353;
    23 const int N=1e5+50;
    24 int T,n;
    25 int A,R,M;
    26 int s[N];
    27 ll check(int x)
    28 {
    29     ll suma=0,sumb=0;
    30     for(int i=1;i<=n;i++)
    31     {
    32         if(s[i]<x)  suma+=x-s[i];
    33         if(s[i]>x)  sumb+=s[i]-x;
    34     }
    35     ll cnt=min(suma,sumb);
    36     suma-=cnt;
    37     sumb-=cnt;
    38     return 1ll*suma*A+1ll*sumb*R+1ll*cnt*M;
    39 }
    40 int main()
    41 {
    42     scanf("%d%d%d%d",&n,&A,&R,&M);
    43     M=min(M,A+R);
    44     for(int i=1;i<=n;i++)
    45         scanf("%d",s+i);
    46     int l=0,r=1e9;
    47     while(l<r)
    48     {
    49         int lmid=l+(r-l)/3,rmid=r-(r-l)/3;
    50         if(check(lmid)<check(rmid)) r=rmid-1;
    51         else    l=lmid+1;
    52     }
    53     //cout<<l<<" "<<r<<endl;
    54     printf("%lld
    ",check(l));
    55 
    56     return 0;
    57 }
    View Code
    1 int l=0,r=1e9;
    2     while(l<r)
    3     {
    4         int lmid=l+(r-l)/3,rmid=r-(r-l)/3;
    5         if(check(lmid)<check(rmid)) r=rmid-1;
    6         else    l=lmid+1;
    7     }
     
  • 相关阅读:
    一本通1268 完全背包问题
    一本通1267 01背包
    合并石子1,2
    求最长不下降子序列++
    数字金字塔升级版
    一本通1354 括弧匹配检验
    一本通1353表达式括号匹配
    一本通1357车厢调度
    Centos查看端口占用情况和开启端口命令
    centos后台运行python程序
  • 原文地址:https://www.cnblogs.com/Kaike/p/12918815.html
Copyright © 2011-2022 走看看