UVA 11093 Just Finish it Up

Source: Internet
Author: User

Original question:
Along a circular track, there is N gas stations, which is numbered clockwise from 1 up to N. at
Station I, there is P i gallons of petrol available. To race from station I to its clockwise neighbor one
Need q I gallons of petrol. Consider a race where a car would start the race with an empty fuel tank. Your
Task is to find whether the car can complete the race from any of the stations or not. If it can then
Mention the smallest possible station I from which the lap can be completed.
Input
First line of the input contains one integer T the number of test cases. Each test case would start with a
Line containing one integer N, which denotes the number of gas stations. In the next few lines contain
2∗n integers. First N integers denote the values of P i s (petrol available at station i), subsequent N
integers denote the value of Q I s (amount of patrol needed to go to the next station in the clockwise
direction).
Output
For each test case, output the case number in the format ' Case c: ', where c is the case number starting
Form 1. Then display whether it was possible to complete a lap by a car with an empty tank or not. If it
Is isn't possible to complete the lap then display "not possible". If possible, then display "possible
From Station X ", where X was the first possible station from which the car can complete the lap.
Constraints
• T < 25
• N < 100001
Sample Input
2
5
1 1 1) 1 1
1 1 2) 1 1
7
1 1 1 10 1 1 1
2 2 2 2 2 2 2
Sample Output
Case 1:not Possible
Case 2:possible from Station 4


English:
There is a car in a roundabout run, there are N gas stations, you can choose a gas station as a starting point, the request can run a circle back to the beginning. Suppose the tank size of the car is infinitely large, allowing you to choose a starting point with the smallest number and satisfying conditions.
The first given n number indicates how much oil the N gas stations have, and the next row is given n number, indicating how much oil is consumed from the current gas station to the next. Note that the subject only considers the single direction of the past. -_-



Code:


#include <bits / stdc ++. h>
using namespace std;

int t, n, T = 1;
int a [100001];
int main ()
{
    ios :: sync_with_stdio (false);
    cin >> t;
    while (t--)
    {
        cin >> n;
        memset (a, 0, sizeof (a));
        int tmp;
        for (int i = 1; i <= n; i ++)
        {
            cin >> a [i];
        }

        for (int i = 1; i <= n; i ++)
        {
            cin >> tmp;
            a [i]-= tmp;
        }


        tmp = 0;
        for (int i = 1; i <= n; i ++) // If the sum is less than 0, then it is impossible to succeed
            tmp + = a [i];
        cout << "Case" << T ++ << ":";


        if (n == 1) // only one gas station
        {
            if (a [1]> = 0)
            {
                cout << "Possible from station" << 1 << endl;
            }
            else
            {
                cout << "Not possible" << endl;
            }
            continue;
        }
        if (tmp <0)
        {
            cout << "Not possible" << endl;
            continue;
        }
        int ind, flag = 1, flag2 = 1; // s indicates the selection of the starting point, ind enumeration goes backwards from the current gas station
        tmp = 0; // tmp is the cumulative fuel volume when walking backward
        int s = 1;
        ind = s;
        while (a [s] <0) // The starting point selected must be the distance from the year when the refueling amount is greater than or equal to the next gas station
        {
            s ++;
            ind = s;
        }
        while (true)
        {
            if (ind> n)
            {
                ind = 1;

                flag2 = 0;
            }
            tmp + = a [ind];
            while (tmp <0)
            {
                s ++;
                if (s == ind)
                {
                    while (a [s] <0)
                    {
                        s ++;
                        ind = s;
                    }
                    tmp = a [s];
                    break;
                }
                if (s == n)
                {
                    flag = 0;
                    break;
                }
                tmp-= a [s-1];
            }

            if (flag == 0 || (ind + 1)% n == s && tmp> = 0)
                break;
            ind ++;
            if (ind == s && flag2 == 0)
                break;
        }
        cout << "Possible from station" << s << endl;



    }
    return 0;
} 


Answer:



Examples of purple Books



Note that this problem can only go in one direction, not the reverse circle.



This topic can use the knowledge of the sliding window mentioned earlier, first of all A[i] value into the oil at the petrol filling station minus the amount of oil from I to i+1 gas stations,
Recorded in A[i].



If the problem has a solution, then the cumulative sum of a[i] must be greater than or equal to 0



First set the current starting point S, and then go forward from S, with the IND value to traverse the remaining oil after S a[i], and the TMP variable accumulation a[i], if there is tmp<0, then must be from the S point to the IND point can not go through, then the starting point to move forward, while changing the TMP value, The method of change is tmp-=a[s], because the value of the oil in this window [S,ind] [s, i n d] [s,ind] is TMP, and the S value on the left side of the window is now shifted forward, so the left interval of the window will be reduced by a[s].



The result of the final output can be:



The practice of the book is a lot simpler, the main idea is: Consider the number I dating station, to determine if there is a solution, if there is output, if not, explain the way to meet gas station p, from p to p+1 no oil. Then, with I+1,I+2,I+3....P as the starting point is not the solution, the reason is that if I can as a starting point, I point of oil minus to i+1 point of consumption value must be greater than or equal to 0 of a number, set to go from I point to I+1 Point after the remaining oil amount of T (t>=0). If there is no oil from I to P, then from I+1 point to P Point, the equivalent of subtracting s from I to P, the remaining amount of oil will be less . So, if I to P if there is no solution, jump directly to p+1 as the starting point to judge.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.