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 ...
随机推荐
- static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store); 的作用
在 老罗的android例程里面有 static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store); /*读取设 ...
- css(一)-- 概述以及引入方式
概述 层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.CSS不仅可 ...
- FZU 2099 魔法阵
手算. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> u ...
- 【转】Linux Shell脚本面试25问
Q:1 Shell脚本是什么.它是必需的吗? 答:一个Shell脚本是一个文本文件,包含一个或多个命令.作为系统管理员,我们经常需要使用多个命令来完成一项任务,我们可以添加这些所有命令在一个文本文件( ...
- C++中类的大小计算方法总结《网络+总结》
C++中类的成员函数,静态成员是不占类的大小的.类的大小等于基类的大小+子类个non-static成员变量的大小再+非虚基类大小,如果有多态性还要考虑vptr(可能不止一个)大小,这里成员变量是会被字 ...
- group by 汇总
group by 的意思为分组汇总.使用了group by 后,要求Select出的结果字段都是可汇总的,否则就会出错. 比如,有:{学号,姓名,性别,年龄,成绩}字段 这样写:SELECT 学号,姓 ...
- centos 软件库安装
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the mo ...
- UVa 706 & ZOJ 1146 LC-Display
题目大意:给你一个数字n和字体大小s,输出数字的液晶显示.直接模拟,代码如下: #include <stdio.h> void draw(int n,int s,int row) { in ...
- grunt live reload 配置记录
1.npm install --save-dev grunt-contrib-watch 安装 watch 2.安装chrome livereload 插件 3.配置Gruntfile.js wat ...
- Html 和 Css 的杂乱总结
1. input 中可以设置 maxLength 属性,控制输入的文字数量,中英文字节数一样,但是没有验证兼容性 2.客户端中的页面禁止右键,复制等 <body scroll="no& ...