zoukankan      html  css  js  c++  java
  • 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 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

    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


    代码为:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    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"<<endl;
             else
                cout<<"No"<<endl;
         }
         printf("
    ");
        }
        return 0;
    }
    


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Poj 1742 Coins(多重背包)
    Poj 2350 Above Average(精度控制)
    求二进制数中1的个数
    Poj 1659 Distance on Chessboard(国际象棋的走子规则)
    Poj 2411 Mondriaan's Dream(压缩矩阵DP)
    Poj 2136 Vertical Histogram(打印垂直直方图)
    Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)
    poj 2390 Bank Interest(计算本利和)
    Poj 2533 Longest Ordered Subsequence(LIS)
    Poj 1887 Testing the CATCHER(LIS)
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4807845.html
Copyright © 2011-2022 走看看