Codeforces Round #389 (Div. 2) C. Santa Claus and Robot 贪心+最短路
来源:互联网 发布:中银淘宝校园卡过期 编辑:程序博客网 时间:2024/05/16 16:24
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m pointsp1, 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.
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.
Source
Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3)
My Solution
题意:给定一个路径,让找出尽可能少的关键点,没相邻的2个关键点的路径必须是最短路径
贪心+最短路
这是一个很有趣的题 Y ^_^ Y
ans = 0;
(px,py)表示最近出现的一个关键点坐标,cnt表示从最近的那个关键点到当前点所走的路程, (x, y)表示当前所在的点的坐标。
然后每步更新cnt及(x, y)后 判断abs(px - x) + abs(py - y) == cnt,如果成立则从最近的一个关键点到当前点一直在根据最短路走,直到每次出现不合理的情况,则上一个点更新为新的最近的关键点,并且ans++,扫一遍,最后一个点必然是关键点,故额外 ans++ 把最后一个点加上。
复杂度 O(n)
#include <iostream>#include <cstdio>#include <string>#include <cmath>using namespace std;typedef long long LL;const int maxn = 1e6 + 8;string s;int main(){ #ifdef LOCAL freopen("c.txt", "r", stdin); //freopen("c.out", "w", stdout); int T = 8; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); int n, px = 0, py = 0, x = 0, y = 0, rx = 0, ry = 0, ans = 0, cnt = 0; cin >> n; cin >> s; bool flag = false; for(int i = 0; i < n; i++){ rx = x, ry = y; if(s[i] == 'L'){ x--; } else if(s[i] == 'R'){ x++; } else if(s[i] == 'U'){ y++; } else if(s[i] == 'D'){ y--; } cnt++; if(abs(px - x) + abs(py - y) == cnt){ //if(!flag){ans++; flag = true; } } else{ ans++; //if(flag) ans++; cnt = 1; px = rx; py = ry; //cout << px << ' ' << py << endl; } } ans++; cout << ans << endl; #ifdef LOCAL cout << endl; } #endif // LOCAL return 0;}
Thank you!
------from ProLights
- Codeforces Round #389 (Div. 2) C. Santa Claus and Robot 贪心+最短路
- Codeforces Round #389(Div.2)C Santa Claus and Robot【思维】
- 【Codeforces Round #389】Codeforces 752C Santa Claus and Robot
- Codeforces Round #389 C. Santa Claus and Robot
- Codeforces Round #389 (Div. 2) E. Santa Claus and Tangerines 二分+贪心+记忆化搜索
- Codeforces Round #389 (Div. 2) D. Santa Claus and a Palindrome 回文串+贪心+数据结构、字符串
- Codeforces Round #389 (Div. 2):Santa Claus and a Palindrome
- 【codeforces 752C】Santa Claus and Robot
- 748 C. Santa Claus and Robot codeforces
- 【codeforces 752C】Santa Claus and Robot
- Codeforces Round #389 (Div. 2) B. Santa Claus 贪心+字符串处理、易错
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round #389 (Div. 2)A. Santa Claus and a Place in a Class(水题)
- Codeforces Round #389 (Div. 2)B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round #389 (Div. 2):Santa Claus and Tangerines(二分+DP)
- Codeforces 752C Santa Claus and Robot 思维
- 【Codeforces 748 C Santa Claus and Robot】+ 思维
- 748C Santa Claus and Robot
- 在云服务器上部署项目(下)
- Java JSON library哪家强?
- mysql5.6数据库同步,单向双向同步问题
- 冒个泡
- struct中打开网址出现新的窗口
- Codeforces Round #389 (Div. 2) C. Santa Claus and Robot 贪心+最短路
- Java Swing TextField对用户输入数据进行格式化处理
- TE0703无法下程序
- 【PAT】1002.A+B for Polynomials
- linux:vim异常中断处理办法
- scrapy爬虫第一阶段——爬取多级url
- echarts timeline 设置多个点,却只显示一个
- nmap的使用(待续)
- theano-scan