算法竞赛 例6-2铁轨(UVa 514)
2018-02-06 23:04
190 查看
Sample Input
5
12345
54123
0
6
654321
0
0
Sample Output
Yes
No
Yes
下面代码并不能按照题述输入输出格式输出。问题在于不知道如何处理这个0
5
12345
54123
0
6
654321
0
0
Sample Output
Yes
No
Yes
下面代码并不能按照题述输入输出格式输出。问题在于不知道如何处理这个0
#include<iostream> #include<stack> #include<cstdio> using namespace std; const int MAXN = 1000 + 10; int n,target[MAXN]; int main() { freopen("/Users/zhaohaibo/Desktop/a.txt","r",stdin); while(scanf("%d",&n)==1) { stack<int> s; int A=1,B=1; for(int i=0; i<n; i++) scanf("%d",&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; } } printf("%s\n",ok?"Yes":"No"); } return 0; }
相关文章推荐
- 算法之路二:刘汝佳算法竞赛入门经典:栈和队列 铁轨UVa514
- 《算法竞赛-训练指南》第二章-2.2_UVa 11401
- 《算法竞赛-训练指南》第二章-2.3_UVa 11806
- 【算法竞赛入门经典】多阶段决策问题 例题9-4 UVa116
- 算法竞赛入门经典 习题3-1 得分(Score, ACM/ICPC Seoul 2005, UVa1585)
- UVA514铁轨
- 《算法艺术与信息学竞赛》之 栈 例一 铁轨 Uva 514
- UVa 514 Rails(铁轨) 栈
- 算法竞赛入门经典 UVa1585
- UVA514:Rails(铁轨)
- 算法竞赛入门经典-前四章-UVa10300 - Ecological Premium-生态奖金-难度0
- UVa 11426--算法竞赛入门经典训练指南
- [刷题]算法竞赛入门经典 3-12/UVa11809
- 【算法竞赛入门经典】6.5[图的概念和拓扑序] 例题6-21 UVa506
- 算法竞赛入门经典 UVa1586
- 【算法竞赛入门经典】6.5[图的BFS] 例题6-20 UVa1599 (2)
- 【算法竞赛入门经典】7.7 回溯法求连通块 例题7-14 UVa1602
- 算法竞赛入门经典-习题3-6 纵横字谜的答案(Crossword Answers, ACM/ICPC World Finals 1994,UVa232)
- 算法竞赛入门经典第五章例题5-5 The SetStack Computer UVA - 12096
- 算法竞赛入门经典 第二版 习题9-2 免费糖果 Free Candies uva10118