codeforces Round #237(div2) E解题报告

E. Maze 1D
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Valera has a strip infinite in both directions and consisting of cells. The cells are numbered by integers. The cell number 0 has a robot.

The robot has instructions — the sequence of moves that he must perform. In one move, the robot moves one cell to the left or one cell to the right, according to instructions. Before the robot starts moving, Valera puts obstacles in some cells of the strip, excluding cell number 0. If the robot should go into the cell with an obstacle according the instructions, it will skip this move.

Also Valera indicates the finish cell in which the robot has to be after completing the entire instructions. The finishing cell should be different from the starting one. It is believed that the robot completed the instructions successfully, if during the process of moving he visited the finish cell exactly once — at its last move. Moreover, the latter move cannot be skipped.

Let's assume that k is the minimum number of obstacles that Valera must put to make the robot able to complete the entire sequence of instructions successfully and end up in some finishing cell. You need to calculate in how many ways Valera can choose k obstacles and the finishing cell so that the robot is able to complete the instructions successfully.

Input

The first line contains a sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), consisting only of letters "L" and "R". If character si equals "L", then the robot on the i-th move must try to move one cell to the left. If the si-th character equals "R", then the robot on the i-th move must try to move one cell to the right.

Output

Print a single integer — the required number of ways. It's guaranteed that this number fits into 64-bit signed integer type.

Sample test(s)
input
RR
output
1
input
RRL
output
1
Note

In the first sample Valera mustn't add any obstacles and his finishing cell must be cell 2.

In the second sample, Valera must add an obstacle in cell number 1, and his finishing cell must be cell number  - 1. In this case robot skips the first two moves and on the third move he goes straight from the starting cell to the finishing one. But if Valera doesn't add any obstacles, or adds an obstacle to another cell, then the robot visits the finishing cell more than once.


题目大意:

        1维的迷宫,有个机器人在里面转悠,主人已经给出了1堆的指令,而且机器人必须按顺序照做,如果指令要求到达的地方遇到障碍物,则可以跳过,但要求最后一个指令不能跳过.最后一个格子必须是之前都没有经过的.

        求放置障碍物的最少个数,并且输出方案


解法:

        分析题目,障碍物一边最多一个(假设左边放了2个,但是实际上跟放1个障碍物没区别,因为碰到了第一个障碍物就过不去了,之后的障碍物纯粹摆设). 然后要求最后一个各自必须是之前都没有经过的,而且最后一个指令必须执行(不能遇到障碍物),就可以知道,不存在放置2个障碍物的情况.

        试着想一下,如果在某个点x放置一个障碍物,符合题目要求,那么在x-1这个点是否符合呢?

        以障碍物放在0点右端为例子,当机器人站在x-1点,x点有障碍物时,那些无效的'R'指令都会省去,当我们在x-2点时,x-1点有障碍物时,会有更多的无效'R'指令产生,而'L'指令依然是照常执行,那么机器人最终运行到最左端的距离会比前一个更远(至少会一样).那么这样就符合二分的性质了.

        由于最后一条指令必须执行,我们可以知道如果最后一条指令是'R',那么必须障碍物在左边,反之亦然.

        当然还有不用放置障碍物的情况,模拟一边即可,且答案为1


小技巧: 网上看到某段代码根据最终指令将所有情况全部统一为障碍物在右边.这样就大大的减少了代码量:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char str[1111111];
char last;
int len;

bool check(int n)
{
    int pos=0;
    int minp=0;
    for(int i=0;i<len;i++)
    {
        minp = min(minp,pos);
        if(str[i]==last)
            pos--;
        else
            pos++;
        if(pos==n) pos--;
    }
    return pos<minp;
}

int main()
{
    scanf("%s",str);
    len = strlen(str);
    last = str[len-1];

    int l=1,r=len;
    while(l<=r)
    {
        int m = (l+r)>>1;
        if(!check(m))
            r = m-1;
        else
            l = m+1;
    }

    printf("%d\n", r==len?1:r);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值