time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

Input
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.

Output
The only line of input should contain the minimum possible length of the sequence.

Examples
input
4
RURD
output
2
input
6
RRULDD
output
2
input
26
RRRULURURUULULLLDLDDRDRDLD
output
7
input
3
RLL
output
2
input
4
LRLR
output
4
Note
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.

【题目链接】:http://codeforces.com/contest/752/problem/C

【题解】

记录上一个放节点的位置在哪里(一开始为0,0)
然后按照所给的序列向左或向右走;
如果走到一个新的格子之后,这个格子到上一个放节点的距离与未走之前的距离相比变小了;则需要在未走之前的节点那个地方放一个节点;
最后一个位置一定要放一个节点;
按照这个规律搞就可以了;
细心一点.

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)typedef pair<int,int> pii;
typedef pair<LL,LL> pll;const int MAXN = 2e5+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);int nx,ny,n,px,py,ans = 0;
map <pii,int> dic;
char s[MAXN];int dis(int x0,int y0,int x1,int y1)
{return abs(x0-x1)+abs(y0-y1);
}int main()
{//freopen("F:\\rush.txt","r",stdin);cin >> n;scanf("%s",s+1);int tdis = 0;rep1(i,1,n){int tx = nx,ty = ny;if (s[i]=='U')tx++;if (s[i]=='D')tx--;if (s[i]=='L')ty--;if (s[i]=='R')ty++;int tdis1 = dis(tx,ty,px,py);if (tdis1>tdis && i!=n){nx = tx;ny=ty;tdis = dis(nx,ny,px,py);}else{if (i==n && tdis1<tdis){ans+=2;}elseans++;px = nx,py = ny;nx = tx,ny = ty;tdis = dis(nx,ny,px,py);}}cout << ans << endl;return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/7626768.html

【codeforces 752C】Santa Claus and Robot相关推荐

  1. 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 ...

  2. 【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)

    题干: A string t is called an anagram of the string s, if it is possible to rearrange letters in t so ...

  3. 【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)

    题干: Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Ri ...

  4. 【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)

    题干: 题目大意: 题意是在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围.现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭.否 ...

  5. 「一题多解」【CodeForces 85D】Sum of Medians(线段树 / 分块)

    题目链接 [CodeForces 85D]Sum of Medians 题目大意 实现一个setsetset,支持插入,删除,求∑a5k+3∑a5k+3\sum a_{5k+3}.注意,setsets ...

  6. 【CodeForces 997C】Sky Full of Stars(组合计数)

    题目链接:[CodeForces 997C]Sky Full of Stars 官方题解:Codeforces Round #493 - Editorial 题目大意:有一个n×nn×nn\times ...

  7. 【Educational Codeforces Round 6A】【水题】Professor GukiZ's Robot 曼哈顿距离

    A. Professor GukiZ's Robot time limit per test 0.5 seconds memory limit per test 256 megabytes input ...

  8. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

  9. 【codeforces 508B】Anton and currency you all know

    [题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...

最新文章

  1. iOS逆向之iOSOpenDev
  2. SAP CRM settype COMM_PROD_VAR
  3. javaScript基础讲义第四天(1)
  4. javascript数组浅谈1
  5. eclipse 版本理解
  6. android apk 反编译工具,安卓apk反编译神器
  7. mysql 多个字段拼接 concat
  8. Delphi2010 Dll 函数列表查看
  9. 【Grafana】通过阿里云日志服务监控Nginx访问日志显示统计信息
  10. Mac Android Studio连接MuMu模拟器
  11. 八爪鱼采集器32位_飞智八爪鱼2游戏手柄评测
  12. centos添加互信
  13. mysql查询数据库剩余空间大小_MySQL中查询所有数据库占用磁盘空间大小
  14. 广告图片自动轮播控件
  15. 智库献策大数据时代食品安全
  16. 计算机论文致谢词范文500字,论文致谢词范文
  17. Poi实现Excel导出
  18. 基因表达微阵列数据分类的多目标启发式算法
  19. 2023 人工智能在线聊天机器人网页HTML源码
  20. 京东校招2017届应届生java研发岗,面试一,感想

热门文章

  1. SSL和HTTPS两者有什么区分?
  2. 单引号,双引号,javascript,HTML,转义字符
  3. 要不要我教你如何提取出视频的伴奏
  4. Vue · Table:手写横向竖向滚动表格,分页、条数设置
  5. 华为支付购买Token的verify接口报错wrong application
  6. i9 12900k和r7 5800x选哪个好
  7. 【云原生 · Kubernetes】Taint和Toleration(污点和容忍)
  8. android vold 分析,vold流程分析
  9. Python爬虫:抓取手机APP数据
  10. Python字典结构