Codeforces 404E Maze 1D

http://codeforces.com/contest/404/problem/E

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.

 

题意:一个机器人在数轴上的0点  给一串指令机器人按照指令走  为了使机器人最后一步走到一个从来没来过的位置  我们可以在数轴上放石头  每次机器人被石头卡住他就跳过当前的那个指令  问  最少使用石头前提下  一共几种放石头方法

思路:

1.最后一个指令是L/R,一定停在0的左/右侧。因为最后一个命令是L,即向左走走到了新位置,这时一定是在0的左侧,因为如果在右侧,从0到这个位置是都走过的;如果最后一个命令是R,即向右走走到了新位置,这时一定是在0的右侧。

2.要么不放障碍,要么只放一个。(以下均以最后一个指令是R举例,L是对称的)既然最后在0右侧,那么放障碍一定不放在0的右侧,否则要么没用,要么阻碍完成目标,因此只放左边,放多个和放一个是一样的,只取决于最靠右的那个。

3.不放障碍就可以的话就输出1结束,放1个障碍不管放在哪都不行的话,那就确实是不行的。主要考虑放一个障碍的情形。

4.二分性质:放在x处可行的话,那么放在[x+1,-1]也是可行的。拿x+1举例,我们想象这个执行的过程,a和b都到了x+1,然后向左一个,a到了x,b还是在x+1,之后b的每一个动作就相当于a右移一格的。

做法:二分放障碍的最远位置,往外都不行,往内都行,输出个数。

#include<bits/stdc++.h>
using namespace std;
#define base 1000000
#define maxn 2000000+100

char s[maxn];
int v[maxn];
int n,ans;

bool check(int p)
{
	int pos=0;
	memset(v,0,sizeof(v));
	v[pos+base]=1;
	for(int i=1;i<=n;i++)
	{	
		if(s[i]=='L'&&p==pos-1 || s[i]=='R'&&p==pos+1)continue;
		s[i]=='L'?pos--:pos++;
		v[base+pos]++;
	}
	return v[base+pos]==1;
}

int main()
{
	//freopen("input.in","r",stdin);
	scanf("%s",s+1);
	n=strlen(s+1);
	if(check(base*10)){cout<<1;exit(0);}
	int l,r,m;
	if(s[n]=='L')l=1,r=n;
	else l=-n,r=-1;
	while(l<=r)
	{
		m=(l+r)/2;
		if(check(m))s[n]=='L'?l=m+1:r=m-1,ans=m;
		else s[n]=='L'?r=m-1:l=m+1;
	}
	cout<<abs(ans);
	return 0;
}

 

 

 

 

 

### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值