zoukankan      html  css  js  c++  java
  • Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017

    Description

    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.

    题意:不明白

    解法:每运动到一个点,如果之前有反向就+1,清空标记,然后记录当前方向,依次进行下去

    #include<bits/stdc++.h>
    using namespace std;
    int n;
    string s;
    map<char,int>q;
    int main()
    {
        int flag=0;
        cin>>n;
        cin>>s;
        int num=1;
        for(int i=n-1; i>=0; i--)
        {
            flag=0;
           // q.clear();
            if(s[i]=='R')
            {
                if(q['L']==1)
                {
                    flag=1;
                   // num++;
                   // q['L']=0;
                   // continue;
                }
            }
            if(s[i]=='L')
            {
                if(q['R']==1)
                {
                    flag=1;
                  //  num++;
                  //  q['R']=0;
                  //  continue;
                }
            }
            if(s[i]=='D')
            {
                if(q['U']==1)
                {
                    flag=1;
                   // num++;
                   // q['U']=0;
                  //  continue;
                }
            }
            if(s[i]=='U')
            {
                if(q['D']==1)
                {
                    flag=1;
                   // num++;
                   // q['D']=0;
                   // continue;
                }
            }
            if(flag==1)
            {
                num++;
                q.clear();
            }
            q[s[i]]=1;
        }
        cout<<num<<endl;
        return 0;
    }
  • 相关阅读:
    .NET System.Web.HttpContext.Current.Request报索引超出数组界限。
    Jq将字符串复制粘贴到剪贴板
    设置VS以管理员身份运行
    http遇到的那些坑,iis上传文件报413错误 asp.net MVC
    百度地图api使用,简单搜索+经纬度定位+自定义消息窗口
    常见的sql server 链接问题------持续更新
    解决电脑不能访问局域网共享,局域网共享中找不到。
    博文图片挂了临时解决办法
    博客声明
    06. redis cluster
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6220904.html
Copyright © 2011-2022 走看看