poj 1363 火车进站 (栈的应用)
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
The last block consists of just one line containing 0.
Output
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 先输入火车车厢数N,然后输入N个数代表车厢编号a[i],判断火车是否能按1~N或者N~1的顺序开出,a[0] = 0;代表车厢数为N的循环结束,N = 0;代表程序结束。
思路:
先输入车厢编号,用数组a[i]保存,在从1~N依次输入栈st中,依次判断st.top()是否等于[i].
#include<cstdio>
#include<stack>
using namespace std;
int main()
{
int n,a[],i,j;
while(scanf("%d",&n) && n)
{
stack<int>st;
while(scanf("%d",&a[]) && a[])
{
for( i = ; i < n ; i++)
{
scanf("%d",&a[i]);
}
for( i = , j = ; i <= n ; i++)
{
st.push(i);
while(st.top() == a[j])
{
j++;
st.pop();
if(st.empty()) //栈为空时结束循环
break;
}
}
if( j == n) printf("Yes\n");
else
printf("No\n");
}
printf("\n");
}
}
poj 1363 火车进站 (栈的应用)的更多相关文章
- POJ 1363 Rails(栈)
题目代号:POJ 1363 题目链接:http://poj.org/problem?id=1363 题目原题: Rails Time Limit: 1000MS Memory Limit: 100 ...
- poj 1363 Rails (【栈的应用】 刘汝佳的写法 *学习)
Rails Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25964 Accepted: 10199 Descripti ...
- 华为OJ:火车进站
火车进站 给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号.要求以字典序排序输出火车出站的序列号. 输入描述: 有多组测试用例, ...
- C++ HOJ 火车进站
[问题描写叙述] 给定一个正整数N代表火车数量.0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号. 要求以字典序排序输出火车出站的序列号. 输入: 有多组 ...
- (hdu)1022 Train Problem I 火车进站问题
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1022 Problem Description As the new term comes, ...
- 【火车出栈】ZOJ - 2603 Railroad Sort
好久没写递归了,怕手生再来练练手. 题意:车轨上有上图所示的n个中转栈,现有2n个列车,给出列车初始编号序列.列车从最右边驶入车轨,并且列车只能从右向左移动,要求给出列车中转操作序列,使列车经过这n个 ...
- 【讲●解】火车进出栈类问题 & 卡特兰数应用
火车进出栈类问题详讲 & 卡特兰数应用 引题:火车进出栈问题 [题目大意] 给定 \(1\)~\(N\) 这\(N\)个整数和一个大小无限的栈,每个数都要进栈并出栈一次.如果进栈的顺序为 \( ...
- CodeVS3958 火车进站
3958 火车进站 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 火车站内往往设有一些主干线分叉出去的铁路支路 ...
- luogu P1044 火车进出栈问题(Catalan数)
Catalan数就是魔法 火车进出栈问题即: 一个栈(无穷大)的进栈序列为 1,2,3,4,...,n 求有多少个不同的出栈序列? 将问题进行抽象, 假设'+'代表进栈, 则有'-'代表出栈 那么如果 ...
随机推荐
- Python实现判断回文串
回文数的概念:即是给定一个数,这个数顺读和逆读都是一样的.例如:121,1221,a,aa是回文数,123,1231不是回文数. while 1: String = input('请先输入一个字符串 ...
- Git客户端使用教程
课程地址 <版本控制入门 – 搬进 Github> 笔记参考 <搬进 Github> Git客户端的使用 Git for windows下载 新建一个仓库tata,使用subl ...
- 暑期训练狂刷系列——Foj 1894 志愿者选拔 (单调队列)
题目连接: http://acm.fzu.edu.cn/problem.php?pid=1894 解题思路: 因为出队和入队都满足队列的性质,优先单调队列的优先级有进入队列的顺序和人品的高低,在一段区 ...
- DB2:在缓冲池 "1" 中当前没有任何页面可用。. SQLCODE=-1218, SQLSTATE=57011, DRIVER=3.61.75
服务器端DB2数据库最近频繁报错: 在缓冲池 , SQLSTATE=, DRIVER= 试验发现简单的查询SQL执行没有问题,复杂的SQL就会报上述错误. 百度发现是因为数据库bufferpool太小 ...
- 416 Partition Equal Subset Sum 分割相同子集和
详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...
- AJPFX:关于面向对象及java的一些机制的思考
1.变量的作用域和生命周期什么关系? 2.java除了在类体和方法体(包括参数)中可以声明变量外,其他位置一定不可以声明变量吗?比如高级别的应用里是不是有例外呢? 3.java源文件中代码的组织方式一 ...
- 工厂方法模式及php实现
工厂方法模式: 工厂方法模式(Factory Method Pattern)又称为工厂模式,也叫虚拟构造器(Virtual Constructor)模式或者多态工厂(Polymorphic Facto ...
- RSA js加密 java解密
1. 首先你要拥有一对公钥.私钥: ``` pubKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1gr+rIfYlaNUNLiFsK/Kn ...
- C/C++ new/delete []、内存泄漏、动态数组
一.概念 new/delete是用于动态分配和撤销内存的运算符.new/delete是c++里才有的,c中是用malloc和free,c++虽然也可以用,但是不建议用.当我们使用关键字new在堆上动态 ...
- Winsock2_WSADATA
使用Winsock2进行win下网络编程的第一步是初始化Winsock.其中需要创建一个WSADATA类型的变量,这个变量用来保存Windows socket的实现信息. typedef struct ...