poj1363 Rails(栈模拟)

D - Rails

Crawling in process...
Crawling failed
Time Limit:1000MS    
Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Submit
Status Practice POJ 1363

Appoint description:
System Crawler (2016-05-09)

Description

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track.
Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.

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 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

The 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

读题读了好久。。。

大概题意就是问你 按照1.......n的顺序输入   能否输出你输入的那种序列

#include <stdio.h>
#include <stack>
using namespace std;
int main()
{
	int n;
	int num[1005];
	while(~scanf("%d",&n)&&n)
	{
		while(true)
		{
			int x;
			scanf("%d",&x);
			if(x==0)
			break;
			else
			{
				num[0]=x;
				for(int i=1;i<n;i++)
				scanf("%d",&num[i]);
			}
			stack<int>s;
			int t=0;
			for(int i=0;i<n;i++)
			{
				s.push(i+1);
				while(!s.empty()&&s.top()==num[t])
				{
					s.pop();
					t++;
				}
			}
			if(t==n)
			puts("Yes");
			else
			puts("No");
			while(!s.empty())
			s.pop();
		}
		printf("\n");
	}
	return 0;
}
时间: 2024-08-03 12:16:48

poj1363 Rails(栈模拟)的相关文章

POJ1363 Rails 验证出栈序列问题(转)

题目地址: http://poj.org/problem?id=1363 此题只需验证是否为合法的出栈序列. 有两个思路:1.每个已出栈之后的数且小于此数的数都必须按降序排列.复杂度O(n^2),适合人脑. //思路 1 不对!!! 例如 数据 ,               3 5 2 4 1              --------                正确答案为 no 2.另一个思路就是直接模拟入栈出栈过程.虽然模拟毫无技巧可言,但复杂度O(n),优于算法1.适合电脑. 代码如

UVA514 - Rails(栈)

题目:UVA514 - Rails(栈) 题目大意:某城市有一个火车站,铁轨成Y字形,有n节车厢从A方向驶入车站,按进站顺序编号1-n,现在给你一个序列代表进入B的顺序,你可以借助一个C中转站,问能否可以按这样的序列驶入B. 解题思路:栈模拟,中转站就代表栈,将车厢入栈后,只能从栈顶一个一个出去.注意输出每个cas都有一个空行. 代码: #include <cstdio> #include <cstring> #include <stack> using namespa

poj1363Rails(栈模拟)

题目链接: 啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟....我最开始觉得难处理是当出栈后top指针变化了..当不满足条件时入栈的当前位置怎么办,这时候想到用一个Copy数组保持入栈记录即可..当满足所有的火车都出栈时或者已经没有火车可以进栈了,那么久跳出..最后判断 是否出栈的火车是否达到n... 题目: Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24940   Accepted: 9771

hdu1515 dfs栈模拟

Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1513    Accepted Submission(s): 690 Problem Description How can anagrams result from sequences of stack operations? There are tw

用两个栈模拟实现一个队列

题目:如何用两个栈模拟实现一个队列?  如果这两个堆栈的容量分别是m和n(m>n),你的方法能保证的队列容量是多少?(这里讨论的是顺序栈,如果是链式栈的话完全没有必要考虑空间) 分析:栈的特点是“后进先出(LIFO)”,而队列的特点是“先进先出(FIFO)”.用两个栈模拟实现一个队列的基本思路是:用一个栈作为存储空间,另一个栈作为输出缓冲区,把元素按顺序压入两栈(模拟的队列),并按此顺序出队并输出即可. 如下图,用容量为m的栈作为存储空间,容量为n的栈作为输出缓冲区,一开始先将n个元素压入(pu

ACM学习历程——UVA 127 &quot;Accordian&quot; Patience(栈;模拟)

Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patience, the rules for which are as follows: Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate n

HDOJ 4699 Editor 栈 模拟

用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1913    Accepted Submission(s): 591 Problem Description Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2 Sample Output 2 3 Hint The fol

web前端面试系列 - 数据结构(两个栈模拟一个队列)

一. 用两个栈模拟一个队列 思路一: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时将数据压人s1 3. 出队时将s1弹出,并压人s2,然后弹出s2中的顶部数据,最后再将剩余数据弹出s2,并压人s1. 思路二: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时,判断s1, 是否为空,如果不为空,则将数据直接压入s1, 如果为空,则将s2中的数据全部倒入s1,在将数据压人s1. 3. 出队时,判断s2, 是否为空,如果不为空,则直接弹出s2

自定义栈的实现及使用两个栈模拟队列

一,使用单链表实现栈 ①栈需要一个栈顶指针 ②栈的基本操作有出栈和入栈,以及判断栈是否为空 ③单链表中每个结点表示一个栈元素,每个结点有指向下一个结点的指针.因此,在栈内部需要实现一个单链表.代码如下: public class Stack<T extends Comparable<? super T>>{ private class Node{ T ele; Node next; public Node(T ele) { this.ele = ele; } } Node top;