Search Here

Friday, September 4, 2015

UVa 11850 - ( Alaska Solution )[C++]

=>Try Yourself First. For help Scroll Down .

Oh . Its long I have posted my last Post . Actually I was little busy . Whatever today I'll try to help you on a interesting Data Structure Problem.

Clearance : This problem is interesting , easier but a little Bit Tricky . All you have to do is to 
1. Sort the list in Ascending that are given ( As you have to Start from Starting Position that are given in the Problem ) . 
2. And then you have to check whether the Difference between two Charging Station is Greater than 200 or Not ( As Charging once Can go 200 miles ) . As you have to come back at your Starting position from Alaska You have to check reversely that you can Come Back or Not . 
3. To do so, You just need to check the last Station's Distance from 1422 ( As the last destination is 1422 . Don't misunderstood this line like me . At point 1422 there is no Station unless it given in the input . ). Just add 200 with last Station and Subtract 1422 from it Again Subtract this result 1422 . If it is greater than last input then " IMPOSSIBLE ".

Hope You Got It.
**************************************___________________________________________**************************************
Code :

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    vector<int>mv, speed;
    while(scanf("%d",&n)==1 && n)
    {
        mv.clear();
        for(int i=0; i<n; i++)
        {
            int x;
            scanf("%d",&x);
            mv.push_back(x);
        }
        mv.push_back(1422);
        sort(mv.begin(), mv.end());
        bool flag = true;
        for(int i=0; i<n; i++)
        {
            //cout<<mv[i]<<' '<<mv[i+1]<<endl;
            if(mv[i+1]>mv[i]+200)
            {
                flag = false;
                break;
            }
        }
        if(flag)
        {
            int calc = 1422 - (mv[n-1]+200-1422);
            //cout<<calc<<endl;
            if(mv[n-1]<calc)
            {
                flag = false;
            }
        }

        if(!flag)
            printf("IMPOSSIBLE\n");
        else
            printf("POSSIBLE\n");
    }
    return 0;
}

To Check Correctness Of Your Code Check This Critical Inputs :
==============
CRITICAL INPUT
==============
2
0
900

8
1400
1200
1000
800
600
400
200
0

8
1300
1200
1000
800
600
400
200
0

8
1401
1200
1000
800
600
400
200
0

9
1421
1400
1200
1000
800
600
400
200
0

8
1399
1200
1000
800
600
400
200
0

8
0
200
400
600
800
900
1100
1300

8
400
1200
800
0
600
1400
200
1000

7
600
1400
400
1200
200
1000
0
0
===============
ACCEPTED OUTPUT
===============
IMPOSSIBLE
POSSIBLE
IMPOSSIBLE
IMPOSSIBLE
POSSIBLE
POSSIBLE
IMPOSSIBLE
POSSIBLE
IMPOSSIBLE



=>Question or Need Help ?? Leave a Comment .

No comments:

Post a Comment