铁轨(UVa 514)

  利用栈实现

C++11 代码如下:

 1 #include<iostream>
 2 #include<stack>
 3 using namespace std;
 4 #define maxn 1001
 5 int n, num[maxn];
 6
 7 int main() {
 8     while ((cin >> n)&&n!=0) {
 9         while (n) {
10             for (int i = 1; i <= n; i++) {
11                 cin >> num[i];
12                 if (num[1] == 0) {
13                     n = 0; break;
14                 }
15             }
16             if (n == 0)break;
17             stack<int>s;
18             int A = 1, B = 1;
19             bool flag = true;
20             while (B <= n) {
21                 if (A == num[B]) { A++; B++; }
22                 else if ((!s.empty()) && s.top() == num[B]) { s.pop(); B++; }
23                 else if (A <= n) s.push(A++);
24                 else { flag = false; break; }
25             }
26             cout << (flag ? "Yes" : "No") << endl;
27         }
28         cout << endl;
29     }
30     return 0;
31 }

原文地址:https://www.cnblogs.com/pgzhang/p/9385366.html

时间: 2024-08-30 00:42:20

铁轨(UVa 514)的相关文章

[2016-02-03][UVA][514][Rails]

时间:2016-02-03 22:24:52 星期三 题目编号:UVA 514 题目大意:给定若干的火车(编号1-n),按1-n的顺序进入车站, 给出火车出站的顺序,问是否有可能存在 分析:    FIFO,用栈模拟一遍即可, 方法:    根据输入的顺序,从1-n开始,当前操作的为i 如果i是当前对应的编号,那么直接跳过(进入B) 如果不是,根据当前需求的编号,小于i,就从栈顶弹出一个元素, 看这个元素是否是需求的,是则继续.否则NO 1 2 3 4 5 6 7 8 9 10 11 12 13

UVa 514 Rails(经典栈)

 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, i

UVa 514 - Rails

https://uva.onlinejudge.org/external/5/514.pdf 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 possib

Rails,uva 514

题目:铁轨 题目链接:UVa514链接 题目描述: 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1-n.你的任务是判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶入车站.例如,出栈顺序(5 4 1 2 3)是不可能的,但是(5 4 3 2 1)是可能的. 题目分析: 为了重组车厢,借助中转站,对于每个车厢,一旦从A移入C就不能回到A了,一旦从C移入B,就不能回到C了,意思就是A->C和C->B.而且在中转站C中,车厢符合后进先出的原则.故这里可以看做为一个栈. 题目

UVa 514 数据结构栈

背景:1A 思路:栈模拟 我的代码: #include <set> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <map> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm&g

UVA 514

#include<iostream> #include<string> #include<string.h> #include<stack> #include<stdio.h> using namespace std; int main(){ int n,mm=0; int a[1010]; while(cin>>n){ if(n==0) break; while(true){ stack<int> st; mm=0; m

UVA - 514 Rails 经典栈使用

题目大意:有一列n节车厢的火车要入栈,车厢从1到n,只能从小到大入栈. 现在给出一个出栈顺序,问能否实现 解题思路:如果栈顶元素大于要出栈的数,肯定就不能实现了. 如果栈顶元素小于要出栈的数,就继续入栈 如果栈定元素等于要出栈的数,就出栈 #include<cstdio> #include<algorithm> #include<stack> using namespace std; #define maxn 1010 int num[maxn], n; void so

UVa 514 (stack的使用) Rails

练习一下stack的使用,还有要注意一下输入的格式,看了好长时间没懂. 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <stack> 6 using namespace std; 7 8 const int maxn = 1000 + 10; 9 int n, target[maxn]; 10 11 int main(vo

UVa 514 Rails(模拟栈)

题意  n辆火车按顺序依次进站  判断给定的出战顺序是否可能 用数组模拟模拟栈代表车站  车依次进站  每当栈顶火车序号与当前要出站的b[cur] 相等时 就让栈顶元素出栈  即top-- #include<cstdio> #include<cstring> using namespace std; const int N = 2000; int b[N], c[N]; int main() { int l, cur, top; while(scanf("%d"