题目链接: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. HDU 5083 Instruction(字符串处理)

    Problem Description Nowadays, Jim Green has produced a kind of computer called JG. In his computer, ...

  2. vue时间格式化

    export function formatTime(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.g ...

  3. Python easyGUI 文件对比 覆盖保存

    #在35-3的基础上进行优化,当用户点击ok按钮的时候,对打开的文件进行检查是否修改.# 如果修改过,则提示覆盖保存.放弃保存.另存为并实现相应的功能 1 import easygui as g im ...

  4. scala函数demo

    /** * Created by root * Description : 柯里化函数,偏应用函数,匿名函数,高阶函数 */ object FunctionTest { def main(args: ...

  5. pyhton验证码识别

    1.PIL 下载地址: PIL-1.1.7.win-amd64-py2.7.exe 2.tesseract-ocr下载地址: tesseract-ocr-setup-3.02.02.exe 3.pyt ...

  6. Android开发-- The content of the adapter has changed but ListView did not receive a notification - With AsyncTask

    最近在联系开发DaysMatter时遇到一个问题: app中使用ListView来展示所有事件,每次添加完事件后使用下面代码来更新ListView. toDoListView.refreshDrawa ...

  7. php-fpm 配置进程池

    什么是 php-fpm :php 是作为一个独立服务存在的,这个服务叫做 php-fpm什么是 php-fpm pool :也就是 php-fpm 的进程池,这个进程池中运行了多个子进程,用来并发处理 ...

  8. date 类型转为varchar

    select t.type_id as typeId, t.type_name as typeName, t.type_order as typeOrder, t.type_link as typeL ...

  9. Python网络编程笔记

    01. UDP(user datagram protocol)用户数据报协议 01. 特点 01. 无连接 02. 不可靠 03. 每个被传输的数据报必须限定在64KB之内 02. 优点:效率高s 缺 ...

  10. 深入理解 Neutron -- OpenStack 网络实现(4):网络名字空间

    问题导读1.如何查看网络名字空间?2.网络名字空间开头的名字有什么规律?3.dhcp服务是如何实现的?4.router的实现是通过iptables进行的是否正确?5.SNAT和DNAT规则有什么作用? ...