codeforces 1779A. Hall of Fame

文章描述了一个关于国际象棋名人Thalia的谜题,她的奖杯由L和R标记的灯照亮。每个灯可以向左或向右照亮相邻的奖杯。题目要求通过最多一次操作(交换相邻灯的位置)来确保所有奖杯都被照亮,或者在无法做到时告知不可能。给定输入包括奖杯数量和灯的方向,输出应指示是否需要操作以及如何操作。示例展示了不同情况下的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Hall of Fame
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.

Input
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.

Output
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.

Example
inputCopy
6
2
LL
2
LR
2
RL
2
RR
7
LLRLLLR
7
RRLRRRL
outputCopy
-1
1
0
-1
3
6
Note
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.

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+7;
void solve(){
    int n;
    string s;
    cin>>n>>s;
    if(s.find("RL")!=string::npos){
        cout<<0<<endl;
        return;
    }
    if(s.find("LR")!=string::npos){
        cout<<s.find("LR")+1<<endl;
        return;
    }
    cout<<-1<<endl;
}
int main(void){
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}
//code by 01100_10111;
### Codeforces 1732A Bestie 题目解析 对于给定的整数数组 \(a\) 和查询次数 \(q\),每次查询给出两个索引 \(l, r\),需要计算子数组 \([l,r]\) 的最大公约数(GCD)。如果 GCD 结果为 1,则返回 "YES";否则返回 "NO"[^4]。 #### 解决方案概述 为了高效解决这个问题,可以预先处理数据以便快速响应多个查询。具体方法如下: - **预处理阶段**:构建辅助结构来存储每一对可能区间的 GCD 值。 - **查询阶段**:利用已有的辅助结构,在常量时间内完成每个查询。 然而,考虑到内存限制以及效率问题,直接保存所有区间的结果并不现实。因此采用更优化的方法——稀疏表(Sparse Table),它允许 O(1) 时间内求任意连续子序列的最大值/最小值/GCD等问题,并且支持静态RMQ(Range Minimum Query)/RANGE_GCD等操作。 #### 实现细节 ##### 构建稀疏表 通过动态规划的方式填充二维表格 `st`,其中 `st[i][j]` 表示从位置 i 开始长度为 \(2^j\) 的子串的最大公约数值。初始化时只需考虑单元素情况即 j=0 的情形,之后逐步扩展至更大的范围直到覆盖整个输入序列。 ```cpp const int MAXN = 2e5 + 5; int st[MAXN][20]; // Sparse table for storing precomputed results. vector<int> nums; void build_sparse_table() { memset(st,-1,sizeof(st)); // Initialize the base case where interval length is one element only. for(int i = 0 ;i < nums.size(); ++i){ st[i][0]=nums[i]; } // Fill up sparse table using previously computed values. for (int j = 1;(1 << j)<=nums.size();++j){ for (int i = 0;i+(1<<j)-1<nums.size();++i){ if(i==0 || st[i][j-1]!=-1 && st[i+(1<<(j-1))][j-1]!=-1) st[i][j]=__gcd(st[i][j-1],st[i+(1<<(j-1))][j-1]); } } } ``` ##### 处理查询请求 当接收到具体的 l 和 r 参数后,可以通过查找对应的 log₂(r-l+1) 来定位合适的跳跃步长 k ,进而组合得到最终答案。 ```cpp string query(int L,int R){ int K=(int)(log2(R-L+1)); return __gcd(st[L][K],st[R-(1<<K)+1][K])==1?"YES":"NO"; } ``` 这种方法能在较短时间内完成大量查询任务的同时保持较低的空间开销,非常适合本题设定下的性能需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值