1D Maze

Description

1D people lived in a 1D country. Everything in the country was one-dimensional, and everything was simple and clear: just one axis and two directions — forward and backward. Even a 1D world has problems, though; for instance, finding an exit from a maze. An idea of a 1D maze might seem weird to us, but not to 1D people. Escaping from such a maze is a hard and vital task for them. They solve this task in a following way.
A 1D person chooses a direction: backward (decreasing his coordinate) or forward (increasing it), and then moves in this direction. If he finds an exit, he escapes the maze immediately; if he encounters an obstacle, he reverses his direction and continues walking.
In order to feel the hard life of 1D residents, try to implement a function that will compute a distance a 1D person will walk before finding an exit, based on the initial direction.

Input

The first line contains space-separated integers  n and  x — the number of obstacles and the coordinate of an exit point  (0 ≤  n ≤ 100) . 1D person is located at the origin. The second line contains  n different integers — the coordinates of the obstacles. Each coordinate, including  x, is non-zero and doesn't exceed 1000 in absolute value. No obstacle is located at the exit point. It is guaranteed that 1D person will encounter either obstacle or exit point sooner or later regardless of the initial direction.

Output

Output two space-separated integers — the distance a 1D person should walk before finding an exit if his initial direction is forward or backward, respectively. If he can't find the exit due to the obstacles, output “Impossible”.

Sample Input

input output
3 -2
-10 -4 2
6 2
3 -2
10 -1 2
Impossible

代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <cstdio>
#include <cmath>
#include <string>
#include <stack>
#include <cctype>
using namespace std;

int main()
{

    int n,m,k;
    while(~scanf("%d%d",&n,&m))
    {
        int nn=-1000005;
        int mm=1000005;
        int tt;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&k);
            if(k<0&&k>=nn)
                nn=k;
            if(k>0&&k<=mm)
                mm=k;
        }

        if(m<0)
        {
            if(m<nn)
                printf("Impossible\n");
            else
                printf("%d %d\n",mm*2+abs(m),abs(m));

        }
        if(m>0)
        {
            if(m>mm)
                printf("Impossible\n");
            else
                printf("%d %d\n",abs(m),abs(nn)*2+abs(m));
        }



    }


    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值