Codeforces Round #389(Div.2)C Santa Claus and Robot【思维】
2016-12-25 21:53
633 查看
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.
题目大意:
给你一个行进的过程,每次我们行进都走的是最短路。
让你选择最少的点的个数,从起点走到终点的路径可以是字符串模拟出来的路径.
题干下方四个图中,黑色点就是选中的点。
思路:
1、不难理解,对应最后一个点一定是需要选中的一个点。
2、我们将路径进行分段,考虑其他点选取的方式:我们有一个起点,对应走到下一个点的距离如果是 增加的,那么很肯定这条路径的走法,如果对应走到下一个点的距离是减少的,那么我们不能从那个起点按照这个过程来行进,因为每次走的都是最短路,所以此时如果走到下一个点到起点的距离,小于了当前点到起点的距离,那么此时一定要选取当前点作为下一段路径的起点。
3、确定好思路之后,按照过程分类讨论模拟即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
char a[45454545];
int main()
{
int n;
while(~scanf("%d",&n))
{
int x=0,y=0;
int prex=0,prey=0;
int output=0;
scanf("%s",a);
for(int i=0;i<n;i++)
{
int flag=0;
if(a[i]=='U')
{
int predis=abs(x-prex)+abs(y-prey);
x--;
int nowdis=abs(x-prex)+abs(y-prey);
if(nowdis<predis)
{
flag=1;
prex=x+1;
prey=y;
}
}
if(a[i]=='D')
{
int predis=abs(x-prex)+abs(y-prey);
x++;
int nowdis=abs(x-prex)+abs(y-prey);
if(nowdis<predis)
{
flag=1;
prex=x-1;
prey=y;
}
}
if(a[i]=='L')
{
int predis=abs(x-prex)+abs(y-prey);
y--;
int nowdis=abs(x-prex)+abs(y-prey);
if(nowdis<predis)
{
flag=1;
prex=x;
prey=y+1;
}
}
if(a[i]=='R')
{
int predis=abs(x-prex)+abs(y-prey);
y++;
int nowdis=abs(x-prex)+abs(y-prey);
if(nowdis<predis)
{
flag=1;
prex=x;
prey=y-1;
}
}
output+=flag;
if(i==n-1)output++;
}
printf("%d\n",output);
}
}
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.
题目大意:
给你一个行进的过程,每次我们行进都走的是最短路。
让你选择最少的点的个数,从起点走到终点的路径可以是字符串模拟出来的路径.
题干下方四个图中,黑色点就是选中的点。
思路:
1、不难理解,对应最后一个点一定是需要选中的一个点。
2、我们将路径进行分段,考虑其他点选取的方式:我们有一个起点,对应走到下一个点的距离如果是 增加的,那么很肯定这条路径的走法,如果对应走到下一个点的距离是减少的,那么我们不能从那个起点按照这个过程来行进,因为每次走的都是最短路,所以此时如果走到下一个点到起点的距离,小于了当前点到起点的距离,那么此时一定要选取当前点作为下一段路径的起点。
3、确定好思路之后,按照过程分类讨论模拟即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
char a[45454545];
int main()
{
int n;
while(~scanf("%d",&n))
{
int x=0,y=0;
int prex=0,prey=0;
int output=0;
scanf("%s",a);
for(int i=0;i<n;i++)
{
int flag=0;
if(a[i]=='U')
{
int predis=abs(x-prex)+abs(y-prey);
x--;
int nowdis=abs(x-prex)+abs(y-prey);
if(nowdis<predis)
{
flag=1;
prex=x+1;
prey=y;
}
}
if(a[i]=='D')
{
int predis=abs(x-prex)+abs(y-prey);
x++;
int nowdis=abs(x-prex)+abs(y-prey);
if(nowdis<predis)
{
flag=1;
prex=x-1;
prey=y;
}
}
if(a[i]=='L')
{
int predis=abs(x-prex)+abs(y-prey);
y--;
int nowdis=abs(x-prex)+abs(y-prey);
if(nowdis<predis)
{
flag=1;
prex=x;
prey=y+1;
}
}
if(a[i]=='R')
{
int predis=abs(x-prex)+abs(y-prey);
y++;
int nowdis=abs(x-prex)+abs(y-prey);
if(nowdis<predis)
{
flag=1;
prex=x;
prey=y-1;
}
}
output+=flag;
if(i==n-1)output++;
}
printf("%d\n",output);
}
}
相关文章推荐
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- 748 C. Santa Claus and Robot codeforces
- Codeforces Round 421 Div.2 D. Mister B and PR Shifts 思维 乱搞
- Codeforces Round #206 (Div. 2) C - Vasya and Robot
- Codeforces Round #253 (Div. 2) E. Artem and Array (思维)
- Codeforces Round #190 DIV.2 A. Ciel and Dancing
- Codeforces Round #421 (Div. 1):B. Mister B and PR Shifts 思维,乱搞