Uva 514 (模拟stack)
来源:互联网 发布:linux 除法 保留小数 编辑:程序博客网 时间:2024/06/05 02:03
Uva 514
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.
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 coaches numbered in increasing order 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 . 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.
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
The last line of the block contains just 0.
The last block consists of just one line containing 0.
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.
简单模拟STACK
#include <iostream>#include <stack>using namespace std;bool solve(int a[],int b[],int t){ int i,j; stack <int> c; for(i=1,j=1;j<=t;i++){ if(b[j]==a[i]) j++; else if(!c.empty()&&c.top()==b[j]){ c.pop(); j++; i--; }else if(i<=t) c.push(a[i]); else{ return 0; } } return 1;}int main(){ int t; while(cin>>t){ if(!t) break; int a[1005]={0}; int b[1005]={0}; for(int i=1;i<=t;i++) a[i]=i; while(1){ cin>>b[1]; if(!b[1]) break; for(int i=2;i<=t;i++) cin>>b[i]; if(solve(a,b,t)) cout<<"Yes"<<endl; else cout<<"No"<<endl; } cout<<endl; }}
- Uva 514 (模拟stack)
- uva 10205 - Stack 'em Up(模拟)
- UVa 514 Rails(stack)
- UVa 10205 Stack 'em Up (模拟)
- UVa--514 Rails (stack)
- Uva 514 Rails 栈,stack
- UVa - 514 - Rails(栈-stack)
- UVA 10205 Problem E: Stack 'em Up(模拟)
- Rails UVA 514(栈模拟,水题)
- UVA 514 - Rails(模拟)
- UVA Palindromes(模拟)
- uva 133(模拟)
- UVA 12662(模拟)
- UVA 12503(模拟)
- UVA 12504(模拟)
- UVa 673 Parentheses Balance (stack)
- 封装顺序栈(模拟实现stack)
- 封装顺序栈(模拟实现stack)
- No ulink device found肿么办
- JavaSE导论
- iOS Responder Chain
- xml解析-sax范例
- unity动态加载之AssetBundle应用
- Uva 514 (模拟stack)
- UDP与TCP的区别
- Android系统启动-init篇
- MES设备流程0 - 基本操作
- Session原理浅析
- 【Android 自定义控件】TextView的drawable与text一起居中
- Android系统启动-zygote篇
- 激活django的admin管理界面
- python ctrl +d 退出