• codeforces 637D D. Running with Obstacles(dp,水题,贪心)


    题目链接:

    D. Running with Obstacles

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A sportsman starts from point xstart = 0 and runs to point with coordinate xfinish = m (on a straight line). Also, the sportsman can jump — to jump, he should first take a run of length of not less than s meters (in this case for these s meters his path should have no obstacles), and after that he can jump over a length of not more than d meters. Running and jumping is permitted only in the direction from left to right. He can start andfinish a jump only at the points with integer coordinates in which there are no obstacles. To overcome some obstacle, it is necessary to land at a point which is strictly to the right of this obstacle.

    On the way of an athlete are n obstacles at coordinates x1, x2, ..., xn. He cannot go over the obstacles, he can only jump over them. Your task is to determine whether the athlete will be able to get to the finish point.

    Input

    The first line of the input containsd four integers nms and d (1 ≤ n ≤ 200 000, 2 ≤ m ≤ 109, 1 ≤ s, d ≤ 109) — the number of obstacles on the runner's way, the coordinate of the finishing point, the length of running before the jump and the maximum length of the jump, correspondingly.

    The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ m - 1) — the coordinates of the obstacles. It is guaranteed that the starting and finishing point have no obstacles, also no point can have more than one obstacle, The coordinates of the obstacles are given in an arbitrary order.

    Output

    If the runner cannot reach the finishing point, print in the first line of the output "IMPOSSIBLE" (without the quotes).

    If the athlete can get from start to finish, print any way to do this in the following format:

    • print a line of form "RUN X>" (where "X" should be a positive integer), if the athlete should run for "X" more meters;
    • print a line of form "JUMP Y" (where "Y" should be a positive integer), if the sportsman starts a jump and should remain in air for "Y" more meters.

    All commands "RUN" and "JUMP" should strictly alternate, starting with "RUN", besides, they should be printed chronologically. It is not allowed to jump over the finishing point but it is allowed to land there after a jump. The athlete should stop as soon as he reaches finish.

    Examples
    input
    3 10 1 3
    3 4 7
    output
    RUN 2
    JUMP 3
    RUN 1
    JUMP 2
    RUN 2
    input
    2 9 2 3
    6 4
    output
    IMPOSSIBLE

    题意:n个障碍,坐标分别是啊a[i],终点是m,助跑s米才能最远跳d米,问最后是否能到达m,以及是怎样到达的;
    思路:把障碍都标记看是否与上一障碍之间的距离是否够s米,不够的话就将这个与上一障碍合并,我采用一个数组标记的方法标记合并的最前面的障碍位置,再从后往前找,看跑和跳是否满足条件,是的话用ans数组记录下来,不行的话就return 0 结束;
    AC代码:
    #include <bits/stdc++.h>
    using namespace std;
    const int N=2e5+4;
    int a[N],dp[N],ans[2*N];
    int n,m,s,d;
    int main()
    {
        scanf("%d%d%d%d",&n,&m,&s,&d);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a+1,a+n+1);
        if(a[1]-1>=s)dp[1]=1;
        else dp[1]=0;
        for(int i=2;i<=n;i++)
        {
            if(a[i]-a[i-1]-2>=s)dp[i]=i;
            else dp[i]=dp[i-1];
        }
        /*for(int i=1;i<=n;i++)
        {
            cout<<dp[i]<<"#"<<endl;
        }*/
        int pos=0,flag=1,cnt=1;
        stack<int>st;
        for(int i=n;i>0; )
        {
            if(dp[i]==i)
            {
                st.push(a[i]+1);
                st.push(a[i]-1);
                i--;
            }
            else
            {
                st.push(a[i]+1);
                i=dp[i];
                st.push(a[i]-1);
                i--;
            }
        }
        while(!st.empty())
        {
           int x=st.top();
           st.pop();
           if(flag)
           {
               if(x-pos>=s)
               {
                   ans[cnt++]=x-pos;
                   pos=x;
               }
               else
               {
                   cout<<"IMPOSSIBLE"<<"
    ";
                   return 0;
               }
               flag=0;
           }
           else
           {
               if(x-pos<=d)
               {
                   ans[cnt++]=x-pos;
                   pos=x;
               }
               else
               {
                   cout<<"IMPOSSIBLE"<<"
    ";
                   return 0;
               }
               flag=1;
           }
        }
        if(a[n]!=m-1)
        {
            ans[cnt++]=m-a[n]-1;
        }
        for(int i=1;i<cnt;i++)
        {
            if(i%2)printf("RUN %d
    ",ans[i]);
            else printf("JUMP %d
    ",ans[i]);
        }
        return 0;
    }
  • 相关阅读:
    同一位置(同一个ImageView)显示不同的图片levellist (转)
    LinearLayout android:layout_weight的理解
    给C盘减减肥,让你电脑飞一般速度!
    (Android实战)界面设计注意事项
    利用sender的Parent获取GridView中的当前行(转载)
    用SQL查看字符串ASCII码
    自己写DataPage分页
    连接长宽高的UDF
    把Dictionary绑定到combox
    trackbar和processbar功能合并
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5281116.html
Copyright © 2020-2023  润新知