Codeforces Round #389 C. Santa Claus and Robot
来源:互联网 发布:discovery纪录片知乎 编辑:程序博客网 时间:2024/06/04 19:13
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 fromp0 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 reachesp1, it'll move top2, again, choosing one of the shortest ways, then top3, 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 ofn letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled thek-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.
4RURD
2
6RRULDD
2
26RRRULURURUULULLLDLDDRDRDLD
7
3RLL
2
4LRLR
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.
题意:
题目很有意思,记录下。
已知圣诞老人送了x个礼物,且老人走向每个礼物的路径都是最短路径。
现在只知道送礼物的路径,求礼物的最小数量可以是多少。
思路:
开始的时候想复杂了。
其实只要对出现过的点做标记,找到一对相反的点就能判断出有一个礼物。
之后要把之前的标记清空。
注意答案要加一,因为最后一个点一定是存在礼物的。
代码:
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int maxn = 200005;char data[maxn];int flag[5];int main(){int n;scanf("%d",&n);int i,j;scanf("%s",data+1);int ans =0;for(i=1;i<=n;i++){if(data[i]=='D'){if(flag[2]==1){ans++;memset(flag,0,sizeof(flag));}flag[1]=1;}else if(data[i]=='U'){if(flag[1]==1){ans++;memset(flag,0,sizeof(flag));}flag[2]=1;}else if(data[i]=='R'){if(flag[4]==1){ans++;memset(flag,0,sizeof(flag));}flag[3]=1;}else{if(flag[3]==1){ans++;memset(flag,0,sizeof(flag));}flag[4]=1;}}printf("%d\n",ans+1);return 0;}
- 【Codeforces Round #389】Codeforces 752C Santa Claus and Robot
- Codeforces Round #389 C. Santa Claus and Robot
- 【codeforces 752C】Santa Claus and Robot
- 748 C. Santa Claus and Robot codeforces
- 【codeforces 752C】Santa Claus and Robot
- Codeforces Round #389(Div.2)C Santa Claus and Robot【思维】
- Codeforces Round #389 (Div. 2) C. Santa Claus and Robot 贪心+最短路
- Codeforces 752C Santa Claus and Robot 思维
- 【Codeforces 748 C Santa Claus and Robot】+ 思维
- 748C Santa Claus and Robot
- Codeforces Round #389 (Div. 2):Santa Claus and a Palindrome
- codeforces748c-Santa Claus and Robot
- 【Codeforces Round #389】Codeforces 752A Santa Claus and a Place in a Class
- 【Codeforces Round #389】Codeforces 752B Santa Claus and Keyboard Check
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法一】
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法二】
- Codeforces Round #389 Technocup 2017 E. Santa Claus and Tangerines(二分+DP)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- 系统调用
- Scala中的Map使用例子
- HDU——校赛1007
- 143. Reorder List leetcode list
- Qt5.6 自写封装MessageBox类
- Codeforces Round #389 C. Santa Claus and Robot
- [Leetcode] Container With Most Water
- 最长公共子串
- Python数据分析
- Error: connect ECONNREFUSED 127.0.0.1:8087
- 用hbuilder打包网站项目为apk或ios应用
- a
- Hibernate-多关系时对应的多个关联对象Set集合进行排序
- Java入门第二季