例题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;}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 猪肉馅不新鲜了怎么办 买的肉有点臭了怎么办 炸的东西不脆了怎么办 油炸东西回软了怎么办 吃石斑鱼蛋吐了怎么办 家里的烟筒堵了怎么办 脖子上长鸡皮肤怎么办 铁板烤蔬菜粘锅怎么办 残余尿量300ml怎么办 肌肉拉伤怎么办恢复快小腿 睡觉把背扭了怎么办 后背一侧扭筋了怎么办 背部的筋扭到了怎么办 跳绳跳得膝盖疼怎么办 跑步小腿变粗了怎么办 一蹲下膝盖就响怎么办 做深蹲时膝盖总是吱吱响怎么办 爬山爬的膝盖疼怎么办 膝盖一吹风就疼怎么办 走路太多膝盖腿疼怎么办 膝盖一着凉就痛怎么办 月子里脚受凉了怎么办 膝关节受凉少量积液发胀怎么办 刮痧后吹空调了怎么办 200斤胖子膝盖痛怎么办 风扇吹的腿疼怎么办 膝盖受凉但不疼怎么办 刮痧后洗了澡怎么办 刮痧后喝了啤酒怎么办 艾灸后吃水果了怎么办 刮痧后能洗澡了怎么办 膝盖筋扭了肿了怎么办 骑行之后膝盖疼怎么办 膝盖软骨磨没了怎么办 腿上膝盖长骨刺怎么办 打羽毛球后膝盖疼怎么办 打完羽毛球膝盖疼怎么办 骑动感单车后膝盖痛怎么办 踩完动感单车右膝盖痛怎么办 跑步机走步损伤膝盖怎么办 健身把膝盖伤了怎么办