POJ 1363 解题报告
2015-03-24 07:16
381 查看
这道题不难但是题目很难理解。看了测试样例才明白,如果入栈顺序是递增的:1,2,3,4,5. 那么给出一个出栈顺序,比如5,4,1,2,3,判断这个出栈顺序是否可能。
我这里就是按照题意模拟的。比如碰到5,就将小于等于5的都入栈(1,2,3,4,5),然是将5出栈(判断这时栈顶一定是5),同样地,之后遇到4,已经没有什么可入栈了,栈顶是4,出栈,再遇到1,同样没什么可入栈的,栈顶元素是3,不是1,说明不可能。
我这里就是按照题意模拟的。比如碰到5,就将小于等于5的都入栈(1,2,3,4,5),然是将5出栈(判断这时栈顶一定是5),同样地,之后遇到4,已经没有什么可入栈了,栈顶是4,出栈,再遇到1,同样没什么可入栈的,栈顶元素是3,不是1,说明不可能。
thestoryofsnow | 1363 | Accepted | 156K | 110MS | C++ | 1354B |
/* ID: thestor1 LANG: C++ TASK: poj1363 */ #include <iostream> #include <fstream> #include <cmath> #include <cstdio> #include <cstring> #include <limits> #include <string> #include <vector> #include <list> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> #include <cassert> using namespace std; int main() { int N; scanf("%d", &N); while (N) { int num; scanf("%d", &num); // the number of numbers input int cnt = 1; // fprintf(stderr, "num: %d, cnt: %d\n", num, cnt); if (!num) { printf("\n"); scanf("%d", &N); continue; } int instacktop = 1; stack<int> outstack; bool flag = true; while (true) { while (instacktop <= N && instacktop <= num) { outstack.push(instacktop); instacktop++; } if (outstack.empty() || outstack.top() != num) { flag = false; break; } outstack.pop(); if (cnt == N) { break; } scanf("%d", &num); cnt++; // fprintf(stderr, "num: %d, cnt: %d\n", num, cnt); } // get the rest of numbers while (cnt < N) { scanf("%d", &num); cnt++; // fprintf(stderr, "num: %d, cnt: %d\n", num, cnt); } if (flag) { printf("Yes\n"); } else { printf("No\n"); } } return 0; }
相关文章推荐
- poj 1363 Rails 解题报告
- poj解题报告——1363
- poj1363--Rails解题报告(c++版)
- 【原】 POJ 3126 Prime Path 筛素数+BFS单源无权最短路径 解题报告
- 解题报告:POJ_3904 Sky Code 莫比乌斯反演|容斥
- POJ1001 Exponentiation解题报告
- poj解题报告——1350
- poj2914解题报告
- POJ 2513 Colored Sticks 解题报告
- POJ 1469 COURSES 解题报告
- poj解题报告——2948
- poj 3774 Elune’s Arrow 解题报告
- POJ 1009 解题报告
- poj1390解题报告
- poj2406解题报告
- POJ 3020(最小路径覆盖)解题报告
- POJ 2255 解题报告
- POJ 2733 判断闰年 解题报告
- POJ 2753 菲波那契数列 解题报告
- POJ 2806 公共子序列 解题报告