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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章