ZOJ 3992 One-Dimensional Maze(思维题)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
BaoBao is trapped in a one-dimensional maze consisting of \(n\) grids arranged in a row! The grids are numbered from 1 to \(n\) from left to right, and the \(i\)-th grid is marked with a character \(s_i\), where \(s_i\) is either 'L' or 'R'.
Starting from the \(m\)-th grid, BaoBao will repeatedly take the following steps until he escapes the maze:
- If BaoBao is in the 1st grid or the \(n\)-th grid, then BaoBao is considered to arrive at the exit and thus can escape successfully.
- Otherwise, let BaoBao be in the \(t\)-th grid. If \(s_t = \text{'L'}\), BaoBao will move to the \((t-1)\)-th grid; If \(s_t = \text{'R'}\), Baobao will move to the \((t+1)\)-th grid.
Before taking the above steps, BaoBao can change the characters in some grids to help himself escape. Concretely speaking, for the \(i\)-th grid, BaoBao can change \(s_i\) from 'L' to 'R', or from 'R' to 'L'.
But changing characters in grids is a tiring job. Your task is to help BaoBao calculate the minimum number of grids he has to change to escape the maze.
Input
There are multiple test cases. The first line of the input contains an integer \(T\), indicating the number of test cases. For each test case:
The first line contains two integers \(n\) and \(m\) (\(3 \le n \le 10^5\), \(1 < m < n\)), indicating the number of grids in the maze, and the index of the starting grid.
The second line contains a string \(s\) (\(|s| = n\)) consisting of characters 'L' and 'R'. The \(i\)-th character of \(s\) indicates the character in the \(i\)-th grid.
It is guaranteed that the sum of \(n\) over all test cases will not exceed \(10^6\).
Output
For each test case output one line containing one integer, indicating the minimum number of grids BaoBao has to change to escape the maze.
Sample Input
3
3 2
LRL
10 4
RRRRRRRLLR
7 4
RLLRLLR
Sample Output
0
2
1
Hint
For the first sample test case, BaoBao doesn't have to change any character and can escape from the 3rd grid. So the answer is 0.
For the second sample test case, BaoBao can change \(s_8\) to 'R' and \(s_9\) to 'R' and escape from the 10th grid. So the answer is 2.
For the third sample test case, BaoBao can change \(s_4\) to 'L' and escape from the 1st grid. So the answer is 1.
给定起始位置,R表示向右,L表示向左,
现在要求输出最少改变几个字符可以移动到字符第一个或者最后一个。
例如:
6 3
LLRRLR
这道题目通过模拟就可以做出来,
就是将到达两边要改变的字符都求出来,输出最小的那个
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define max_v 100005
char str[max_v];
/*
Submit Failed
*/
int main()
{
int t;
int n,m,sum1,sum2;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
scanf("%s",str+);
sum1=;
sum2=;
for(int i=m;i<n;i++)
if(str[i]=='L')
sum1++;
for(int i=m;i>;i--)
if(str[i]=='R')
sum2++;
printf("%d\n",min(sum1,sum2));
}
return ;
}
/*
题意:给出一串只有RL的字符串,
给定起始位置,R表示向右,L表示向左,
现在要求输出最少改变几个字符可以移动到字符第一个或者最后一个。
例如:
6 3
LLRRLR 起始位置是第三个字符R,只用改变第三个R就可以到达第一个字符L
这道题目通过模拟就可以做出来,
就是将到达两边要改变的字符都求出来,输出最小的那个 */
ZOJ 3992 One-Dimensional Maze(思维题)的更多相关文章
- ZOJ 3983 Crusaders Quest(思维题)
C - Crusaders Quest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu S ...
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...
随机推荐
- Matlab给三维点云添加高斯噪声和随机噪声
写在前面 在我们进行点云配准一类的模拟实验时,第一步就是对原始点云进行适当的RT变换,并添加一定的噪声,得到测量点云,然后才可以用我们的算法去进行后面的配准操作.在添加噪声这一块,matlab里并没有 ...
- Docker for Windows(五)实践搭建SqlServer服务&执行数据库操作
上一篇我们已经搭建了一个mysql数据库服务了:Docker for Windows(四)实践搭建&删除MySQL服务,发现用Docker确实是方便且容易,但上一篇主要是服务的搭建删除等基础操 ...
- 用CSS的方法如何让一个元素不可见?(面试题目)
面试中看到这个问题,自己想的不全面,下面整理下,一起学习: 一.CSS元素隐藏 在CSS中,让元素隐藏(指屏幕范围内肉眼不可见)的方法很多,有的占据空间,有的不占据空间:有的可以响应点击,有的不能响应 ...
- unrecognized selector sent to class
Other Linker Flags=-ObjC -all_load Loads all members of static archive libraries. -ObjC Loads all me ...
- 润乾报表新功能–导出excel支持锁定表头
在以往的报表设计中,锁定表头是会经常被用到的一个功能,这个功能不仅能使浏览的页面更加直观,信息对应的更加准确,而且也提高了报表的美观程度.但是,很多客户在将这样的报表导出excel时发现exce ...
- linux 目录、文件名、logout、exit、shutdown、reboot、init 0、init 6、runlevel
/dev 设备目录/boot 系统启动目录/etc 配置文件保存目录/media./mnt./misc 挂载目录,实际可以自己随便定义一个目录作为挂载目录/opt 安装第三方软件位置,但现在 ...
- pycharm安装激活及简单设置
(1)在激活界面的License server输入:http://idea.liyang.io/:或者:点击help→Register→License sever ,输入http://idea.liy ...
- 12.Spring——Web MVC框架
1.Spring Web MVC 框架 2.Spring MVC Hello World 例子 1.Spring Web MVC 框架 Spring web MVC 框架提供了模型-视 ...
- MVC技术的面试问题
MVC中的三种方式: ORM框架:对象关系映射关系 ,面向对象的对象模型和关系型数据之间的相互转换.基于关系型数据库的数据存储,实现一个虚拟的面向对象的数据访问接口.只要提供了持久化类与表的映射关系, ...
- 转:asp.net 中的viewstate
概述 ViewState是一个被误解很深的动物了.我希望通过此文章来澄清人们对ViewState的一些错误认识.为了达到这个目的,我决定从头到尾详细的描述一下整个ViewState的工作机制,其中我会 ...