CF 637D. Running with Obstacles 贪心

来源:互联网 发布:pic12数据手册 编辑:程序博客网 时间:2024/05/16 05:32



自己A这个题还是花了一些力气,原因还是在于思维上的不足,首先容易想到连续的点可以当作一个区间来处理,

然后幼稚认为跨过一个区间必须从上一个区间后面跨来,结果wa了。


后来发现可以一次性跨过多个区间。


最后得出本题解法:


首先将点排序,两点之间距离太短不够起跳的两点放在一个区间,这样问题转化为跨过若干个区间。


如果能跨过某一个区间,那么一定有办法停在ri+1,就是区间右端点+1的位置,而且这样不会比最优解差。


转化出的区间一个一个顺次跨,这样也不会比最优解差。


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 0002 ≤ m ≤ 1091 ≤ 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 33 4 7
output
RUN 2JUMP 3RUN 1JUMP 2RUN 2
input
2 9 2 36 4
output
IMPOSSIBLE



#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>using namespace std;typedef long long ll;const int INF =0x3f3f3f3f;const int maxn= 200000   ;int n,ed,runD,jumpD;int a[maxn+20];struct Seg{    int le,ri;}b[maxn+10];void pre(){   int i=1;   int cnt=0;   while(i<=n)   {       b[++cnt].le=a[i];       while(i+1<=n&&a[i+1]-(a[i]+1)<=runD)       {           i++;       }       b[cnt].ri=a[i];       i++;   }   n=cnt;}bool work(){    b[0].ri=-1;    for(int i=1;i<=n;i++)    {        if(runD>=b[i].le-(b[i-1].ri+1)|| b[i].ri-b[i].le+2>jumpD )  return false;    }    return true;}void print(int x,int des){    if(!x)    {         printf("RUN %d\n",b[1].le-1);         return;    }    print(x-1,b[x].le-1 );    printf("JUMP %d\n",b[x].ri+1-b[x].le+1  );    if(b[x].ri+1!=des)    {        printf("RUN %d\n",des-b[x].ri-1);    }}int main(){    while(~scanf("%d%d%d%d",&n,&ed,&runD,&jumpD))    {        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        sort(a+1,a+1+n);        pre();        if(!work())        {            puts("IMPOSSIBLE");            continue;        }        print(n,ed);    }   return 0;}


首先要简化问题

0 0
原创粉丝点击