Neutralzz的博客

我有自己的梦想和追求!

Codeforces 637D Running with Obstacles (贪心)

解析:查看每对相邻的障碍,如果距离大于s,那么可以落地并在下一个障碍前起跳。对每次落地和起跳加以判断即可。

[code]:

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
typedef pair<int,int> P;
const int maxn = 2e5+5;

int n,m,s,d,a[maxn],top;
P ans[4*maxn];

void sol(){
    int i,j,run,jump,len;
    run = -1;jump = -1;
    for(i = 1;i <= n;i++){
        if(s+2<=a[i]-a[i-1]){
            if(jump!=-1){
                if(a[i-1]+1-jump<=d&&(top>0&&ans[top-1].first==0&&ans[top-1].second>=s))ans[top++] = P(1,a[i-1]+1-jump);
                else{
                    puts("IMPOSSIBLE");
                    return;
                }
            }
            ans[top++] = P(0,a[i]-a[i-1]-2);
            jump = a[i]-1;
        }
    }
    if(a[n]+1-jump<=d&&(top>0&&ans[top-1].first==0&&ans[top-1].second>=s)) ans[top++] = P(1,a[n]+1-jump);
    else{
        puts("IMPOSSIBLE");
        return;
    }
    if(m!=a[n]+1){
        ans[top++] = P(0,m-a[n]-1);
    }
    for(i = 0;i < top;i++){
        if(ans[i].first) printf("JUMP %d\n",ans[i].second);
        else printf("RUN %d\n",ans[i].second);
    }
}

int main(){
    int i,j;
    scanf("%d%d%d%d",&n,&m,&s,&d);
    for(i = 1;i <= n;i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    a[0] = -1;
    a[n+1] = m+1;
    sol();

    return 0;
}



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26572969/article/details/51546433
个人分类: Codeforces 贪心
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Codeforces 637D Running with Obstacles (贪心)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭