zoukankan      html  css  js  c++  java
  • 【codeforces 752C】Santa Claus and Robot

    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;
                }
                else
                    ans++;
                px = nx,py = ny;
                nx = tx,ny = ty;
                tdis = dis(nx,ny,px,py);
            }
        }
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    Android程序反编译的方法(转)
    VMware的Easy Install安装
    Oracle ORA12514:TNS:监听程序当前无法识别连接描述符中请求的服务
    Java Eclipse 如何导入外部Jar包
    Oracle IMP00010: 不是有效的导出文件,标题验证失败
    怎样取消Windows 2003 server 意外关机提示
    没有文件扩展“.vbs”的脚本引擎的解决方案
    Oracle init.ora常用配置详解
    eclipse断点调试 出现Source not found
    VMware Workstation 8
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626768.html
Copyright © 2011-2022 走看看