POJ-1363 Rails
来源:互联网 发布:身份证借给人家开淘宝 编辑:程序博客网 时间:2024/05/01 11:59
Description
There is afamous railway station in PopPush City. Country there is incredibly hilly. Thestation was built in last century. Unfortunately, funds were extremely limitedthat time. It was possible to establish only a surface track. Moreover, itturned out that the station could be only a dead-end one (see picture) and dueto lack of available space it could have only one track.
The local tradition is that every train arriving from the direction A continuesin the direction B with coaches reorganized in some way. Assume that the trainarriving from the direction A has N <= 1000 coaches numbered in increasingorder 1, 2, ..., N. The chief for train reorganizations must know whether it ispossible to marshal coaches continuing in the direction B so that their orderwill be a1, a2, ..., aN. Help him and write a program that decides whether itis possible to get the required order of coaches. You can assume that singlecoaches can be disconnected from the train before they enter the station andthat 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 asnecessary in the station. But once a coach has entered the station it cannotreturn to the track in the direction A and also once it has left the station inthe direction B it cannot return back to the station.
Input
The inputconsists of blocks of lines. Each block except the last describes one train andpossibly more requirements for its reorganization. In the first line of theblock there is the integer N described above. In each of the next lines of theblock there is a permutation of 1, 2, ..., N. The last line of the blockcontains just 0.
The last block consists of just one line containing 0.
Output
The outputcontains the lines corresponding to the lines with permutations in the input. Aline of the output contains Yes if it is possible to marshal the coaches in theorder required on the corresponding line of the input. Otherwise it containsNo. In addition, there is one empty line after the lines corresponding to oneblock 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
题目大意:有一个火车站,只有一个出入口,检验调度的可能性
思路:堆栈模拟,由于总是受到堆栈是否为空影响在栈底压入0,从而方便之后的计算
代码
#include <iostream>#include <stack>#include <cstdio>using namespace std;int main(){int i,j,n,a[1003];stack<int> s;s.push(0);scanf("%d",&n);while(n!=0) { while(1) { scanf("%d",&a[0]); if(a[0]==0)break; for(i=1;i<n;i++) scanf("%d",&a[i]); j=0,i=1; while(i<=n||a[j]==s.top()) { if(a[j]!=i&&a[j]!=s.top()) s.push(i++); else { if(a[j]==i)i++; else s.pop(); j++; } } if(s.top()==0) printf("Yes\n"); else { printf("No\n"); while(s.top()!=0)s.pop(); } } puts(""); scanf("%d",&n); } return 0;}
- Poj 1363 Rails
- 【poj】1363-Rails
- POJ 1363 Rails
- [poj] 1363 Rails
- poj 1363(Stack ) Rails
- poj,1363,Rails
- poj-1363-Rails(栈)
- poj 1363 Rails
- poj 1363 Rails
- poj 1363 Rails
- POJ - 1363 Rails
- POJ 1363 Rails
- poj-1363-rails
- [置顶]poj-1363Rails
- POJ-1363 Rails
- POJ 1363 Rails
- OpenJudg / Poj 1363 Rails
- POJ 1363 Rails
- Java并发学习之四——操作线程的中断机制
- Unknown media type in type ‘all/all’ 的解决办法
- HDU 4939 Stupid Tower Defense dp
- 程序员请尊重前辈的代码
- hdu 1867 A + B for you again(KMP)
- POJ-1363 Rails
- Maven管理Spring的jar包
- HDU 2056 rectangles
- Git
- Page Cache, the Affair Between Memory and Files(转自melody_lu123)
- HDOJ 题目1233还是畅通工程(最小生成树,kruskal模板)
- android--SwipeListView实现ListView滑动删除效果
- 汇总:缓存、缓存算法和缓存框架简介
- iOS的View之间的数据传递