CF 752C. Santa Claus and Robot
C. Santa Claus and Robot
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
The illustrations to the first three tests are given below.



The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.
倒着走一遍,边走边判断,要不走包围当前两点路线的矩形的长与宽之和,要不走当前路,打擂台比小的值,判断是否放礼物。水~
#include<iostream>
#include<string>
#include<cmath>
using namespace std; int n,x,y,lx,ly,ans=1,cnt;
string s; int main()
{
cin>>n>>s;
for(int i=0;i<n;i++)
{
if(s[i]=='R')s[i]='L';
else if(s[i]=='L')s[i]='R';
else if(s[i]=='U')s[i]='D';
else s[i]='U';
} for(int i=n-1;i>=0;i--)
{
cnt++;
int px=x,py=y;
if(s[i]=='L')x--;
else if(s[i]=='R')x++;
else if(s[i]=='D')y--;
else y++;
//cout<<x<<" "<<y<<endl;
if(abs(lx-x)+abs(ly-y)<cnt)
{
ans++;
cnt=1;
lx=px;
ly=py;
//cout<<px<<" "<<py<<endl;
}
}
cout<<ans<<endl;
return 0;
}
CF 752C. Santa Claus and Robot的更多相关文章
- Codeforces 752C - Santa Claus and Robot - [简单思维题]
题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...
- 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 ...
- C. Santa Claus and Robot 思考题
http://codeforces.com/contest/752/problem/C 这题的意思其实就是叫你固定x个点,使得按顺序走这x个点后,产生的轨迹是给定的序列. 对于有若干条最短路径走到第i ...
- CodeForces 748C Santa Claus and Robot (思维)
题意:给定一个机器人的行走路线,求最少的点能使得机器人可以走这样的路线. 析:每次行走,记录一个方向向量,每次只有是相反方向时,才会增加一个点,最后再加上最后一个点即可. 代码如下: #pragma ...
- codeforces Round #389(Div.2)C Santa Claus and Robot(思维题)
题目链接:http://codeforces.com/contest/752/problem/C 题意:给出一系列机器人的行动方向(机器人会走任意一条最短路径),问最少标记几个点能让机器人按这个 路径 ...
- cf 478D.Santa Claus and a Palindrome
原来set,priority_queue也可以映射..涨姿势2333 比较麻烦的应该就是判断自身回文的串是选2个还是选一个吧. #include<bits/stdc++.h> #defin ...
- CodeForces - 748C Santa Claus and Robot
题意:机器人在网格线上行走,从p1点开始,沿最短路径到p2,再沿最短路径到p3,依此类推.在此过程中留下了行走的运动轨迹,由“RLDU”表示.问若只给出运动轨迹,求最少的pi点的个数. 分析:pi到p ...
- 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 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 ...
随机推荐
- ionic中将service中异步返回的数据赋值给controller的$scope
1.service中异步获取数据实例 angular.module('starter.services', []) .factory('Chats', function($http,$q) {//定义 ...
- php 依赖注入容器
原文: http://blog.csdn.net/realghost/article/details/35212285 https://my.oschina.net/cxz001/blog/53316 ...
- Unity3d之协程自实现测试
using UnityEngine; using System.Collections; public class TestStartCoroutine : MonoBehaviour { IEnum ...
- 微信小程序下拉刷新和上拉加载
小程序知识点二 1.上拉加载和下拉刷新 Wxml文件 <scroll-view scroll-top="{{scrollTop}}" scroll-y="true& ...
- javascript DOM(2) 一个网页上切换显示不同的图片或文本
摘自: javascript DOM 编程艺术 1. 在一个网页上切换显示不同的图片 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- Linux ALSA声卡驱动之二:声卡的创建
1. struct snd_card 1.1. snd_card是什么 snd_card可以说是整个ALSA音频驱动最顶层的一个结构,整个声卡的软件逻辑结构开始于该结构,几乎所有与声音相关的逻辑设备都 ...
- JUCE_FIFO实现分析
JUCE_FIFO代码简单剖析,此处的剖析就是给自己留一个方便查询的内容:不做太多内容的要求,只做简单的记录:本文以初学者的角度来些,大牛请飘过O(∩_∩)O FIFO的功能在博客的其他帖子里面已经有 ...
- android studio 更新失败解决办法
在andriod studio目录下找到studio.exe.vmoptions这个文件,用记事本打开,在后面加上 -Djava.net.preferIPv4Stack=true -Didea.upd ...
- 3)Javascript设计模式:Observer模式
Observer模式 var Observer = (function() { var instance = null; function Observe() { this.events = {} } ...
- EF的 JOIN操作
ef join 用法 var customers = DB.Customer.Join(DB.Commission, cst => cst.CommissionId, ...