谁在看守看门猪?AI (CI翻译)
There are $n$ piggies standing in a line, numbered from $1$ to $n$ from left to right. Each piggy is either facing left or right.
有 $n$ 只小猪排成一列,从左到右编号为 $1$ 到 $n$。每只小猪要么面向左方,要么面向右方。AI (CI翻译)
Two piggies $x$ and $y$ are called a watchpig pair if $x \lt y$, $x$ is facing right, and $y$ is facing left.
如果 $x \lt y$,小猪 $x$ 面向右方,且小猪 $y$ 面向左方,则称小猪 $x$ 和 $y$ 构成一个看门猪对。AI (CI翻译)
You are given an integer $k$ such that $1 \le k \lt n$. A piggy is called safe if it belongs to at least $k$ watchpig pairs.
给定一个整数 $k$,满足 $1 \le k \lt n$。如果一只小猪至少属于 $k$ 个看门猪对,则称其为安全的。AI (CI翻译)
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).
你的任务是让所有小猪都变得安全。为此,你可以选择任意数量的小猪,将它们的朝向翻转(即将向左改为向右,或反之)。AI (CI翻译)
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.
请计算使每只小猪都安全所需翻转的小猪数量的最小值;如果无法做到,则报告这一点。AI (CI翻译)
Input
输入
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
每个测试包含多组测试用例。第一行包含测试用例数量 $t$($1 \le t \le 500$)。接下来是各测试用例的描述。AI (CI翻译)
The first line of each test case contains two integers $n$ and $k$ ($1 \le k \lt n \le 100$), representing the number of piggies and the number of watchpig pairs a piggy needs to belong to, respectively.
每个测试用例的第一行包含两个整数 $n$ 和 $k$($1 \le k \lt n \le 100$),分别表示小猪的数量,以及每只小猪需要属于的看门猪对数量。AI (CI翻译)
The second line contains a string $s$ of length $n$ consisting only of the characters $\mathtt{L}$ and $\mathtt{R}$. For every $1 \le i \le n$, if $s_i= \mathtt{L}$, piggy $i$ is facing left; otherwise, if $s_i= \mathtt{R}$, piggy $i$ is facing right.
第二行包含一个长度为 $n$ 的字符串 $s$,其中只包含字符 $\mathtt{L}$ 和 $\mathtt{R}$。对于每个 $1 \le i \le n$,如果 $s_i= \mathtt{L}$,则小猪 $i$ 面向左方;否则,如果 $s_i= \mathtt{R}$,则小猪 $i$ 面向右方。AI (CI翻译)
Output
输出
AI (CI翻译)For each test case, if it is impossible to make every piggy safe, output $-1$. Otherwise, output the minimum number of piggies you need to turn around so that every piggy becomes safe.
对于每个测试用例,如果无法使所有小猪都安全,则输出 $-1$。否则,输出使每只小猪都安全所需翻转的小猪数量的最小值。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, one optimal solution is to turn piggy $1$ around. Both piggy $1$ and piggy $3$ now belong to the watchpig pair $(1,3)$, and piggy $2$ belongs to the watchpig pair $(1,2)$. Every piggy belongs to at least $k=1$ watchpig pairs, hence every piggy is safe.
在第一个测试用例中,一种最优方案是翻转小猪 $1$ 的朝向。此时,小猪 $1$ 和小猪 $3$ 都属于看门猪对 $(1,3)$,而小猪 $2$ 属于看门猪对 $(1,2)$。每只小猪都至少属于 $k=1$ 个看门猪对,因此所有小猪都安全。AI (CI翻译)
In the second test case, it can be proven that it is impossible to make every piggy safe.
在第二个测试用例中,可以证明无法使所有小猪都安全。AI (CI翻译)
In the third test case, one optimal solution is to turn piggies $2$ and $5$ around. For example, piggy $2$ is now safe, as it belongs to both $(2,3)$ and $(2,5)$. Piggy $4$ is also safe, as it belongs to both $(4,5)$ and $(4,6)$. The rest of the piggies are all safe.
在第三个测试用例中,一种最优方案是翻转小猪 $2$ 和小猪 $5$ 的朝向。例如,小猪 $2$ 现在是安全的,因为它属于 $(2,3)$ 和 $(2,5)$ 这两个看门猪对。小猪 $4$ 也安全,因为它属于 $(4,5)$ 和 $(4,6)$ 这两个看门猪对。其余小猪也都安全。AI (CI翻译)
样例
4
3 1
LLL
4 3
LRLR
6 2
RLLRRL
12 4
LRLLRRLRLRLR
1
-1
2
5
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。