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

本文探讨了在游戏《植物大战僵尸》中,通过优化浇水机器人路径以最大化植物防御值的策略。考虑到植物的成长速度和浇水限制,文章提出了一种算法,通过调整浇水机器人的移动方向,以达到植物防御值的最大化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 

t个测试样例,输入n个植物,洒水车总共可以走m步,每走到一个植物上植物获得相应守御值,求植物最小守御值最大的值
当m步无法走完n个植物时,最小守御值为0,
当m步能走完n个植物时,从0到理想最大守御值(植物最大成长乘m)进行二分列举查找最小守御值,检验是否正确时先默认洒水车在n个植物上都走了一遍,存下要达到最小守御值应该再经过几次植物的值。计算步数的时候因为一个植物要经过两次要通过去前面那个植物再回来,每次step加上当前位置需要经过的次数乘二,再把前一个的需要次数减去当前位置需要经过的次数
但要考虑的是, 当走到最后一个植物返回来时达成了最小守御值,这种情况在之前的算法中未被包含,所以单独考虑下

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int Maxx = 100500;

long long plant[Maxx];
long long defent[Maxx];
long long n, m;
bool zd;

bool check(long long pp)
{
    for(int i = 0; i < n; i++)defent[i] = (pp - 1)/ plant[i];
    long long step = n;
    for(int i = 0; i < n; i++)
    {
        if(defent[i] > 0)step += defent[i] * 2, defent[i+1] -= defent[i];
//        cout<< step <<endl;
        if(step > m) return false;
    }

    return true;
}

bool checkk(long long pp)
{
    for(int i = 0; i < n; i++)defent[i] = (pp - 1)/ plant[i];
    long long step = n + 1;
    defent[n - 2]--;
    for(int i = 0; i < n; i++)
    {
        if(defent[i] > 0)step += defent[i] * 2, defent[i+1] -= defent[i];
        if(step > m) return false;
    }

    return true;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--){

    scanf("%lld%lld", &n, &m);

    long long da = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &plant[i]);
        da = max(plant[i], da);
    }

    if(m < n){//当m步无法走完n个植物时,最小守御值为0,
        cout<<0<<'\n';
        continue;
    }

    long long l = 0, r = da * m;
    while(l < r)
    {
        long long mid = (l + r + 1) / 2;
//        cout<< l << ' ' <<mid<<' '<< r << '\n';
//        cout<<l<<' '<<mid<<' '<<r<<' '<< hua << '\n';
        if(check(mid) || checkk(mid)) l = mid;
        else r = mid - 1;
//        cout<<l <<' '<< r<<'\n';
    }
    cout<< l <<endl;
    }

    return 0;
}

2019.04.05附记:今天做青岛区域赛模拟,,,居然没做出来,,一下下二分tle了,换成步数的卡壳了,,,,不是做过就能a啊,,之前(就是上面那个)做得太暴力,,两个check居然过了,,太暴力有来做居然没搞出来,,这里附上这一次a的,思路一样,合并了两个check,,

题目不是a了就完了啊,,,

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int Ma = 1e5 + 100;
const int inf = 0x3f3f3f3f;
ll plant[Ma];
ll tree[Ma];
ll n, m;

ll solve(ll mb){
    for(ll i = 0; i < n; i++)
        tree[i] = (mb - 1) / plant[i];
    tree[n] = 0;
    ll tot = n;
    for(ll i = 0; i < n; i++){
        if(i == n - 2 && tree[i] > tree[i + 1]){
            tot += tree[i] * 2 - 1;
            if(tot > m)return false;
            break;
        }
        tot += tree[i] * 2;
        if(tot > m)return false;
        if(tree[i + 1] > tree[i])tree[i + 1] -= tree[i];
        else tree[i + 1] = 0;
    }

    return true;
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        ll mi = inf;
        scanf("%lld%lld", &n, &m);
        for(int i = 0; i < n; i++){
            scanf("%lld", &plant[i]);
            mi = min(plant[i], mi);
        }
        if(n > m){
            puts("0");
            continue;
        }
        ll l = 0;
        ll r = mi * (m - n + 1);
        ll ans = 0;
        while(l <= r){
            ll mid = (l + r + 1) >> 1;
            if(solve(mid))ans = mid, l = mid + 1;
            else r = mid - 1;
        }
        printf("%lld\n", ans);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值