CF237 E 404E - Maze 1D(二分)

来源:互联网 发布:js table 高度 编辑:程序博客网 时间:2024/06/05 20:01

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
题意:给出一系列行动操作,求在放置障碍物尽量小的前提下障碍物的放置种类数
思路:不难想出至多只需要1个障碍物。先判不放障碍物能否实现。放障碍物的话在两边二分。比如起点是0.障碍物放-3能实现,那放-2和-1也一定可以实现。右边同理。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 2000800#define star 1000040char str[maxn];int vis[maxn];int len;bool Judge(int obs){int pos = star;int llef = pos,rrig = pos;memset(vis,0,sizeof(vis));vis[star] = 1;for(int i = 0;i < len;i++){if(str[i] == 'R' && (pos+1 != obs))pos++;if(str[i] == 'L' && (pos-1 != obs))pos--;vis[pos]++;}if(vis[pos] == 1 && pos != obs)return 1;return 0;}int main(){//freopen("in.txt","r",stdin);while(scanf("%s",str)!=EOF){len = strlen(str);int pos = star;memset(vis,0,sizeof(vis));vis[star] = 1;int lef = star,rig = star;for(int i = 0;i < len;i++){if(str[i] == 'R')pos++;else pos--;vis[pos]++;if(pos > rig)rig = pos;if(pos < lef)lef = pos;}if(vis[pos] == 1){printf("1\n");continue;}int ans = 0;int l = lef,r = star - 1;int ppp = star;while(l <= r){int mid = (l+r) >> 1;if(Judge(mid)){ppp = mid;r = mid - 1;}else l = mid+1;}ans += star - ppp;l = star + 1,r = rig;int pp = star;while(l <= r){int mid = (l+r) >> 1;if(Judge(mid)){pp = mid;l = mid+1;}else r = mid - 1;}ans += pp - star;printf("%d\n",ans);}return 0;}

0 0
原创粉丝点击