AtCoder AtCoder Beginner Contest 063 D - Widespread(二分)

题目链接:http://abc063.contest.atcoder.jp/tasks/arc075_b

D - Widespread


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

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.
  • 1N105
  • 1B<A109
  • 1hi109

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

Copy
4 5 3
8
7
4
2

Sample Output 1

Copy
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

Copy
2 10 4
20
20

Sample Output 2

Copy
4

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


Sample Input 3

Copy
5 2 1
900000000
900000000
1000000000
1000000000
1000000000

Sample Output 3

Copy
800000000

Submit


解析:二分选择A操作的次数

代码:

#include
#define N 100009
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f;
 
LL h[N];
LL n, a, b;
 
bool judge(LL mid)
{
    LL ans = mid;
    for(int i = 1; i <= n; i++)
    {
        LL tm = h[i] - b*mid;
        if(tm > 0)
        {
            LL cnt = tm / (a - b) + (tm % (a - b) == 0 ? 0 : 1);
            ans -= cnt;
            if(ans < 0) return false;
        }
    }
    return true;
}
 
int main()
{
    scanf("%lld%lld%lld", &n, &a, &b);
    for(int i = 1; i <= n; i++) scanf("%lld", &h[i]);
    LL l = 0, r = INF;
    LL ans = INF;
    while(l <= r)
    {
        LL mid = (l + r)>>1;
        if(judge(mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    printf("%lld\n", ans);
    return 0;
}



你可能感兴趣的:(AtCoder,二分)