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 ...
随机推荐
- javascript 中 arguments.callee属性
javascript 中 arguments.callee属性 可以在函数内部,指向的是这个函数(或者叫做“类”)本身. 相当于PHP 中的 self 关键字. The arguments.calle ...
- JavaScript------for-in的使用方法
1.在数组中使用 var str = ["a", "b", "c"]; for (var item in str) { //alert(it ...
- Apache2 MPM 模式了解
一.MPM MPM(Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server)是Apa ...
- sublime text 调出结果输出框
sublime是一个非常好用的代码编辑器,同时可以build program 但是在执行代码的过程中,如果进行了查找等操作,下面原来显示输出框的地方被查找界面替代,而程序结果输出框就会"消失 ...
- FZU 1061 矩阵连乘
用栈来算一算就可以了. #include<iostream> #include<algorithm> #include<cstdio> #include<cs ...
- WCF中传递对象的成员变量行不通?
今早通过WCF服务添加对象到数据库,有一个变量始终没有传过来 定义: public bool isLogin; 修改成 private bool _isLogin; public bool isLog ...
- PHP实反向代理-收藏
需求 现在有些后辍的域名不支持备案,这个时候需要用免备案主机或空间做个反向代理,这样可实现内容存放在国内主机统一管理 实现 用 php-dynamic-mirror 可实现,并在头部进行域名转换,可实 ...
- ECshop中的session机制理解
ECshop中的session机制理解 在网上找了发现都是来之一人之手,也没有用自己的话去解释,这里我就抛砖引玉,发表一下自己的意见,还希望能得到各界人士的指导批评! 此session机制不需 ...
- spring.net的基本搭建
这几天在学C#,感觉还是需要一个控制反转的框架,正好Spirng也有.net版的,看着API搭建一个 大致目录是这样的,我们在APP.CONFIG里面配好xml文件的地址,这个APP.CONFIG就相 ...
- Java解析JSON文件的方法 (二)
assets文件夹资源的访问 assets文件夹里面的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件. 1. 先在Activity里面调用g ...