uva 11093 Just Finish it up

來源:互聯網
上載者:User

原題:
Along a circular track, there are N gas stations, which are numbered clockwise from 1 up to N. At
station i, there are 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 will 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 will 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 is possible to complete a lap by a car with an empty tank or not. If it
is not possible to complete the lap then display “Not possible”. If possible, then display “Possible
from station X”, where X is 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

中文:
有一輛車在一個個環形跑道跑,上面有n個加油站,你可以任意選一個加油站作為起點,要求能跑一圈回到起點。假設車的油箱大小無限大,讓你選擇一個編號最小的、而且滿足條件的起點。
首先給定n個數表示n個加油站有多少油,下一行給定n個數,表示從當前加油站到下一個加油站消耗多少油。 注意,本題只考慮從前往後的單方向。-_-

代碼:

#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++)//如果總和小於0,那麼不可能成功            tmp += a[i];        cout<<"Case "<<T++<<": ";        if(n==1)//只有一個加油站的情況        {            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表示選擇起點,ind枚舉從當前加油站往後走        tmp = 0;    //tmp為累加往後走時的油量        int s = 1;        ind = s;        while (a[s]<0)//選取的起點一定是有當年的加油量大於等於到下一個加油站的路程        {            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;}

解答:

紫書上面的例題

注意此題只能單方向走,不能反向走圈。

此題可以用到前面提到的滑動視窗的知識,首先把所有a[i]值變成第i加油站的油量減去從i到i+1個加油站的消耗油量,
記錄在a[i]當中。

如果問題有解,那麼a[i]的累加和一定是大於等於0的

個首先設定當前起始點s,然後從s往前走,以ind值遍曆s後面的剩餘油量a[i],並以tmp變數累加a[i],如果出現tmp<0,那麼一定是從s點到ind點不能走通,那麼起始點向前移動,同時變化tmp值,變化的方法為tmp-=a[s],因為以s為起點到ind之間這個視窗 [s,ind] [ s , i n d ] [s,ind]的油量值是tmp,現在視窗左側的s值要向前移動,所以要把視窗中左區間的加油站剩餘值a[s]減掉即可。

最後輸出結果即可。。

書中的做法要簡單不少,原話大意為:考慮第i號交友站,判斷是否有解,如果有輸出,如果沒有,說明途中遇到加油站p,從p到p+1沒油了。那麼,以i+1,i+2,i+3….p為起點也不是解,原因是,如果i能作為起點,i點的油量減去到i+1點的消耗值一定大於等於0的一個數,設為從i點走到i+1點後剩餘的油量為t(t>=0)。如果從i點到p沒油了,那麼從i+1點到p點,相當於從i點到p點減去s,那剩下的油量就更少了。所以,如果i到p如果無解,直接跳到以p+1為起點進行判斷即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.