【codeforces 752C】Santa Claus and Robot
2017-07-10 22:25
323 查看
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.
Example
Input
Output
Input
Output
Input
Output
Input
Output
Input
Output
Note
The illustrations to the first three tests are given below.
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.
开始看到附图以为比较复杂就留在了后面做,最后看懂题目之后发现算是一道贪心,用数字表示方向,初始方向设0,可知,如果方向改变,就代表该方向上有一所求点,ans++即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
char str[200005];
int n;
int main()
{
while(cin>>n)
{
for(int i = 0; i < n; i++)
cin>>str[i];
int a = 0, b = 0, ans = 1;
for(int i = 0; i < n; i++)
{
if(str[i] == 'L')
{
if(a == 2){
b = 0;
ans++;
}
a = 1;
}
else if(str[i] == 'R')
{
if(a == 1){
b = 0;
ans++;
}
a = 2;
}
else if(str[i] == 'U')
{
if(b == 2){
a = 0;
ans++;
}
b = 1;
}
else if(str[i] == 'D')
{
if(b == 1)
{
a = 0;
ans++;
}
b = 2;
}
}
cout<<ans<<endl;
}
return 0;
}
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.
Example
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.
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.
开始看到附图以为比较复杂就留在了后面做,最后看懂题目之后发现算是一道贪心,用数字表示方向,初始方向设0,可知,如果方向改变,就代表该方向上有一所求点,ans++即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
char str[200005];
int n;
int main()
{
while(cin>>n)
{
for(int i = 0; i < n; i++)
cin>>str[i];
int a = 0, b = 0, ans = 1;
for(int i = 0; i < n; i++)
{
if(str[i] == 'L')
{
if(a == 2){
b = 0;
ans++;
}
a = 1;
}
else if(str[i] == 'R')
{
if(a == 1){
b = 0;
ans++;
}
a = 2;
}
else if(str[i] == 'U')
{
if(b == 2){
a = 0;
ans++;
}
b = 1;
}
else if(str[i] == 'D')
{
if(b == 1)
{
a = 0;
ans++;
}
b = 2;
}
}
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- 【Codeforces Round #389】Codeforces 752C Santa Claus and Robot
- 【codeforces 752C】Santa Claus and Robot
- 【codeforces 752C】Santa Claus and Robot
- 【codeforces 752C】Santa Claus and Robot
- Codeforces 752C Santa Claus and Robot 思维
- 【Codeforces 748 C Santa Claus and Robot】+ 思维
- CodeForces - 748C Santa Claus and Robot(思维)
- CodeForces 748C Santa Claus and Robot (思维)
- CodeForces 748E. Santa Claus and Tangerines(二分,递推)
- Codeforces 752E Santa Claus and Tangerines 二分+记忆化
- Codeforces 748E - Santa Claus and Tangerines
- CodeForces 748E. Santa Claus and Tangerines 题解(递推or二分)
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法一】
- 【Codeforces 748 B Santa Claus and Keyboard Check】
- 【codeforces 752E】Santa Claus and Tangerines
- 【codeforces 752B】Santa Claus and Keyboard Check
- CodeForces - 748E Santa Claus and Tangerines(枚举)
- C. Santa Claus and Robot 思考题
- 748C Santa Claus and Robot
- codeforces 748E Santa Claus and Tangerines