poj 1363 Rails (棧的應用+STL)

來源:互聯網
上載者:User

標籤:poj   acm   stl   棧   

Rails
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24762   Accepted: 9715

Description

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track. 

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, ..., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ..., aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station. 

Input

The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, ..., N. The last line of the block contains just 0. 

The last block consists of just one line containing 0.

Output

The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null‘‘ block of the input.

Sample Input

51 2 3 4 55 4 1 2 3066 5 4 3 2 100

Sample Output

YesNoYes

Source

Central Europe 1997
棧的應用的簡單題;就是給你一個出棧的序列,判斷這段序列是否合法,這道題有兩種思路:1.每個已出棧之後的數且小於此數的數都必須按降序排列。複雜度O(n^2),任何元素x出棧前,所有大於x的元素必須出棧,棧內的元素值必須小於x,因為大於x的元素後於x入棧,小於x的值先於x入棧;2.直接類比入棧出棧的過程,可以用數組類比,也可以用STL,我這裡把兩種類比都實現了;複雜度O(n)。直接用數組類比:用數組類比棧
#include <cstdio>using namespace std;const int maxn=1000+5;int a[maxn],b[maxn];//a數組儲存入棧的序列,b數組儲存要判斷的序列int main(){    int n,i,j,k;    while(scanf("%d",&n)&&n)    {        while(scanf("%d",&b[0])&&b[0])        {            for(i=1;i<n;i++)                scanf("%d",&b[i]);           for(i=1,j=0,k=0;i<=n&&j<n;i++,j++)           {               a[j]=i;               while(a[j]==b[k])//進行判斷(出棧的順序是否合理)               {                   if(j>0) j--;//判斷下一個                   else                   {                       a[j]=0;                       j--;                   }                   k++;                   if(j==-1) break;               }           }           if(k==n) printf("Yes\n");//如果全部匹配就輸出yes           else printf("No\n");        }        printf("\n");    }    return 0;}
用STL棧來實現,思想上是一樣的:代碼略有不同
#include <cstdio>#include <stack>using namespace std;const int maxn=1000+5;int a[maxn];int main(){    int n,i,k;    while(scanf("%d",&n)&&n)    {        stack<int>s;//設立一個棧儲存按順序進棧的序列 (一個空棧)        while(scanf("%d",&a[0])&&a[0])        {            for(i=1;i<n;i++)                scanf("%d",&a[i]);//要進行判斷出棧的序列            for(i=1,k=0;i<=n;i++)            {                s.push(i);//進棧                while(s.top()==a[k])//判斷棧頂元素和a數組是否相等                {                    if(!s.empty()) s.pop();//棧不為空白就出棧                    k++;//判斷下一個位置                    if(s.empty()) break;//直到棧空就結束迴圈                }            }            if(k==n) printf("Yes\n");//完全符合就輸出yes           else printf("No\n");        }        printf("\n");    }    return 0;}
棧的基本思想的實現,水題,主要要掌握這種思想。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.