
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.
题目
- 原题地址:Rails
- 题目编号:NC14326
- 题目类型:栈
- 时间限制:C/C++ 1秒,其他语言2秒
- 空间限制:C/C++ 32768K,其他语言65536K
1.题目大意
2.题目分析
- 直接做即可
- 本题有多个测试用例,每组测试用例给出一个
N
和多组数据
- 输入数据为
0
时代表单个用例结束,需要输出换行,输入N
为0
时代表输入结束,不必换行
3.题目代码
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
while(cin >> n){
if(!n) break;
while(true) {
int a[n+1];
cin >> a[1];
if(!a[1]) {
cout << endl;
break;
}
for(int i=2;i<=n;i++) cin >> a[i];
stack<int> s;
int tmp = 1;
for(int i=1;i<=n;i++) {
if(!s.empty()&&s.top()==a[i]) s.pop();
else {
while(a[i]!=tmp) {
s.push(tmp++);
if(tmp>n+1)
break;
}
tmp++;
}
}
if(!s.empty()) cout << "No" << endl;
else cout << "Yes" << endl;
}
}
}