多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Codeforces Round #389 (Div. 2) C. Santa Claus and Robot 貪心+最短路

Codeforces Round #389 (Div. 2) C. Santa Claus and Robot 貪心+最短路

來源:程序員人生   發布時間:2017-02-07 09:10:57 閱讀次數:3029次

C. Santa Claus and Robot
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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 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.

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




Source

Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3)


My Solution

題意:給定1個路徑,讓找出盡量少的關鍵點,沒相鄰的2個關鍵點的路徑必須是最短路徑


貪心+最短路

這是1個很有趣的題 Y ^_^ Y

ans = 0;

(px,py)表示最近出現的1個關鍵點坐標,cnt表示從最近的那個關鍵點到當前點所走的路程, (x, y)表示當前所在的點的坐標。 

然后每步更新cnt及(x, y)后 判斷abs(px - x) + abs(py - y) == cnt,如果成立則從最近的1個關鍵點到當前點1直在根據最短路走,直到每次出現不公道的情況,則上1個點更新為新的最近的關鍵點,并且ans++,掃1遍,最后1個點必定是關鍵點,故額外 ans++ 把最后1個點加上。

復雜度 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

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美性猛交黑人 | h免费在线观看 | 日本高清不卡免费 | 亚洲色欲色欲综合网站 | 欧美亚洲一级片 | 亚洲欧美日本国产 | 在线播放精品视频 | 久草视频福利在线观看 | 吃奶添下面大尺度视频 | 日本一区二区精品88 | 久久久www成人免费精品 | 精品亚洲成a人在线播放 | 国产精品亚洲片夜色在线 | 最近更新中文字幕免费版 | 手机在线中文字幕乱码免费 | 2020久久精品亚洲热综合一本 | 亚洲视频福利 | 日本高清一道本 | 在线午夜视频 | 男女那个视频免费 | 久久精品视频一区 | japanese18日本护士 | 国语对白清晰好大好白在线 | 午夜高清免费观看视频 | 国模一区二区三区视频一 | 国产亚洲精品一区二区 | 国产第一页福利 | 欧美日韩亚洲国产一区二区综合 | 中文字幕一区二区三区在线观看 | 亚洲精品成人a | 福利视频播放 | 一区二区三区四区国产精品 | 欧美日韩精品一区二区在线线 | 国产精品日产三级在线观看 | 日韩亚州| 久久久久久国产精品免费 | 狠狠操网站| 成人欧美视频在线看免费 | 亚洲精品一区91 | 亚洲一区二区观看 | 欧美黑人粗暴另类多交 |