CF1779A.Hall of Fame
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Thalia is a Legendary Grandmaster in chess. She has n trophies in a line numbered from 1 to n (from left to right) and a lamp standing next to each of them (the lamps are numbered as the trophies).
A lamp can be directed either to the left or to the right, and it illuminates all trophies in that direction (but not the one it is next to). More formally, Thalia has a string s consisting only of characters 'L' and 'R' which represents the lamps' current directions. The lamp i illuminates:
- trophies 1,2,…,i−1 if si is 'L';
- trophies i+1,i+2,…,n if si is 'R'.
She can perform the following operation at most once:
- Choose an index i (1≤i<n);
- Swap the lamps i and i+1 (without changing their directions). That is, swap si with si+1.
Thalia asked you to illuminate all her trophies (make each trophy illuminated by at least one lamp), or to tell her that it is impossible to do so. If it is possible, you can choose to perform an operation or to do nothing. Notice that lamps cannot change direction, it is only allowed to swap adjacent ones.
塔莉娅是一位国际象棋传奇特级大师。她有 n 个奖杯排成一行,编号从 1 到 n(从左到右),每个奖杯旁都有一盏灯(灯的编号与对应奖杯相同)。
每盏灯只能朝左或朝右照射,且会照亮该方向上的所有奖杯(但不包括它旁边的那一个奖杯)。更准确地说,塔莉娅有一个仅由字符 'L' 和 'R' 组成的字符串 s,表示各盏灯当前的照射方向。第 i 盏灯照射:
- 若 si 为
'L',则照射奖杯 1,2,…,i−1; - 若 si 为
'R',则照射奖杯 i+1,i+2,…,n。
她最多可以执行以下操作一次:
- 选择一个下标 i(满足 1≤i<n);
- 交换第 i 盏与第 i+1 盏灯的位置(不改变它们的照射方向),即交换 si 与 si+1。
塔莉娅请你判断:能否让她的所有奖杯都被至少一盏灯照亮?若能,请给出一种方案(可选择执行一次上述操作,也可选择不执行任何操作);若不能,请告诉她这是不可能的。注意:灯的照射方向不可更改,只允许交换相邻的两盏灯。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10000). The description of the test cases follows.
The first line of each test case contains a positive integer n (2≤n≤100000) — the number of trophies.
The second line of each test case contains a string s of length n consisting only of characters 'L' and 'R' — the i-th character describes the direction of the i-th lamp.
It is guaranteed that the sum of n over all test cases does not exceed 100000.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤10000)。随后是各测试用例的描述。
每个测试用例的第一行包含一个正整数 n(2≤n≤100000)—— 表示奖杯的数量。
每个测试用例的第二行包含一个长度为 n 的字符串 s,仅由字符 'L' 和 'R' 组成 —— 其中第 i 个字符表示第 i 个灯的方向。
保证所有测试用例的 n 之和不超过 100000。
输出格式
For each test case print −1 if it is impossible to illuminate all trophies by performing one operation (or doing nothing). Otherwise, print 0 if you choose not to perform the operation (i.e., the trophies are illuminated by the initial positioning of the lamps), or an index i (1≤i<n) if you choose to swap lamps i and i+1.
If there are multiple answers, print any.
对于每个测试用例,如果通过执行一次操作(或不执行任何操作)无法照亮所有奖杯,则输出 −1;否则,若选择不执行操作(即奖杯已由灯的初始位置照亮),则输出 0;若选择交换第 i 盏与第 i+1 盏灯(其中 1≤i<n),则输出索引 i。
若存在多个可行答案,输出任意一个即可。
输入输出样例
输入#1
6 2 LL 2 LR 2 RL 2 RR 7 LLRLLLR 7 RRLRRRL
输出#1
-1 1 0 -1 3 6
说明/提示
In the first example, it is possible to swap lamps 1 and 2, or do nothing. In any case, the string "LL" is obtained. Not all trophies are illuminated since trophy 2 is not illuminated by any lamp — lamp 1 illuminates nothing and lamp 2 illuminates only the trophy 1.
In the second example, it is necessary to swap lamps 1 and 2. The string becomes "RL". Trophy 1 is illuminated by lamp 2 and trophy 2 is illuminated by lamp 1, hence it is possible to illuminate all trophies.
In the third example, all trophies are initially illuminated — hence, not performing any operation is a valid solution.
In the last two examples performing swaps is not necessary as all trophies are illuminated initially. But, the presented solutions are also valid.
在第一个例子中,可以交换灯 1 和 2,或者不进行任何操作。无论哪种情况,最终得到的字符串均为 "LL"。并非所有奖杯都被照亮,因为奖杯 2 未被任何灯照亮——灯 1 不照亮任何奖杯,而灯 2 仅照亮奖杯 1。
在第二个例子中,必须交换灯 1 和 2。字符串变为 "RL"。奖杯 1 被灯 2 照亮,奖杯 2 被灯 1 照亮,因此可以照亮所有奖杯。
在第三个例子中,所有奖杯初始时均已被照亮——因此,不执行任何操作即为一个有效解。
在最后两个例子中,由于所有奖杯初始时均已照亮,故无需执行交换操作。但所给出的解法同样有效。
输入解题思路,AI测评打分。不知道怎么写?