Codeforces Round 957 (Div. 3) D. Test of Love

题目描述

ErnKor is ready to do anything for Julen, even to swim through crocodile-infested swamps. We decided to test this love. ErnKor will have to swim across a river with a width of 1 meter and a length of n meters.

The river is very cold. Therefore, in total (that is, throughout the entire swim from 0 to n+1 ) ErnKor can swim in the water for no more than k meters. For the sake of humanity, we have added not only crocodiles to the river, but also logs on which he can jump. Our test is as follows:

Initially, ErnKor is on the left bank and needs to reach the right bank. They are located at the 0 and n+1 meters respectively. The river can be represented as nn segments, each with a length of 1 meter. Each segment contains either a log 'L', a crocodile 'C', or just water 'W'. ErnKor can move as follows:

  • If he is on the surface (i.e., on the bank or on a log), he can jump forward for no more than mm ( 1≤m≤10 ) meters (he can jump on the bank, on a log, or in the water).
  • If he is in the water, he can only swim to the next river segment (or to the bank if he is at the nn -th meter).
  • ErnKor cannot land in a segment with a crocodile in any way.

Determine if ErnKor can reach the right bank.

输入格式

The first line contains a single integer tt ( 1≤t≤104 ) — the number of test cases.

The first line of each test case contains three numbers n,m,kn,m,k ( 0≤k≤2⋅105 , 1≤n≤2⋅105 , 1≤m≤10 ) — the length of the river, the distance ErnKor can jump, and the number of meters ErnKor can swim without freezing.

The second line of each test case contains a string aa of length nn . aiai​ denotes the object located at the i -th meter. ai​∈{ 'W','C','L' }

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅105 .

输出格式

For each test case, output "YES" if ErnKor can pass the test, and output "NO" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

题意翻译

有一个长 nn 米的河流,你站在 0 号点上,想要去 n+1 号点。

1 号点到 n 号点有 3 种情况。

  1. W 表示水,允许经过。

  2. L 表示木头,允许经过。

  3. C 表示鳄鱼,不允许经过。

这个人可以经过不超过 k 个水点。

运动的方式有 2 种。

  1. 站在木点 i 上,你可以跳到 min⁡(n+1,i+j(0≤j≤m))min(n+1,i+j(0≤j≤m)) 上,可以跳入水中或者另一个木头,但是不能跳到鳄鱼上。

  2. 在水 i 上,你能且仅能到达 i+1 号点。如果 i+1 是鳄鱼,不可以走。

不能走回头路。

如果这两种运动方式都无法到达 n+1 号点,或是经过了超过 k 个水点,则失败,反之成功。

判断能不能成功。

输入输出样例

输入 #1复制

6
6 2 0
LWLLLW
6 1 1
LWLLLL
6 1 1
LWLLWL
6 2 15
LWLLCC
6 10 0
CCCCCC
6 6 1
WCCCCW

输出 #1复制

YES
YES
NO
NO
YES
YES

说明/提示

Let's consider examples:

  • First example: We jump from the shore to the first log ( 0→1 ), from the first log to the second ( 1→3), from the second to the fourth ( 3→5 ), and from the last log to the shore ( 5→7 ). So, we have 0→1→3→5→70→1→3→5→7 . Since we did not encounter a crocodile and swam no more than k meters, the answer is «YES».
  • Second example: 0→1 , we jump into the water from the first log ( 1→2 ), swim a cell to the log ( 2⇝3 ), 3→4→5→6→7 . Since we did not encounter a crocodile and swam no more than k meters, the answer is «YES».
  • In the third example, ErnKor needs to swim two cells 'W', but can only swim one. Therefore, the answer is «NO».
  • Sixth example: We jump from the shore into the water ( 0→6 ) and swim one cell in the water ( 6⇝7 ). Since we did not encounter a crocodile and swam no more than k meters, the answer is «YES».

原题链接:

Problem - D - Codeforces

思路:
 

这道题,我们初步看有点像模拟,模拟跳跃的整个过程,但会发现整个过程下来是比较麻烦的,我们考虑动态规划,dp[i] 是到达位置 i所需的 最小游泳距离,最后看看dp[n+1] 和 k 之间的大小关系即可。

代码实现:
 

#include<bits/stdc++.h>

using namespace std;

const int INF=0x3f3f3f3f;

void solve()
{
    int n,m,k;
    cin>>n>>m>>k;

    string S;
    cin>>S;

    S='L'+S+'L';

    vector<int> dp(n+2,INF);//dp[i]是到达位置i所需的最小游泳距离,初始化为无穷大

    dp[0]=0;

    for(int i=0;i<n+1;i++)
    {
        for(int j=i+1;j<=n+1 && j<=i+(S[i]=='L'?m:1);j++)
        {
            if(S[j]=='L') dp[j]=min(dp[j],dp[i]);
            else if(S[j]=='W') dp[j]=min(dp[j],dp[i]+1);
        }
    }

    if(dp[n+1]<=k) puts("YES");
    else puts("NO");
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin>>t;

    while(t--)
    {
        solve();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值