B1&B2. Koa and the Beach

22 篇文章 0 订阅

outputstandard output The only difference between easy and hard
versions is on constraints. In this version constraints are lower. You
can make hacks only if all versions of the problem are solved.

Koa the Koala is at the beach!

The beach consists (from left to right) of a shore, n+1 meters of sea
and an island at n+1 meters from the shore.

She measured the depth of the sea at 1,2,…,n meters from the shore and
saved them in array d. di denotes the depth of the sea at i meters
from the shore for 1≤i≤n.

Like any beach this one has tide, the intensity of the tide is
measured by parameter k and affects all depths from the beginning at
time t=0 in the following way:

For a total of k seconds, each second, tide increases all depths by 1.
Then, for a total of k seconds, each second, tide decreases all depths
by 1. This process repeats again and again (ie. depths increase for k
seconds then decrease for k seconds and so on …). Formally, let’s
define 0-indexed array p=[0,1,2,…,k−2,k−1,k,k−1,k−2,…,2,1] of length
2k. At time t (0≤t) depth at i meters from the shore equals
di+p[tmod2k] (tmod2k denotes the remainder of the division of t by
2k). Note that the changes occur instantaneously after each second,
see the notes for better understanding.

At time t=0 Koa is standing at the shore and wants to get to the
island. Suppose that at some time t (0≤t) she is at x (0≤x≤n) meters
from the shore:

In one second Koa can swim 1 meter further from the shore (x changes
to x+1) or not swim at all (x stays the same), in both cases t changes
to t+1. As Koa is a bad swimmer, the depth of the sea at the point
where she is can’t exceed l at integer points of time (or she will
drown). More formally, if Koa is at x (1≤x≤n) meters from the shore at
the moment t (for some integer t≥0), the depth of the sea at this
point — dx+p[tmod2k] — can’t exceed l. In other words,
dx+p[tmod2k]≤l must hold always. Once Koa reaches the island at n+1
meters from the shore, she stops and can rest. Note that while Koa
swims tide doesn’t have effect on her (ie. she can’t drown while
swimming). Note that Koa can choose to stay on the shore for as long
as she needs and neither the shore or the island are affected by the
tide (they are solid ground and she won’t drown there).

Koa wants to know whether she can go from the shore to the island.
Help her!

题目大意:koa想要跨越长度为n的海到达n+1的海岸,已知(1~n)米处,河的深度, 海会有涨潮和退潮现象,涨潮会每秒会使每个位置水的深度增加1单位。退潮则相反。涨潮和退潮的持续时间都为k。如果深度超过l,KOA会被淹死,koa在一秒内可以选择向前游一米,或原地等待,问KOA是否能到对岸。
思路:当最深深度>l时koa在任何时刻不可能跨过此处。则为不可能。若最深深度+k<=l则一定能通过。个人觉得koa出发最合适的时间为退潮到涨潮的时间段,将它对应的时间定为0~2*k,当某一处的深度加上涨潮时间仍小于等于l时,我们让他原地等待到最合适的时机再前进。否则选择时间区间的左端点与前时间+1的较大者。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<set>
#include<cmath>
#include<numeric>
#include<map>
#include<deque>
#include<algorithm>
#include<functional>
using namespace std;
const int maxn=4e5+5;
int dd[maxn];
int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        int n,k,l;
        cin>>n>>k>>l;
        int maxx=0;
        for(int i=1;i<=n;i++){
            cin>>dd[i];
            maxx=max(dd[i],maxx);
        }
        if(maxx>l){
            cout<<"NO"<<endl;
            continue;
        }
        else if(maxx+k<=l){
            cout<<"YES"<<endl;
            continue;
        }
        int ti=-1;int wait=0;
        for(int i=1;i<=n;i++)
        {
            int li=max(dd[i]+k-l,0),ri=min(k+(l-dd[i]),2*k);
			//可以通过的时间区间[li,ri]
            if(dd[i]+k<=l){
                wait=1;
                continue;
            }
            if(wait==1){
                ti=li;
                wait=0;
                continue;
            }
            if(ti+1<=ri)ti=max(ti+1,li);
            else {ti=-14;break;}
        }
        if(ti<0){
            cout<<"NO"<<endl;
        }
        else {
            cout<<"YES"<<endl;
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值