zoukankan      html  css  js  c++  java
  • poj1363 Rails

    Rails

    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.

    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.

    intput

    The input file 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 file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains ‘Yes’ if it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains ‘No’. In addition, there is one empty line after the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last “null” block of the input file.

    Sample Input

    5

    1 2 3 4 5

    4 1 2 3

    0

    6

    6 5 4 3 2 1

    0

    0

    Sample Output

    Yes

    No

    Yes

    栈的基础题目,按汝佳大佬的方法一点一点写咯。

     1 #include<iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<stdio.h>
     5 #include<stack>
     6 #include<string.h>
     7 #include<string>
     8 #include<algorithm>
     9 #include<queue>
    10 
    11 int main()
    12 {
    13     int t,a[1010],i;
    14     while(cin>>t,t)
    15     {
    16         while(1)
    17         {
    18         cin>>a[1];
    19         if(!a[1])
    20         {
    21             cout<<endl;
    22             break;
    23         }
    24         for(i=2;i<=t;i++)
    25             cin>>a[i];
    26         int TM=1;
    27         int aa=1,bb=1;
    28         stack<int>s;
    29         while(bb<=t)
    30         {
    31             if(aa==a[bb])
    32             {
    33                 aa++;
    34                 bb++;
    35             }
    36             else if(!s.empty()&&s.top()==a[bb])
    37             {
    38                 bb++;
    39                 s.pop();
    40             }
    41             else if(aa<t)
    42             {
    43                 s.push(aa++);
    44             }
    45             else
    46             {
    47                 TM=0;
    48                 break;
    49             }
    50         }
    51         if(TM)
    52             cout<<"Yes"<<endl;
    53         else
    54             cout<<"No"<<endl;
    55         }
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    挑战程序设计竞赛 第2章习题 poj 1017 Packets 贪心模拟
    挑战程序设计竞赛 2章习题 poj 2376 Cleaning Shifts
    Leetcode 27. 移除元素 双指针
    Leetcode 26. 删除有序数组中的重复项 双指针
    Leetcode 31. 下一个排列
    webserver 发布问题
    [转]机器学习中的各种距离
    VUE3 + TYPESCRIPT 开发实践总结
    我和ABP vNext 的故事
    ABP Framework 为什么好上手,不好深入?探讨最佳学习姿势!
  • 原文地址:https://www.cnblogs.com/dulute/p/7210281.html
Copyright © 2011-2022 走看看