例题6-2 铁轨(Rails, ACM/ICPC CERC 1997, UVa 514)
来源:互联网 发布:乐清知临寄宿学校网站 编辑:程序博客网 时间:2024/04/30 05:14
#include <iostream>#include <string>#include <vector>#include <stack>#include <queue>#include <deque>#include <set>#include <map>#include <algorithm>#include <sstream>#include <utility>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#include <cctype>#define CLEAR(a, b) memset(a, b, sizeof(a))#define IN() freopen("in.txt", "r", stdin)#define OUT() freopen("out.txt", "w", stdout)const int maxn = 1000005;using LL = long long;using UI = unsigned int;const int mod = 1e6 + 7;using namespace std;//------------------------------------------------------------------------------------------//int q[1005] = { 0 }, rear = 0, front = 0;int s[1005] = { 0 }, sfront = 0;bool judge(int x) {if (x >= q[rear] && front != rear) {//若出栈的数大于等于队首的数,则应当把数入栈,当然队列空了也要考虑while (q[rear] != x) s[sfront++] = q[rear++];//把到x为止的所有数入栈,出队rear++;//x出队,入栈出栈}else {if (s[--sfront] != x) return false;//比较栈顶元素与出栈元素}return true;}int main() {#ifdef _DEBUGIN(); OUT();#endifint N;while (cin >> N && N) {int x;while (cin >> x && x) {bool flag = true;CLEAR(q, 0), rear = 0, front = 0;CLEAR(s, 0), sfront = 0;for (int i = 1; i <= N; i++) q[front++] = i;//入队if (!judge(x)) flag = false;for (int i = 0; i < N - 1; ++i) {scanf("%d", &x);if (!judge(x)) flag = false;}if (flag) cout << "Yes\n";else cout << "No\n";}cout << endl;}return 0;}//参考了lls的代码int a[1005];int main() {#ifdef _DEBUGIN(); OUT();#endifint n;while (cin >> n && n) {while (cin >> a[1] && a[1]) {stack<int> s;bool flag = true;for (int i = 2; i <= n; ++i) cin >> a[i];int A = 1, B = 1;//定义A,B两个类似指针的变量来比较,值得学习。while (B != n) {//这个循环把情况全考虑了,,,好厉害啊。。if (A == a[B]) A++, B++;else if (!s.empty() && s.top() == a[B]) s.pop(), B++;else if (A < a[B]) s.push(A++);else { flag = false; break; }}if (flag) cout << "Yes" << endl;else cout << "No" << endl;}cout << endl;}return 0;}
阅读全文
1 0
- 例题6-2 铁轨(Rails, ACM/ICPC CERC 1997, UVa 514)
- 例题6-2 UVA 514 - Rails (铁轨)
- ACM/ICPC CERC 1997,UVa 514,POJ 1363 Rails(栈模拟)
- Rails,ACM/ICPC CERC 1997,UVa514
- UVA 514 铁轨 (Rails)
- UAa514 例题6-2 铁轨 Rails
- UVA 514 Rails 铁轨
- UVa 514 Rails(铁轨) 栈
- 例题8-8 防线(Defense Lines, ACM/ICPC CERC 2010, UVa1471)
- 例题8-14 与非门电路(Gates, ACM/ICPC CERC 2001, UVa1607)
- 例题8-18 洞穴(Cave, ACM/ICPC CERC 2009, UVa1442)
- 例题6-2 铁轨(栈)
- 铁轨(UVa 514)
- 例题6-19 自组合(Self-Assembly, ACM/ICPC World Finals 2013, UVa 1572)
- 经典第六章 例题 6-2 UVA 514 Rails(栈的基础应用)
- uva 514 铁轨(栈)
- UVa 514(栈)铁轨
- 例题6-14 Abbott的复仇(Abbott's Revenge, ACM/ICPC World Finals 2000, UVa 816)
- Myeclipse报错:An unexpected exception was thrown
- hive内部表与外部表
- C++运算符重载讲解与经典实例
- Codeforces 831 E. Cards Sorting set+树状数组模拟
- 数学/物理(游戏开发)《干货收集(持续收集中)》
- 例题6-2 铁轨(Rails, ACM/ICPC CERC 1997, UVa 514)
- ros源码分析(4)—roslaunch之process monitoring(pmon)
- python中zip,map,filter用法
- Ubuntu&GRPC+Idea java demo
- python3爬虫第一步-爬取网页源码
- Python·Jupyter Notebook各种使用方法记录
- Linux 实操
- C++_运算符重载 再思考
- underscore.js 409 -- 526行