CF2245A.Who Watches the Watchpig?

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn piggies standing in a line, numbered from 11 to nn from left to right. Each piggy is either facing left or right.

Two piggies xx and yy are called a watchpig pair if x<yx \lt y, xx is facing right, and yy is facing left.

You are given an integer kk such that 1≤k<n1 \le k \lt n. A piggy is called safe if it belongs to at least kk watchpig pairs.

Your task is to make all piggies safe. To achieve this, you can choose any number of piggies and flip their directions (changing left to right or vice versa).

Compute the minimum number of piggies you need to turn around so that every piggy becomes safe, or report that it is impossible to do so.

有 nn 只小猪排成一列,从左到右依次编号为 11 到 nn。每只小猪面朝左或右。

若满足 x<yx \lt y、xx 面朝右、且 yy 面朝左,则称小猪 xx 和 yy 构成一对“守望对”(watchpig pair)。

给定整数 kk,满足 1≤k<n1 \le k \lt n。若一只小猪至少属于 kk 个守望对,则称其为“安全的”。

你的任务是使所有小猪都变为安全的。为此,你可以任选若干只小猪并翻转其朝向(即左变右、右变左)。

请计算使所有小猪均安全所需的最少翻转次数;若不可能实现,请报告该情况。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains two integers nn and kk (1≤k<n≤1001 \le k \lt n \le 100), representing the number of piggies and the number of watchpig pairs a piggy needs to belong to, respectively.

The second line contains a string ss of length nn consisting only of the characters L\mathtt{L} and R\mathtt{R}. For every 1≤i≤n1 \le i \le n, if si=Ls_i= \mathtt{L}, piggy ii is facing left; otherwise, if si=Rs_i= \mathtt{R}, piggy ii is facing right.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤k<n≤1001 \le k \lt n \le 100),分别表示小猪的数量以及一只小猪需要所属的“守望对”(watchpig pair)的数量。

每个测试用例的第二行包含一个长度为 nn 的字符串 ss,其仅由字符 L\mathtt{L} 和 R\mathtt{R} 组成。对于每个 1≤i≤n1 \le i \le n,若 si=Ls_i = \mathtt{L},则第 ii 只小猪面朝左;否则(即 si=Rs_i = \mathtt{R}),则第 ii 只小猪面朝右。

输出格式

For each test case, if it is impossible to make every piggy safe, output −1-1. Otherwise, output the minimum number of piggies you need to turn around so that every piggy becomes safe.

对于每个测试用例,如果无法使所有存钱罐变得安全,则输出 −1-1;否则,输出需要翻转的存钱罐的最小数量,使得所有存钱罐均变得安全。

输入输出样例

  • 输入#1

    4
    3 1
    LLL
    4 3
    LRLR
    6 2
    RLLRRL
    12 4
    LRLLRRLRLRLR

    输出#1

    1
    -1
    2
    5

说明/提示

In the first test case, one optimal solution is to turn piggy 11 around. Both piggy 11 and piggy 33 now belong to the watchpig pair (1,3)(1,3), and piggy 22 belongs to the watchpig pair (1,2)(1,2). Every piggy belongs to at least k=1k=1 watchpig pairs, hence every piggy is safe.

In the second test case, it can be proven that it is impossible to make every piggy safe.

In the third test case, one optimal solution is to turn piggies 22 and 55 around. For example, piggy 22 is now safe, as it belongs to both (2,3)(2,3) and (2,5)(2,5). Piggy 44 is also safe, as it belongs to both (4,5)(4,5) and (4,6)(4,6). The rest of the piggies are all safe.

在第一个测试用例中,一种最优解是将小猪 11 翻转。此时,小猪 11 和小猪 33 均属于守卫对 (1,3)(1,3),而小猪 22 属于守卫对 (1,2)(1,2)。每只小猪至少属于 k=1k=1 个守卫对,因此每只小猪都是安全的。

在第二个测试用例中,可以证明:不可能使所有小猪都变得安全。

在第三个测试用例中,一种最优解是将小猪 22 和小猪 55 翻转。例如,小猪 22 现在是安全的,因为它同时属于 (2,3)(2,3) 和 (2,5)(2,5);小猪 44 也是安全的,因为它同时属于 (4,5)(4,5) 和 (4,6)(4,6);其余的小猪也全部安全。

输入解题思路,AI测评打分。不知道怎么写?

首页