1. 程式人生 > >Plants vs. Zombies(2018ICPC青島站E題) 二分

Plants vs. Zombies(2018ICPC青島站E題) 二分

Plants vs. Zombies

                                                ZOJ - 4062 

BaoBao and DreamGrid are playing the game Plants vs. Zombies. In the game, DreamGrid grows plants to defend his garden against BaoBao's zombies

There are  plants in DreamGrid's garden arranged in a line. From west to east, the plants are numbered from 1 to  and the -th plant lies  meters to the east of DreamGrid's house. The -th plant has a defense value of  and a growth speed of . Initially,  for all .

DreamGrid uses a robot to water the plants. The robot is in his house initially. In one step of watering, DreamGrid will choose a direction (east or west) and the robot moves exactly 1 meter along the direction. After moving, if the -th plant is at the robot's position, the robot will water the plant and  will be added to . Because the water in the robot is limited, at most  steps can be done.

The defense value of the garden is defined as . DreamGrid needs your help to maximize the garden's defense value and win the game.

Please note that:

  • Each time the robot MUST move before watering a plant;
  • It's OK for the robot to move more than  meters to the east away from the house, or move back into the house, or even move to the west of the house.
Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers  and  (), indicating the number of plants and the maximum number of steps the robot can take.

The second line contains  integers  (), where  indicates the growth speed of the -th plant.

It's guaranteed that the sum of  in all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the maximum defense value of the garden DreamGrid can get.

Sample Input
2
4 8
3 2 6 6
3 9
10 10 1
Sample Output
6
4
Hint

In the explanation below, 'E' indicates that the robot moves exactly 1 meter to the east from his current position, and 'W' indicates that the robot moves exactly 1 meter to the west from his current position.

For the first test case, a candidate direction sequence is {E, E, W, E, E, W, E, E}, so that we have  after the watering.

For the second test case, a candidate direction sequence is {E, E, E, E, W, E, W, E, W}, so that we have  after the watering.

題意:輸入第一行為樣例數,每個樣例的第一行為n,m,代表有n個數字和要走m步,第二行為n個數字的價值,每走到一個數字,該數字所在位置就要加上這個位置的價值。問走完m步之後最小值的最大值為多少。

思路:二分

    l=0,r=1e12 二分可以的最小价值的最大值。以第一個樣例為準,a[1]=3 a[2]=2 a[3]=6 a[4]=6 如果最小值為7,那麼位置1就需要走3次,即走到2,走回1,走到2,走回1。所以當1位置被走了3次後,2位置其實已經被走過了2次。以此類推,用step記錄走的步數,先算出在某個二分的值下每個位置最少需要走幾步,然後遍歷一遍,看看真實走的步數是否比m小,如果小,說明當前二分的值偏小,否則偏大。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn=1e5+10;
 8 int casen;
 9 int n;
10 ll m;
11 ll a[maxn],b[maxn];
12 bool check(ll x)
13 {
14     ll step=0;
15     for(int i=0;i<n;i++)//先計算一遍在以x為最小值的情況下,每一個位置應該走多少步 
16     {
17         if(x%a[i])
18         {
19             b[i]=x/a[i]+1;
20         }
21         else
22         {
23             b[i]=x/a[i];
24         }
25     }
26     //(for(int i=0;i<n;i++) b[i]=(x+a[i]-1)/a[i];) 大佬是這麼寫的,真簡潔啊 
27     for(int i=0;i<n-1;i++)
28     {
29         step++;        //,每經過一個位置,加一步 
30         b[i]--;        
31         if(b[i]<=0)
32             continue;
33         step+=2*b[i];//因為每次想讓當前位置再減小的話,必定是向前然後回來,一來一回需要兩倍的步數 
34         b[i+1]-=b[i];//將把因為b[i]減到0而走過的步數減去 
35         if(step>m)
36             return false;
37     }
38     if(b[n-1]>0)
39     {
40         step++;         
41         b[n-1]--;
42         step+=b[n-1]*2;
43     }
44     if(step>m)
45         return false;
46     return true;
47 }
48 int main()
49 {
50     scanf("%d",&casen);
51     while(casen--)
52     {
53         scanf("%d%lld",&n,&m);//注意m的範圍!!!! 
54         for(int i=0;i<n;i++)
55         {
56             scanf("%lld",&a[i]);
57         }
58         ll l=0,r=1e12+10;
59         ll ans=0;            //注意寫=0啊啊啊啊啊!!!!! 
60         while(l<=r)
61         {
62             ll mid=(l+r)/2;
63             if(check(mid))
64             {
65                 ans=mid;
66                 l=mid+1;
67             }
68             else
69             {
70                 r=mid-1;
71             }
72         }
73         printf("%lld\n",ans);
74     }
75 }