poj 1363 Rails

来源:互联网 发布:vb与c#语法区别 编辑:程序博客网 时间:2024/05/01 12:55
Rails
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 23573 Accepted: 9358

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. 
有一个在PopPush市一个著名的火车站。那里有一个令人难以置信的丘陵。该站建于上个世纪。不幸的是,那个时候资金极其有限。只能建轨道的一个面。而且,事实证明,该站可能是唯一的一条死胡同(见图片),并由于缺乏可用空间,它只能有一个轨道。

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. 
当地的传统是,每列车自A方向来,车厢重组,再向B方向继续前进。假设从A方向来的列车已经将车厢以N <=1000个编号的递增顺序1,2,...,N编号,列车长需要知道是不是出来能以顺序a1,a2,....,aN出来。帮他写一个程序,决定是否有可能。你可以假设这是一个栈(我懒得翻译那么一大堆了。。。)

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. 
输入好几块,每块好几行,每行都是要求的输出车厢顺序,以0结尾
The last block consists of just one line containing 0.
在所有块的最后也有一个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.
如果能以要求顺序输出,就Yes,不然就No,每块之间有个空行

Sample Input

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

Sample Output

YesNoYes

Source

Central Europe 1997
栈的模拟题,很水,1a,总感觉有更高效的方法,先放过吧。
#include <iostream>#include<algorithm>#include <stdio.h>#include <string.h>#include <math.h>#define MAX_NUM 11111using namespace std;int top;void push(int s[1111], int x){    top ++;    s[top] = x;}void pop(int s[1111], int x){    s[top] = 0;    top --;}int main(){    int i, k, input, n, x;    int s[1111];    int in[1111];    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);    while(scanf("%d", &n) && n != 0){        while(scanf("%d", &x) && x != 0){            in[0] = x;            for (i = 1;i < n;i ++){                scanf("%d", &in[i]);            }            top = -1;input = 1;k = 0;            push(s, input);input ++;            while(input <= n + 1){                if ((top != -1) && (s[top] == in[k])){                    pop(s, s[top]);//                    printf("top = %d, pop = %d\n", top, in[k]);                    k ++;                }else {                    push(s, input);                    input ++;                }            }//            printf("top = %d\n", top);            if (top == 0){                printf("Yes\n");            }else{                printf("No\n");            }        }        printf("\n");    }    return 0;}


0 0