铁轨
来源:互联网 发布:gotv源码 编辑:程序博客网 时间:2024/04/19 12:05
某城市有一个火车站,有一节车厢从A方向驶来,按进站顺序编号为1~n。你的任务就是让他们按照某种特定的顺序进入B方向的铁轨并驶出车站。为了重组车厢,你可以借助中转站C,这是一个可以停放任意多节车厢的车站,但由于末端封顶,驶入C的车厢必须按照相反的顺序驶出C。对于每节车厢,一旦从A移入C,就不能再回到A了;一旦从C移入B,就不能再回到C了。
样例输入;
5
1 2 3 4 5
5
5 4 1 2 3
6
6 5 4 3 2 1
样例输出:
Yes
NO
Yes
STL 中栈的使用方法(stack)
基本操作:
push(x) 将x加入栈中,即入栈操作
pop() 出栈操作(删除栈顶),只是出栈,没有返回值
top() 返回第一个元素(栈顶元素)
size() 返回栈中的元素个数
empty() 当栈为空时,返回 true
使用方法:
和队列差不多,其中头文件为:
#include <stack>
定义方法为:
stack<int>s1;//入栈元素为 int 型
stack<string>s2;// 入队元素为string型
stack<node>s3;//入队元素为自定义型
stack<string>s2;// 入队元素为string型
stack<node>s3;//入队元素为自定义型
#include<iostream>#include<stack>using namespace std;const int MAXN=1000+10;int n,i;int target[MAXN];int main(){ while(cin>>n) { stack<int> s; int A=1,B=1; for(i=1;i<=n;i++) cin>>target[i]; int ok=1; while(B<=n) { if(A==target[B]) { A++; B++; } else if(!s.empty()&&s.top()==target[B]) { s.pop(); B++; } else if(A<=n) s.push(A++); else { ok=0; break; } } string str=ok?"Yes":"No"; cout<<str<<endl; } return 0;}
0 0
- 铁轨
- 铁轨
- 铁轨
- 铁轨
- 铁轨
- 铁轨!!!
- 铁轨!!!!
- 铁轨
- 铁轨
- 铁轨
- 铁轨 栈
- 栈 铁轨
- 铁轨问题
- 栈------铁轨
- 铁轨问题
- 铁轨(Rails)
- UVa514 铁轨
- 170402铁轨
- 探寻C++最快的读取文件的方案
- 区间覆盖问题
- Android开发中倒计时功能的实现
- IOS UIView 之类方法篇 Class Methods
- 关于函数PsSetLoadImageNotifyRoutine的通知时机
- 铁轨
- k60 mcg模块概述
- HTML5画布谷歌跳跃的球
- 驱动读取注册表问题
- Valid Palindrome 判断一个字符串是否是回文串
- A drop of performance testing- more protocols
- (十三)Struts2中ognl标签
- 求解关于ie11的报错问题(C++新手)
- Eclipse快捷键大全