【Codeforces 748 C Santa Claus and Robot】+ 思维
2016-12-26 19:26
483 查看
C. Santa Claus and Robot
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, …, pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it’ll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.
While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
Input
The first line of input contains the only positive integer n (1 ≤ n ≤ 2·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R — to the right, U — to the top and D — to the bottom. Have a look at the illustrations for better explanation.
Output
The only line of input should contain the minimum possible length of the sequence.
Examples
Input
4
RURD
Output
2
Input
6
RRULDD
Output
2
Input
26
RRRULURURUULULLLDLDDRDRDLD
Output
7
Input
3
RLL
Output
2
Input
4
LRLR
Output
4
Note
The illustrations to the first three tests are given below.
题意~给出机器人的行走路线,问最少的需要多少个点,机器人每次会选择最短的路走
思路 : 记录下没每走的方向,可以用两个变量分别记录上下,和左右,如果这一步走的方向与之前的方向相反了,者需要加一个点,即把另一个方向变量初始化~
AC代码:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, …, pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it’ll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.
While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
Input
The first line of input contains the only positive integer n (1 ≤ n ≤ 2·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R — to the right, U — to the top and D — to the bottom. Have a look at the illustrations for better explanation.
Output
The only line of input should contain the minimum possible length of the sequence.
Examples
Input
4
RURD
Output
2
Input
6
RRULDD
Output
2
Input
26
RRRULURURUULULLLDLDDRDRDLD
Output
7
Input
3
RLL
Output
2
Input
4
LRLR
Output
4
Note
The illustrations to the first three tests are given below.
题意~给出机器人的行走路线,问最少的需要多少个点,机器人每次会选择最短的路走
思路 : 记录下没每走的方向,可以用两个变量分别记录上下,和左右,如果这一步走的方向与之前的方向相反了,者需要加一个点,即把另一个方向变量初始化~
AC代码:
#include<bits/stdc++.h> using namespace std; char st[200010]; int main() { int N,n1 = 0,n2 = 0,n = 1; scanf("%d %s",&N,st); for(int i = 0 ; i < N; i++){ if(st[i] == 'L'){ if(n1 == 1) n++,n2 = 0; n1 = 2; } if(st[i] == 'R'){ if(n1 == 2) n++,n2 = 0; n1 = 1; } if(st[i] == 'U'){ if(n2 == 1) n++,n1 = 0; n2 = 2; } if(st[i] == 'D'){ if(n2 == 2) n++,n1 = 0; n2 = 1; } } printf("%d\n",n); return 0; }
相关文章推荐
- CodeForces - 748C Santa Claus and Robot(思维)
- CodeForces 748C Santa Claus and Robot (思维)
- Codeforces 752C Santa Claus and Robot 思维
- Codeforces Round #389(Div.2)C Santa Claus and Robot【思维】
- 【codeforces 752C】Santa Claus and Robot
- 【Codeforces Round #389】Codeforces 752C Santa Claus and Robot
- 【Codeforces 748 B Santa Claus and Keyboard Check】
- 【codeforces 752C】Santa Claus and Robot
- 【codeforces 752C】Santa Claus and Robot
- 【codeforces 752C】Santa Claus and Robot
- 748 C. Santa Claus and Robot codeforces
- Codeforces 748 E Santa Claus and Tangerines
- 【codeforces 752E】Santa Claus and Tangerines
- CodeForces 748E. Santa Claus and Tangerines(二分,递推)
- Codeforces 748B-Santa Claus and Keyboard Check
- 【codeforces 752B】Santa Claus and Keyboard Check
- Codeforces 752E Santa Claus and Tangerines 二分+记忆化
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法一】
- Codeforces 322C Ciel and Robot【思维+模拟】细节很多= =
- C. Santa Claus and Robot 思考题