Plants vs. Zombies(二分好题+思维)

Plants vs. Zombies

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5819

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


Plants vs. Zombies(?)
(Image from pixiv. ID: 21790160; Artist: socha)

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.

用ans输出好像会炸long long ???

 1 #include<iostream>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<string>
 6 #include<cstdio>
 7 #include<queue>
 8 #include<vector>
 9 typedef long long ll;
10 #define maxn 100005
11 using namespace std;
12
13 ll a[maxn];
14 ll book[maxn];
15 ll n,m,ans,Min;
16 bool Check(ll mid){
17     ll tmp;
18     if(mid==0) {
19         Min=0;
20         return true;
21     }
22     Min=0x3f3f3f3f3f3f3f3f;
23     for(int i=1;i<=n+1;i++){
24         book[i]=0;
25     }
26     for(int i=1;i<=n;i++){
27         tmp=mid/a[i];
28         if(a[i]*tmp<mid) tmp++;
29         if(i==n&&book[i]>=tmp) break;
30         book[i]++;
31         if(book[i]<tmp){
32             book[i+1]+=tmp-book[i];
33             book[i]=tmp;
34         }
35         Min=min(Min,a[i]*book[i]);
36     }
37     ll sum=0;
38     for(int i=1;i<=n+1;i++){
39         sum+=book[i];
40         if(sum>m) return false;
41     }
42     return true;
43 }
44
45 int main(){
46     int T;
47     while(~scanf("%d",&T)){
48         while(T--){
49             scanf("%lld %lld",&n,&m);
50             for(int i=1;i<=n;i++){
51                 scanf("%lld",&a[i]);
52             }
53             ll L,R,mid;
54             L=0,R=(1LL<<60);
55             while(L<=R){
56                 mid=(L+R)/2;
57                 if(Check(mid)){
58                     L=mid+1;
59                   //  ans=Min;
60                 }
61                 else{
62                     R=mid-1;
63                 }
64             }
65             printf("%lld\n",L-1);
66         }
67     }
68 }

原文地址:https://www.cnblogs.com/Fighting-sh/p/9939747.html

时间: 2024-10-02 17:00:28

Plants vs. Zombies(二分好题+思维)的相关文章

zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)

题目传送门 题目大意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai,现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走m步,问最矮的植物最高是多少. 思路: 一般此类最小值最大问题都是二分,此题显然也是可以二分植物的高度的. 确定某一个高度后,也确定了每个植物需要浇几次水,而对于一株植物来说,应当尽可能的在这株植物和后面那个格子来回走,是这株植物迅速超过最低高度(这样的走法是最优的,因为可以想象,如果往后走很多步再

ZOJ 4062 - Plants vs. Zombies - [二分+贪心][2018 ACM-ICPC Asia Qingdao Regional Problem E]

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062 题意: 现在在一条 $x$ 轴上玩植物大战僵尸,有 $n$ 个植物,编号为 $1 \sim n$,第 $i$ 个植物的位置在坐标 $i$,成长值为 $a_i$,初始防御值为 $d_i$. 现在有一辆小车从坐标 $0$ 出发,每次浇水操作必须是先走 $1$ 单位长度,然后再进行浇水,植物被浇一次水,防御值 $d_i+=a_i$. 现在知道,小车最多进行 $m

ZOJ 4062 Plants vs. Zombies(二分答案)

题目链接:Plants vs. Zombies 题意:从1到n每个位置一棵植物,植物每浇水一次,增加ai高度.人的初始位置为0,人每次能往左或往右走一步,走到哪个位置就浇水一次.求m步走完后最低高度的植物最大高度为多少. 题解:明显二分答案的题目.check时从左往右遍历,贪心思路:把该位置满足同时给后面减少浇水次数,策略是该位置和后一个位置左右横跳,注意最后一个位置的时候,如果满足就不需要再跳过去. 1 #include <cstdio> 2 #include <cstring>

2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)

Plants vs. Zombies Time Limit: 2 Seconds      Memory Limit: 65536 KB BaoBao and DreamGrid are playing the game Plants vs. Zombies. In the game, DreamGrid grows plants to defend his garden against BaoBao's zombies. Plants vs. Zombies(?) (Image from pi

uva 12452 Plants vs. Zombies HD SP (树DP)

Problem I: Plants vs. Zombies HD Super Pro Plants versus Zombies HD Super Pro is a game played not a grid, but on a connected graph G with no cycles (i.e., a tree). Zombies live on edges of the tree and chew through edges so that tree falls apart! Pl

Honk&#39;s pool(二分模板题)

题意:有n个水池,每个水池有a[i]单位水,有k次操作,每次操作将水量最多的水池减少一单位水,水量最少的水池增加一单位水,问最后水量最大的水池和水量最少的水池相差的水量. 思路:二分最后的最大水量和最小水量,特别的,模拟一下可以发现如果总水量sum%n==0,则最大值的下界和最小值的上界均为sum/n,若sum%n!=0,则最大值的下界为sum/n+1,最小值上界为sum/n.二分时注意选取区间是左闭右开还是左开右闭. #include <iostream> #include <algo

【ZOJ4062】Plants vs. Zombies(二分)

题意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai, 现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走m步,问最矮的植物最高是多少. n<=1e6,sigma n<=1e7,0<=m<=1e12,1<=a[i]<=1e5 思路:7Y--你不死谁死 显然最小值最大可以二分,二分答案,最优的策略一定是从左到右依次左右横跳,直到当前格子不小于二分的值 一个致命的细节:最后一个如果在倒数第二个左

51nod 1105 二分好题

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1105 1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * B[0],A[0] * B[1] ......A[1] * B[0],A[1] * B[1]......A[n - 1] * B[n - 1](数组A同数组B的组合).求

【ECNU3542】神奇的魔术(二分交互题)

点此看题面 大致题意: 有一个\(1\sim 2^n\)的排列,\(n\le7\),每次交互告诉你有几个位置上的数是正确的,让你在\(1000\)轮以内猜出每个位置上的数. 二分 显然,我们可以通过二分来求解此题. 具体地,我们先把所有位置填满\(1\),然后暴力枚,找到一个位置填上\(2\)使得此时没有一个位置上的数是正确的. 然后接下来,我们枚举\(3\sim2^n\)的每一个数,每次把\(l\sim mid\)这段区间内除不是\(1\)或\(2\)的位置外全填上当前数,然后询问当前对的数的