Codeforces Round #389 Div.2 C. Santa Claus and Robot
time limit per test
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 pointsp1, 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
思考可以发现,一段连续的操作序列中如果没有方向相反的指令,就可以用一个点确定线路,否则需要添加一个新点。
做题的时候想得有点复杂,模拟了一个坐标系,满足当前距离到上个路径点的距离不递减时,一直走,否则新建一个路径点。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*-''+ch;ch=getchar();}
return x*f;
}
int n;
char s[mxn];
int lx,ly,sx,sy;
int px,py;
int dist(){
return abs(px-sx)+abs(py-sy);
}
int main(){
n=read();
scanf("%s",s);
int i,j;
int len=strlen(s);
int last=,ans=;
lx=ly=px=py=sx=sy=;
bool flag=;
for(i=;i<len;i++){
// printf("%c\n",s[i]);
switch(s[i]){
case 'U':{px--; break;}
case 'R':{py++; break;}
case 'D':{px++; break;}
case 'L':{py--; break;}
}
int now=dist();
if(now<last){
sx=lx;sy=ly;
ans++;
last=;
i--;
continue;
}
last=now;
lx=px;ly=py;
}
cout<<ans+<<endl;
return ;
}
Codeforces Round #389 Div.2 C. Santa Claus and Robot的更多相关文章
- Codeforces Round #389 Div.2 D. Santa Claus and a Palindrome
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Round #389 Div.2 E. Santa Claus and Tangerines
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Round #389 Div.2 B. Santa Claus and Keyboard Check
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Round #389 Div.2 A. Santa Claus and a Place in a Class
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) C
Description Santa Claus has Robot which lives on the infinite grid and can move along its lines. He ...
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) 圣诞之夜!
A. Santa Claus and a Place in a Class 模拟题.(0:12) 题意:n列桌子,每列m张桌子,每张桌子一分为2,具体格式看题面描述.给出n,m及k.求编号为k的桌子在 ...
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL
D. Santa Claus and a Palindrome time limit per test 2 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) B
Description Santa Claus decided to disassemble his keyboard to clean it. After he returned all the k ...
随机推荐
- NOI2018准备Day2
昨天雄心壮志了一番,今天就有点儿松懈了,是生于忧患,死于安乐吗 刷了15道大水题,5道字符串,5道多维数组,5道顺序查找,9个小时,平均40分钟一道水题,目标10分钟一道......昨天才刷了20道. ...
- 增强for循环(forearch)
增强for循环是为了简化在遍历数组需要先获得数组的长度或者在遍历集合中的元素的时候需要使用迭代器的操作. 引入时间:JDK1.5 语法格式: for(数据类型 变量 :需要迭代的数组或者集合){ } ...
- [MetaHook] Find a function signature
Find a non-public function signature, we need a tool "IDA Pro" ( You can open picture in a ...
- realmswift的使用
官网:https://realm.io/ 1.说下数据库迁移的问题: 在func application(application: UIApplication, didFinishLaunchingW ...
- C#中数组Array、ArrayList、泛型List<T>的比较
在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...
- Python3 windows如何安装模块 setuptools
下载的module解压后里面有setup.py文件,如果打开setup.py文件里面有这段代码: from setuptools import setup ... setup( ... 这种的都需要调 ...
- 用户 'IIS APPPOOL\***' 登录失败
用户 'IIS APPPOOL\DefaultAppPool' 登录失败. 我在windows8中安装了iis之后添加了我做的网站打开之后提示用户 'IIS APPPOOL\DefaultAppPoo ...
- html+js实现图片预览
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 十天冲刺---Day5
站立式会议 站立式会议内容总结: 燃尽图 照片 PM确实不应该交给组内编码最强的人来做. 编码的过程还要考虑整个项目的流程压力较大. 需要队友的支持和沟通.
- Android中图片的处理(放大缩小,去色,转换格式,增加水印等)(转)
原文地址:http://menxu.lofter.com/post/164b9d_3ebf79 package com.teamkn.base.utils; import java.io.ByteAr ...