Codeforces 752C - Santa Claus and Robot - [简单思维题]
题目链接:http://codeforces.com/problemset/problem/752/C
2 seconds
256 megabytes
standard input
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.
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.
The only line of input should contain the minimum possible length of the sequence.
4
RURD
2
6
RRULDD
2
26
RRRULURURUULULLLDLDDRDRDLD
7
3
RLL
2
4
LRLR
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 - [简单思维题]的更多相关文章
- codeforces Round #389(Div.2)C Santa Claus and Robot(思维题)
题目链接:http://codeforces.com/contest/752/problem/C 题意:给出一系列机器人的行动方向(机器人会走任意一条最短路径),问最少标记几个点能让机器人按这个 路径 ...
- 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 ...
- CodeForces 748C Santa Claus and Robot (思维)
题意:给定一个机器人的行走路线,求最少的点能使得机器人可以走这样的路线. 析:每次行走,记录一个方向向量,每次只有是相反方向时,才会增加一个点,最后再加上最后一个点即可. 代码如下: #pragma ...
- CodeForces - 748C Santa Claus and Robot
题意:机器人在网格线上行走,从p1点开始,沿最短路径到p2,再沿最短路径到p3,依此类推.在此过程中留下了行走的运动轨迹,由“RLDU”表示.问若只给出运动轨迹,求最少的pi点的个数. 分析:pi到p ...
- 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 ...
- codeforces 748E Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces 784B Santa Claus and Keyboard Check
题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...
- Codeforces 878D - Magic Breeding(bitset,思维题)
题面传送门 很容易发现一件事情,那就是数组的每一位都是独立的,但由于这题数组长度 \(n\) 很大,我们不能每次修改都枚举每一位更新其对答案的贡献,这样复杂度必炸无疑.但是这题有个显然的突破口,那就是 ...
- C. Santa Claus and Robot 思考题
http://codeforces.com/contest/752/problem/C 这题的意思其实就是叫你固定x个点,使得按顺序走这x个点后,产生的轨迹是给定的序列. 对于有若干条最短路径走到第i ...
随机推荐
- Dubbo -- 系统学习 笔记 -- 示例 -- 只注册
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 只注册 问题 如果有两个镜像环境,两个注册中心,有一个服务只在其中一个注册中心有部 ...
- SpringMVC -- 梗概--源码--贰--RestFul收参(了解) @PathVariable
1>定制方式: //如下两个路径都可以访问到如下方法,请求路径不同,则name61和pwd61匹配到的值不同 //http://localhost:8989/appname/ful/lime/1 ...
- 精神状态: Confused
阿里和网易都已开放简历投递入口,本以为招聘季9月才开始的我,着实被震惊到了. 我还没准备好呢,远没有准备好. 这次日志,主要是想写三点.实习经历.接下来的计划.最后,自已在未来应该维持的心态. 关于实 ...
- WAMP运行分析
- Splash images_enabled 属性
images_enabled属性用于设置加载页面时是否加载图片,如下,禁止之后,返回的页面截图就不会带有任何图片,加载速度也会快很多 function main(splash, args) splas ...
- [JS] 如何自定义字符串格式化输出
在其他语言中十分常见的字符串格式化输出,居然在 Javascript 中不见踪影,于是决定自己实现该方法,以下就是个人编写的最简洁实现: String.prototype.format = funct ...
- Java网络编程之TCP通信
一.概述 Socket类是Java执行客户端TCP操作的基础类,这个类本身使用代码通过主机操作系统的本地TCP栈进行通信.Socket类的方法会建立和销毁连接,设置各种Socket选项. Server ...
- composer安装指定版本的laravel
composer create-project laravel/laravel blog 4.2.*
- django restframwork 教程之authentication权限
当前我们的API在编辑或者删除的时候没有任何限制,我们不希望有些人有高级的行为,确保: 代码段始终与创建者相关联 只允许授权的用户可以创建代码段 只允许代码段创建者可以更新和删除 没有认证的请求应该有 ...
- js元素闪动效果
<img src="http://yjy.allbring.com/UpLoadFiles/head/p1_20140326104945_17-10164142709.jpg" ...