zoukankan      html  css  js  c++  java
  • 二分答案(Widespread )

    二分答案其实是变相贪心,这周算是被这个虐了,怎么都想不到,比如这题,一直纠结在最大值的贪心上后面队友一指点,原来可以先减去x*b,然后a-b随机分配就好了,

    仔细一想没错呀,每次攻击必然受到x*b次伤害而剩下的x个a-b就可以随机分配给每个怪物,注意是成对分而不能求和。说下二分答案吧

    二分前提

    1.答案区间上下限确定,即最终答案在哪个范围是容易知道的。

    2.检验某值是否可行是个简单活,即给你个值,你能很容易的判断是不是符合题目要求。

    3.可行解满足区间单调性,即若x是可行解,则在答案区间内x+1(也可能是x-1)也可行。

    二分转换如下,但是检验是否成立就需要对题目的分析了。

    1.最小值最大化

    int l = min_ans, r = max_ans;
    while (l < r) {
        int mid = (l + r + 1) / 2;   //+1避免 r == l + 1 时mid一直等于l,从而死循环
        if (ok(mid))    //符合条件返回True
            l = mid;
        else
            r = mid - 1;
    }

    2.最大值最小化

    1 int l = min_ans, r = max_ans;
    2 while (l < r) {
    3     int mid = (l + r) / 2;
    4     if (ok(mid))    //符合条件返回True
    5         r = mid;
    6     else
    7         l = mid + 1;
    8 }

    题目:

    You are going out for a walk, when you suddenly encounter N monsters. Each monster has a parameter called health, and the health of the i-th monster is hi at the moment of encounter. A monster will vanish immediately when its health drops to 0 or below.

    Fortunately, you are a skilled magician, capable of causing explosions that damage monsters. In one explosion, you can damage monsters as follows:

    • Select an alive monster, and cause an explosion centered at that monster. The health of the monster at the center of the explosion will decrease by A, and the health of each of the other monsters will decrease by B. Here, A and B are predetermined parameters, and A>B holds.

    At least how many explosions do you need to cause in order to vanish all the monsters?

    Constraints

     

    • All input values are integers.
    • 1≤N≤105
    • 1≤B<A≤109
    • 1≤hi≤109

    Input

     

    Input is given from Standard Input in the following format:

    N A B
    h1
    h2
    :
    hN
    

    Output

     

    Print the minimum number of explosions that needs to be caused in order to vanish all the monsters.

    Sample Input 1

     

    4 5 3
    8
    7
    4
    2
    

    Sample Output 1

     

    2
    

    You can vanish all the monsters in two explosion, as follows:

    • First, cause an explosion centered at the monster with 8 health. The healths of the four monsters become 341 and −1, respectively, and the last monster vanishes.
    • Second, cause an explosion centered at the monster with 4 health remaining. The healths of the three remaining monsters become 0−1 and −2, respectively, and all the monsters are now vanished.

    Sample Input 2

     

    2 10 4
    20
    20
    

    Sample Output 2

     

    4
    

    You need to cause two explosions centered at each monster, for a total of four.

    Sample Input 3

     

    5 2 1
    900000000
    900000000
    1000000000
    1000000000
    1000000000
    

    Sample Output 3

     

    800000000
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<iomanip>
     6 #include<algorithm>
     7 using namespace std;
     8 #define eps 1e-7
     9 #define maxi 100005
    10 long long  h[maxi];
    11 long long hi[maxi];
    12 long long  n,a,b;
    13 long long in=10000000000;
    14 bool C(long long  x){
    15     long long t=x;
    16     for(long long i=0;i<n;i++)
    17         hi[i]=h[i];
    18     for(long long i=0;i<n;i++){
    19         hi[i]-=x*b;
    20         if(hi[i]<=0) continue;
    21         long long y;
    22         if(hi[i]%(a-b)==0)
    23           y=hi[i]/(a-b);
    24         else
    25             y=hi[i]/(a-b)+1;
    26         t-=y;
    27     }
    28     if(t>=0) return true;
    29     return false;
    30 }
    31 void solve(){
    32     sort(h,h+n);
    33 long long  lb=0,ub=in;
    34    while(ub-lb>1){
    35         long long mid=(lb+ub)/2;
    36         //cout<<mid<<" "<<C(mid)<<" "<<ub<<endl;
    37         if(C(mid)) ub=mid;
    38         else  lb=mid;
    39         //cout<<lb<<" "<<ub<<endl;
    40     }
    41     cout<<ub<<endl;
    42 }
    43 int main()
    44 {
    45            scanf("%d%d%d",&n,&a,&b);
    46    for(int i=0;i<n;i++)  scanf("%d",&h[i]);
    47   solve();
    48    return 0;
    49 }
    View Code


  • 相关阅读:
    tensorflow在文本处理中的使用——Doc2Vec情感分析
    tf.squeeze()
    tf.concat()
    tf.slice()
    WebService到底是什么?
    Webservice工作原理及实例
    Iterator,foreach遍历小计
    谈谈今年很火的区块链 CDN
    Java 反射简介(转载)
    Ajax二级联动简单实例
  • 原文地址:https://www.cnblogs.com/blvt/p/7846596.html
Copyright © 2011-2022 走看看