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

题目传送门

题目大意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai,现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走m步,问最矮的植物最高是多少。

思路:

  一般此类最小值最大问题都是二分,此题显然也是可以二分植物的高度的。

  确定某一个高度后,也确定了每个植物需要浇几次水,而对于一株植物来说,应当尽可能的在这株植物和后面那个格子来回走,是这株植物迅速超过最低高度(这样的走法是最优的,因为可以想象,如果往后走很多步再走回来,中间浪费的可能性比较大),于是就是对n个植物模拟浇水,考虑一些细节就可以了(二分跳出条件,long long等等)

//#pragma comment(linker,"/STACK:102400000,102400000")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<stdlib.h>
//#include<unordered_map>
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)|1
#define CLR(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
typedef long long ll;
using namespace std;
inline ll read(){
    ll x=0,f=1;
    char ch=getchar();while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;}
const int maxn=100010;
int n;
ll a[maxn],c[maxn],m;
inline bool judge(ll high){
    CLR(c,0);
    ll temp=m;
    if(m==0)return false;
    c[1]=a[1],m--;
    int i=1;
    for(;i<=n;i++)
    {
        if(m<=0)break;
        if(c[i]>=high)
        {
            if(m>0)
            {
            c[i+1]=a[i+1];
            m--;
            continue;
            }else{
                break;
            }
        }
        ll tmp=(ll)ceil((high-c[i])*1.0/a[i]);
        if(m>2*tmp){
            m-=2*tmp+1;
            c[i]+=a[i]*tmp;
            c[i+1]+=a[i+1]*(tmp+1);
            continue;
        }else if(m==2*tmp){
            c[i]+=a[i]*tmp;
            c[i+1]+=a[i+1]*tmp;
            break;
        }else{
            break;
        }
    }
    m=temp;
    for(i=1;i<=n;i++)
    {
        if(c[i]<high)return false;
    }
    return true;
}
int main(){
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        ll l=0,r=0,mid,ans;
        for(int i=1;i<=n;i++){
            a[i]=read();
            r=max(r,a[i]*m);
        }
        if(m==0){
            printf("0\n");
            continue;
        }

        while(l<=r)
        {
            mid=(l+r)>>1;
        //    printf("mid  %d\n",mid);
            if(judge(mid))
            {
                ans=mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        printf("%lld\n",ans);
    }
} 

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 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.

原文地址:https://www.cnblogs.com/mountaink/p/9921988.html

时间: 2024-10-03 16:42:47

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

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

uvalive 4254 Processor处理器 (二分模拟+贪心)

 有n个任务,每个任务有ri,di,wi;代表任务的[ri,di]代表可以做这个任务的时间区间,而wi代表这个任务的工作量;现在有有个处理器,如果它的执行速度是s,则完成第i个任务所需时间wi/s;要求算出处理器执行过程中最大速度的最小值 思路很简单二分,但如何模拟是难点,可以模拟处理器每一秒的工作,对于每一秒来说,用优先队列储存当前时间下可以处理的任务,优先处理d小的,如果处理完了,那么处理下一个任务,如果没处理完,时间加一. #include<cstdio> #include<c

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

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

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(?)(

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

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

HDU 4903 (模拟+贪心)

Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a heat for years in China. The game goes with the 54 poker cards for 3 players, where the “Landlord” has 20 cards and the other two (the “Farmers”) have 1

hdu 4004 (二分加贪心) 青蛙过河

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂直于河岸的一条直线上) 还有青蛙能够跳跃的 最多 的次数,还有每个石头离河岸的距离,问的是青蛙一步最少要跳多少米可以过河> 这是一道二分加贪心的题,从0到的河宽度开始二分,二分出一个数然后判断在这样的最小步数(一步跳多少距离)下能否过河 判断的时候要贪心 主要难在思维上,关键是要想到二分上去,能想到

UVA-11054-Wine trading in Gergovia(模拟+贪心)

首先这道题的节点数太多了,达到10^5,所以不能用数组模拟啊,肯定TLE,所以用贪心算法,读取第一个结点,搬到第二个结点,剩下的和第二个结点合并,一起搬到第三个结点......这个算法很好,每次看成只是邻居间买卖,下面是代码: #include<stdio.h> #include<iostream> #include<stdlib.h> using namespace std; int main() { int n; while(cin>>n &&a