题目链接:http://codeforces.com/problemset/problem/752/C

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

Copy
4
RURD
output
2
input

Copy
6
RRULDD
output
2
input

Copy
26
RRRULURURUULULLLDLDDRDRDLD
output
7
input

Copy
3
RLL
output
2
input

Copy
4
LRLR
output
4
 
题意:

若圣诞老人要按顺序送m个礼物(每个礼物都有一个坐标),则他会在任意两个礼物间走最短的路线到达;

现在只知道圣诞老人送礼物的所走的路径,要求圣诞老人最少送了多少个礼物。

题解:

首先,从某一点出发(这一点可以是某个礼物,也可以是起点),

一旦圣诞老人往L走了一步,最短路径就不允许他走R(同样的道理,一旦走了L,就不能再走D);

所以我们只要定义一个flag[1:4]的数组,从某一起点记录下他是否有走L、R、U、D,一旦出现矛盾(L-R,U-D)……

  则必然到达了一个礼物点,ans+=1;

  then,清零flag数组,表示这个礼物点是新起点,再重新记录……

反复循环,直到走完;

最后终点的时候再加上一个礼物即可。

AC代码:

#include<bits/stdc++.h>
using namespace std; const int MAX=(int)(2e5+); int n,ans;
char Move[MAX];
bool flag[]; int main()
{
scanf("%d",&n);
scanf("%s",Move);
ans=;
for(int i=;i<n;i++)
{
if(Move[i]=='L')
{
if(flag[])
{
ans++;
flag[]=flag[]=flag[]=flag[]=;
}
flag[]=;
}
else if(Move[i]=='R')
{
if(flag[])
{
ans++;
flag[]=flag[]=flag[]=flag[]=;
}
flag[]=;
}
else if(Move[i]=='U')
{
if(flag[])
{
ans++;
flag[]=flag[]=flag[]=flag[]=;
}
flag[]=;
}
else //if(Move[i]=='D')
{
if(flag[])
{
ans++;
flag[]=flag[]=flag[]=flag[]=;
}
flag[]=;
}
}
printf("%d\n",++ans);
}

Codeforces 752C - Santa Claus and Robot - [简单思维题]的更多相关文章

  1. codeforces Round #389(Div.2)C Santa Claus and Robot(思维题)

    题目链接:http://codeforces.com/contest/752/problem/C 题意:给出一系列机器人的行动方向(机器人会走任意一条最短路径),问最少标记几个点能让机器人按这个 路径 ...

  2. CF 752C. Santa Claus and Robot

    C. Santa Claus and Robot time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  3. CodeForces 748C Santa Claus and Robot (思维)

    题意:给定一个机器人的行走路线,求最少的点能使得机器人可以走这样的路线. 析:每次行走,记录一个方向向量,每次只有是相反方向时,才会增加一个点,最后再加上最后一个点即可. 代码如下: #pragma ...

  4. CodeForces - 748C Santa Claus and Robot

    题意:机器人在网格线上行走,从p1点开始,沿最短路径到p2,再沿最短路径到p3,依此类推.在此过程中留下了行走的运动轨迹,由“RLDU”表示.问若只给出运动轨迹,求最少的pi点的个数. 分析:pi到p ...

  5. Codeforces Round #389 Div.2 C. Santa Claus and Robot

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. codeforces 748E Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  7. Codeforces 784B Santa Claus and Keyboard Check

    题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...

  8. Codeforces 878D - Magic Breeding(bitset,思维题)

    题面传送门 很容易发现一件事情,那就是数组的每一位都是独立的,但由于这题数组长度 \(n\) 很大,我们不能每次修改都枚举每一位更新其对答案的贡献,这样复杂度必炸无疑.但是这题有个显然的突破口,那就是 ...

  9. C. Santa Claus and Robot 思考题

    http://codeforces.com/contest/752/problem/C 这题的意思其实就是叫你固定x个点,使得按顺序走这x个点后,产生的轨迹是给定的序列. 对于有若干条最短路径走到第i ...

随机推荐

  1. AndroidManifest详解

    一,重要性AndroidManifest.xml是Android应用程序中最重要的文件之一.它是Android程序的全局配置文件,是每个 android程序中必须的文件.它位于我们开发的应用程序的根目 ...

  2. Dubbo -- 系统学习 笔记 -- API参考手册

    Dubbo -- 系统学习 笔记 -- 目录 API参考手册 配置API 注解API 模型API 上下文API 服务API API参考手册 Dubbo的常规功能,都保持零侵入,但有些功能不得不用API ...

  3. 【视频】ffmpeg mov mp4 m3u8 ts

    1.https://ffmpeg.zeranoe.com/builds/ 2.https://blog.csdn.net/psh18513234633/article/details/79312607 ...

  4. 一句话木马:ASP篇

    ASP一句话木马收集: <%eval request("chopper")%> <%execute request("chopper")%&g ...

  5. 查找被占用的端口的服务并kill掉

    转自:http://blog.csdn.net/gsls200808/article/details/52456136 方法: C:\>netstat -ano|findstr 8000 TCP ...

  6. Kafka一些常见资源汇总

    终于下定决心写一点普及类的东西.很多同学对Kafka的使用很感兴趣.如果你想参与到Kafka的项目开发中,很多资源是你必须要提前准备好的.本文罗列了一些常用的Kafka资源,希望对这些develope ...

  7. 说说C与汇编之间的互相联系(转)

    在嵌入式系统开发中,目前使用的主要编程语言是C和汇编,C++已经有相应的编译器,但是现在使用还是比较少的.在稍大规模的嵌入式软件中,例如含有OS,大部分的代码都是用C编写的,主要是因为C语言的结构比较 ...

  8. N76E003之ISP

    Flash存储器支持硬件编程和应用编程(IAP).如果产品在研发阶段或产品需要更新软固件时,硬件编程就显得不太方便,采用在系统编程(ISP)方式,可使这一过程变得方便.执行ISP不需要将控制器从系统板 ...

  9. 最短路径——Dijkstra算法和Floyd算法

    Dijkstra算法概述 Dijkstra算法是由荷兰计算机科学家狄克斯特拉(Dijkstra)于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图(无 ...

  10. Struts2(五)数据校验

    一.概述 在提交表单数据时,如果数据需要保存到数据库,空输入等可能会引发一些异常,为了避免引起用户的输入引起底层异常,通常在进行业务逻辑操作之前,先执行基本的数据校验. 下面通过两种方式来阐述Stru ...