2018年 ACM/ICPC亚洲区域赛 青岛赛区现场赛 E题(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.

 
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.

题意:

你的房子在0点,1,2,3,...,n(n<=1e5)点每个点都有一颗高度为0的花,浇一次水花会长a[i]。

你有一个机器人刚开始在你家,最多走m步,每一步只能往前走或者往后走,每走到一个地方除了房子都会给花浇水,问m步以后最低那朵花的高度最大是多少。

思路:(贪心+二分)

从0-m*(max(a[i])i=1,2,3,.....n)二分答案。

从第一棵花开始判断,如果当前的成长速度<mid,求出需要浇几次(记为tmp)才能大于等于mid,其实需要2*tmp次(在第一棵和第二棵之间来回)同理如果第i棵花的成长速度小于mid就在i和i+1这两棵花之间来回浇花。

代码:
 

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100009],c[100009];
ll n,m;
ll judge(ll mid)
{
    for(ll i=1; i<=n; i++)
    {
        c[i]=0;
    }
    ll i=1;
    ll cnt=m-1;
    c[1]=a[1];
    for(; i<=n; i++)
    {
        if(i<n)
        {
            if(cnt==0)break;
            ll tmp=(mid-c[i]+a[i]-1)/a[i];
            if(tmp<=0)
            {
                cnt--;
                c[i+1]+=a[i+1];
                continue;
            }
            if(cnt<2*tmp)
            {
                break;
            }
            cnt-=2*tmp;
            c[i]+=a[i]*tmp;
            c[i+1]+=tmp*a[i+1];
            if(cnt==0)
            {
                break;
            }
            cnt--;
            c[i+1]+=a[i+1];
            if(cnt==0)
            {
                break;
            }
        }
        else
        {
           if(cnt==0)
           {
               break;
           }
           if(c[i]>=mid)
           {
               break;
           }
           ll tmp=(mid-c[i]+a[i]-1)/a[i];

           if(cnt<2*tmp)
           {
              return 0;
           }
           c[i]+=a[i]*tmp;
        }
    }

    for(;i<=n;i++){if(c[i]<mid) return 0;}
    return 1;
}
int main()
{
    ll t;
    scanf("%lld",&t);
    while(t--)
    {

        //cin>>n>>m;
        scanf("%lld%lld",&n,&m);
        ll ans=0;
        for(ll i=1; i<=n; i++)
        {
            scanf("%lld",&a[i]);
            if(m*a[i]>ans)
            {
                ans=m*a[i];
            }
        }

        if(ans==0)
        {
           printf("0\n");
            continue;
        }
        ll l=0;
        ll r=ans;
        while(l<r)
        {
            ll mid=(l+r)/2;
            if(judge(mid))
            {
                l=mid+1;
                ans=mid;
            }
            else
            {
                r=mid;

            }
        }
      printf("%lld\n",ans);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值