清華OJ列車調度(Train)

題目:

列車調度(Train)

Description
Figure 1 shows the structure of a station for train dispatching.

Figure 1
In this station, A is the entrance for each train and B is the exit. S is the transfer end. All single tracks are one-way, which means that the train can enter the station from A to S, and pull out from S to B. Note that the overtaking is not allowed. Because the compartments can reside in S, the order that they pull out at B may differ from that they enter at A. However, because of the limited capacity of S, no more that m compartments can reside at S simultaneously.
Assume that a train consist of n compartments labeled {1, 2, …, n}. A dispatcher wants to know whether these compartments can pull out at B in the order of {a1, a2, …, an} (a sequence). If can, in what order he should operate it?
Input
Two lines:
1st line: two integers n and m;
2nd line: n integers separated by spaces, which is a permutation of {1, 2, …, n}. This is a compartment sequence that is to be judged regarding the feasibility.
Output
If the sequence is feasible, output the sequence. “Push” means one compartment goes from A to S, while “pop” means one compartment goes from S to B. Each operation takes up one line.
If the sequence is infeasible, output a “no”.
Example 1
Input
5 2
1 2 3 5 4
Output
push
pop
push
pop
push
pop
push
push
pop
pop
Example 2
Input
5 5
3 1 2 4 5
Output
No
Restrictions
1 <= n <= 1,600,000
0 <= m <= 1,600,000
Time: 2 sec
Memory: 256 MB
描述
某列車調度站的鐵道聯接結構如Figure 1所示。
其中,A爲入口,B爲出口,S爲中轉盲端。所有鐵道均爲單軌單向式:列車行駛的方向只能是從A到S,再從S到B;另外,不允許超車。因爲車廂可在S中駐留,所以它們從B端駛出的次序,可能與從A端駛入的次序不同。不過S的容量有限,同時駐留的車廂不得超過m節。
設某列車由編號依次爲{1, 2, …, n}的n節車廂組成。調度員希望知道,按照以上交通規則,這些車廂能否以{a1, a2, …, an}的次序,重新排列後從B端駛出。如果可行,應該以怎樣
的次序操作?
輸入
共兩行。
第一行爲兩個整數n,m。
第二行爲以空格分隔的n個整數,保證爲{1, 2, …, n}的一個排列,表示待判斷可行性的駛出序列{a1,a2,…,an}。
輸出
若駛出序列可行,則輸出操作序列,其中push表示車廂從A進入S,pop表示車廂從S進入B,每個操作佔一行。
若不可行,則輸出No。
樣例
見英文題面
限制
1 ≤ n ≤ 1,600,000
0 ≤ m ≤ 1,600,000
時間:2 sec
空間:256 MB

我的代碼:

#include<iostream>
#include<cstring>
#include<cstdio>
#define MAX 1600005
//如果是中國石油大學的同學,請不要抄襲,不然0分處理時很尷尬,請獨立完成,有不理解的地方可與我交流
using namespace std;

int A[MAX], S[MAX], B[MAX];
int curA, curS;
bool output[7 * MAX];
int k;
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &B[i]);
    for (int i = n; i >= 1; i--)
        A[n - i + 1] = i;
    curA = n;
    curS = 0;

    for (int i = 1; i <= n; i++)
    {
        if (S[curS] == B[i])
        {
            curS--;
            output[k++] = false;
        }
        else if (A[curA] == B[i])
        {
            --curA;
            output[k++] = true;
            output[k++] = false;
            if (curS + 1 > m)
            {
                printf("No\n");
                return 0;
            }
        }
        else
        {
            S[++curS] = A[curA--];
            output[k++] = true;
            i--;
            if (!curA || curS > m)
            {
                printf("No\n");
                return 0;
            }
        }
    }

    for (int i = 0; i < k; i++)
    {
        if (output[i])
            printf("push\n");
        else
            printf("pop\n");
    }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章