CodeForces - 748C Santa Claus and Robot(思维)
2017-04-08 09:01
429 查看
点击打开题目链接
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
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.
题目大意:一个机器人将要到达n个点,到达每个点都是最短路径,现已知机器人行走的路径,求出最小的n
思路:只要现在走的方向与上一次走的方向相反,那么途中必然经过一个点促使机器人改变方向,用四个方向的标记变量分别记录,然后对于每个点后都要初始化标记变量的值。
附上AC代码:
#include<bits/stdc++.h>>
using namespace std;
const int maxn=2*1e5+5;
char c[maxn];
int l=0,r=0,u=0,d=0;
int n;
int main()
{
cin>>n;
int cnt=1;
for(int i=0;i<n;i++)
{
cin>>c[i];
if(c[i]=='R')
{
if(l==1)
cnt++,l=u=d=0;
r=1;
}
if(c[i]=='L')
{
if(r==1)
cnt++,r=u=d=0;
l=1;
}
if(c[i]=='U')
{
if(d==1)
cnt++,r=l=d=0;
u=1;
}
if(c[i]=='D')
{
if(u==1)
cnt++,r=l=u=0;
d=1;
}
}
cout<<cnt<<endl;
}
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.
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.
题目大意:一个机器人将要到达n个点,到达每个点都是最短路径,现已知机器人行走的路径,求出最小的n
思路:只要现在走的方向与上一次走的方向相反,那么途中必然经过一个点促使机器人改变方向,用四个方向的标记变量分别记录,然后对于每个点后都要初始化标记变量的值。
附上AC代码:
#include<bits/stdc++.h>>
using namespace std;
const int maxn=2*1e5+5;
char c[maxn];
int l=0,r=0,u=0,d=0;
int n;
int main()
{
cin>>n;
int cnt=1;
for(int i=0;i<n;i++)
{
cin>>c[i];
if(c[i]=='R')
{
if(l==1)
cnt++,l=u=d=0;
r=1;
}
if(c[i]=='L')
{
if(r==1)
cnt++,r=u=d=0;
l=1;
}
if(c[i]=='U')
{
if(d==1)
cnt++,r=l=d=0;
u=1;
}
if(c[i]=='D')
{
if(u==1)
cnt++,r=l=u=0;
d=1;
}
}
cout<<cnt<<endl;
}
相关文章推荐
- CodeForces 748C Santa Claus and Robot (思维)
- Codeforces 752C Santa Claus and Robot 思维
- 【Codeforces 748 C Santa Claus and Robot】+ 思维
- 748C Santa Claus and Robot
- 【codeforces 752C】Santa Claus and Robot
- 748C Santa Claus and Robot
- Codeforces Round #389(Div.2)C Santa Claus and Robot【思维】
- 【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
- C. Santa Claus and Robot 思考题
- CodeForces - 748E Santa Claus and Tangerines(枚举)
- Codeforces 748 E Santa Claus and Tangerines
- codeforces 748E Santa Claus and Tangerines
- 【codeforces 752E】Santa Claus and Tangerines
- CodeForces 354A - Vasya and Robot (简单思维)
- codeforces748c-Santa Claus and Robot
- Codeforces Round #389 Div.2 C. Santa Claus and Robot
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法二】