poj 1363 Rails (【栈的应用】 刘汝佳的写法 *学习)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 25964 | Accepted: 10199 |
Description

The local tradition is that every train arriving from the direction A
continues in the direction B with coaches reorganized in some way.
Assume that the train arriving from the direction A has N <= 1000
coaches numbered in increasing order 1, 2, ..., N. The chief for train
reorganizations must know whether it is possible to marshal coaches
continuing in the direction B so that their order will be a1, a2, ...,
aN. Help him and write a program that decides whether it is possible to
get the required order of coaches. You can assume that single coaches
can be disconnected from the train before they enter the station and
that they can move themselves until they are on the track in the
direction B. You can also suppose that at any time there can be located
as many coaches as necessary in the station. But once a coach has
entered the station it cannot return to the track in the direction A and
also once it has left the station in the direction B it cannot return
back to the station.
Input
input consists of blocks of lines. Each block except the last describes
one train and possibly more requirements for its reorganization. In the
first line of the block there is the integer N described above. In each
of the next lines of the block there is a permutation of 1, 2, ..., N.
The last line of the block contains just 0.
The last block consists of just one line containing 0.
Output
output contains the lines corresponding to the lines with permutations
in the input. A line of the output contains Yes if it is possible to
marshal the coaches in the order required on the corresponding line of
the input. Otherwise it contains No. In addition, there is one empty
line after the lines corresponding to one block of the input. There is
no line in the output corresponding to the last ``null'' block of the
input.
Sample Input
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0
Sample Output
Yes
No Yes
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <algorithm>
#include <stack> using namespace std; int main()
{
int n;
int i, j;
int a[1010];
stack<int>q; while(scanf("%d", &n) && n!=0 )
{
while(scanf("%d", &a[0]) && a[0]!=0 )
{
for(i=1; i<n; i++)
{
scanf("%d", &a[i] );
}
int A=1, B=0;
int flag=1;
while(!q.empty())
{
q.pop();
}
while( B<n )
{
if(A==a[B])
{
A++; B++;
}
else if(!q.empty() && q.top()==a[B] )
{
q.pop(); B++;
}
else if(A<=n)
q.push(A++);
else
{
flag=0; break;
}
}
printf("%s\n", flag==1?"Yes":"No" );
}
printf("\n");
}
return 0;
}
poj 1363 Rails (【栈的应用】 刘汝佳的写法 *学习)的更多相关文章
- POJ 1363 Rails(栈)
思路:将出车站的顺序存入数组train,由于入车站的顺序是固定的,为1~N,所以用P表示进站的车,初始为1. 接下来举例说明吧: 原来入站顺序: 1 2 3 4 5 读入的出战顺序: 3 4 2 ...
- vector刘汝佳算法入门学习笔记
//*****-*-----vector***/////// 常用操作封装,a.size();可以读取大小 a.resize();可以改变大小: ...
- ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]
原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...
- c++20701除法(刘汝佳1、2册第七章,暴搜解决)
20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 输入正整数n,按从小到大的顺序输出所有 ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 1(String)
第一题:401 - Palindromes UVA : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8 ...
- 刘汝佳黑书 pku等oj题目
原文地址:刘汝佳黑书 pku等oj题目[转]作者:小博博Mr 一.动态规划参考资料:刘汝佳<算法艺术与信息学竞赛><算法导论> 推荐题目:http://acm.pku.edu. ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- POJ 1363 Rails(栈)
题目代号:POJ 1363 题目链接:http://poj.org/problem?id=1363 题目原题: Rails Time Limit: 1000MS Memory Limit: 100 ...
- poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】
问题如下: 问题 B: Rails 时间限制: Sec 内存限制: MB 提交: 解决: [提交][状态][讨论版] 题目描述 There is a famous railway station in ...
随机推荐
- vue报错之Duplicate keys detected: '0'. This may cause an update error.
昨天运行vue项目的时候,出现了[Vue warn]: Duplicate keys detected: '0'. This may cause an update error(错误,检测到重复的ke ...
- HDU 1045 Fire Net 状压暴力
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- AGC006
AtCoder Grand Contest 006 <br > 心血来潮,开了一套AGC..... 然后发现各种不会做.........感觉智商被AGC摁在地上摩擦...... <b ...
- eclipse不会自动编译的问题解决
注意:非必要的时候,重新下载eclipse安装是最有效的解决方法. 以下为尝试的步骤: 1.看看project->Build Automatically有没有勾上?如果没有,勾上以后,clean ...
- android ListView详解(转)
在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示.抽空把对ListView的使用做了整理,并写了个小例子,如下图. 列表的显示需要三 ...
- 【hibernate】hibernate和mybatis的比较
理解和学习,使自己在做项目中更加得心应手. 第一方面:开发速度的对比就开发速度而言,Hibernate的真正掌握要比Mybatis来得难些.Mybatis框架相对简单很容易上手,但也相对简陋些.个人觉 ...
- 我们为什么要把Dagger2,MVP以及Rxjava引入项目中?
1Why? 我们为什么要把Dagger2,MVP以及Rxjava引入项目中? 毫无疑问在Android开发圈中这三个技术是经常被提及的,如此多的文章和开源项目在介绍他们,使用他们,开发者也或多或少的被 ...
- webpack+vue起步
本文基于vue1.x 基于vue2.x&webpack2.x请移步至 Vue2.x踩坑与总结Webpack2.x踩坑与总结 记得第一次知道Vue.js是在勾三股四大大的微博,那时候他开始翻译v ...
- 关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法
关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法 在传统的ASP.NET中,使用Resp ...
- 【每日Scrum】第五天(4.26) TD学生助手Sprint2站立会议
站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 今天增加了几个页面的子菜单,然后设计了几个要用的界面 今天和楠哥做了课程事件和日历表操作的例子,并尝试做时间表和日历表的数据库设计 安卓的数据 ...