程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj1363Rails(棧模擬)

poj1363Rails(棧模擬)

編輯:C++入門知識

poj1363Rails(棧模擬)


題目鏈接:

啊哈哈,點我點我

思路:

這道題就是一道簡單的棧模擬。。。。我最開始覺得難處理是當出棧後top指針變化了。。當不滿足條件時入棧的當前位置怎麼辦,這時候想到用一個Copy數組保持入棧記錄即可。。當滿足所有的火車都出棧時或者已經沒有火車可以進棧了,那麼久跳出。。最後判斷
是否出棧的火車是否達到n。。。

題目:

Rails Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24940 Accepted: 9771

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 fZ喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vciB0cmFpbiByZW9yZ2FuaXphdGlvbnMgbXVzdCBrbm93IHdoZXRoZXIgaXQgaXMgcG9zc2libGUgdG8gbWFyc2hhbCBjb2FjaGVzIGNvbnRpbnVpbmcgaW4gdGhlIGRpcmVjdGlvbiBCIHNvIHRoYXQgdGhlaXIgb3JkZXIgd2lsbCBiZSBhMSwgYTIsIC4uLiwgYU4uIEhlbHAgaGltIGFuZCB3cml0ZSBhIHByb2dyYW0gdGhhdCBkZWNpZGVzIHdoZXRoZXIgaXQgaXMgcG9zc2libGUgdG8gZ2V0IHRoZSByZXF1aXJlZCBvcmRlciBvZiBjb2FjaGVzLgogWW91IGNhbiBhc3N1bWUgdGhhdCBzaW5nbGUgY29hY2hlcyBjYW4gYmUgZGlzY29ubmVjdGVkIGZyb20gdGhlIHRyYWluIGJlZm9yZSB0aGV5IGVudGVyIHRoZSBzdGF0aW9uIGFuZCB0aGF0IHRoZXkgY2FuIG1vdmUgdGhlbXNlbHZlcyB1bnRpbCB0aGV5IGFyZSBvbiB0aGUgdHJhY2sgaW4gdGhlIGRpcmVjdGlvbiBCLiBZb3UgY2FuIGFsc28gc3VwcG9zZSB0aGF0IGF0IGFueSB0aW1lIHRoZXJlIGNhbiBiZSBsb2NhdGVkIGFzIG1hbnkgY29hY2hlcwogYXMgbmVjZXNzYXJ5IGluIHRoZSBzdGF0aW9uLiBCdXQgb25jZSBhIGNvYWNoIGhhcyBlbnRlcmVkIHRoZSBzdGF0aW9uIGl0IGNhbm5vdCByZXR1cm4gdG8gdGhlIHRyYWNrIGluIHRoZSBkaXJlY3Rpb24gQSBhbmQgYWxzbyBvbmNlIGl0IGhhcyBsZWZ0IHRoZSBzdGF0aW9uIGluIHRoZSBkaXJlY3Rpb24gQiBpdCBjYW5ub3QgcmV0dXJuIGJhY2sgdG8gdGhlIHN0YXRpb24uIDxicj4KPGJyPgoKPHAgY2xhc3M9"pst">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

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

Sample Output

Yes
No

Yes

Source

Central Europe 1997

代碼為:

#include
#include
#include
using namespace std;

const int maxn=1000+10;
int in[maxn],out[maxn],temp[maxn];

int main()
{
    int n,tail,top,cal,pd,u,flag;
    ind:while(~scanf("%d",&n))
    {
        flag=1;
        if(n==0)  return 0;
        while(1)
        {
          cal=0;
          pd=top=tail=1;
          for(int i=1;i<=n;i++)
          {
              in[i]=i;
              scanf("%d",&out[i]);
              if(out[1]==0)
                {
                    flag=0;
                    break;
                }
          }
         if(flag==0)
            break;
         temp[0]=0;
         temp[pd]=in[pd];
         while(cal<=n&&pd<=n)
         {
            if(temp[top]==out[tail])
            {
                top--;
                tail++;
                cal++;
            }
            else
               temp[++top]=in[++pd];
            if(cal>=n||pd>n)
              break;
         }
         if(cal>=n)
            cout<<"Yes"<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved