题目链接: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. iOS去掉icon的(自带磨光效果)gloss effects

    只需两步,第一步:在项目的plist文件,最上层add row ,内容 icon already includes gloss effects   YES. 第二步在 icon files 字段里添加 ...

  2. Java实现策略模式的简单应用

    在使用图像处理软件处理图片后,需要选择一种格式进行保存.然而各种格式在底层实现的算法并不相同,这刚好适合策略模式.编写程序,演示如何使用策略模式与简单工厂模式组合进行开发. 思路如下: 使用inter ...

  3. lua 根据函数名字符串来执行函数

    function myfunction(msg) print("this is msg fun " .. msg); end local fun =_G["myfunct ...

  4. Bypass X-WAF SQL注入防御(多姿势)

    0x00 前言 ​ X-WAF是一款适用中.小企业的云WAF系统,让中.小企业也可以非常方便地拥有自己的免费云WAF. ​ 本文从代码出发,一步步理解WAF的工作原理,多姿势进行WAF Bypass. ...

  5. Ajax 结果提取

    Python 如何提取 Ajax 真正响应的内容: 以 https://m.weibo.cn/u/2830678474 这个网页为例,选择其中一个 Ajax 请求,找到请求的URL和传递的参数 imp ...

  6. drawCall_01

    在屏幕上渲染物体,引擎需要发出一个绘制调用来访问图形API(iOS系统中为OpenGL ES).每个绘制调用需要进行大量的工作来访问图形API,从而导致了CPU方面显著的性能开销.   Unity在运 ...

  7. linux 开机自启

    cd /home/dpf 1.vi startup.sh #!/bin/shecho "hello world" >> /home/dpf/test.txt vi mq ...

  8. MDK972-EK开发板裸调试设置和裸机程序烧写(转)

    硬件平台:MDK972-EK开发板编译调试软件:KEIL uVision4仿真工具:JLINK V7/V8   本例子从串口输出信息,如图:       KEIL uVision4调试设置如图所示: ...

  9. ip防刷脚本

    #!/bin/sh #防刷脚本 #env ACCESS_PATH=/home/wwwlogs ACCESS_LOG=y.log IPTABLES_TOP_LOG=iptables_top.log DR ...

  10. jQuery属性操作(一)

    下载了jQuery的UI组件,发现内容还挺多的,还是决定先把jQuery的源码看完一遍之后再涉足UI组件.考虑到队列和动画使用较少,特别是动画,基本开始使用css3完成.因此暂时略过,开始看jQuer ...