POJ-----1363栈的模拟

Rails
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 31397 Accepted: 12199

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


这个题刚开始没看懂,他这个数据不符合我的常规思虑,关键还是英语,生无可恋.jpg------

就是以第一组数据为例,第一个数5,代表5辆火车(或者5节,无所谓啦)从1到5顺序排列,题意大致就是有一个类似死胡同的拐角,只能正着进去倒着出来,先进后出,就是模拟栈--然后下面的每一排12345,54123就是问能不能通过这种模拟栈的操作以这个顺序过去这个拐角,就像数学上 ,,,下列排序错误的是,,,这种问题一样,最后,遇到0退出

一开始以为12345是顺序,问根据这个能不能排出54123的顺序,但是这样理解,看到下面那个654321,排成0就懵逼了,再次,生无可恋.jpg

看懂了就没问题了


#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
int ss[2500];
int main(){
	int a, b, c, t,p;
	while(scanf("%d", &a) == 1, a){//a是火车数量以及顺序,火车从1到a顺序排列
		while(scanf("%d", &ss[1])){
			if(ss[1] == 0){//为0退出
				printf("\n");
				break;
			}
			stack <int> sta;
			for(int i = 2; i <= a; i++){//选项给出让判断的顺序
				scanf("%d", &ss[i]);
			}
			int s, e, ok;
			s = e = ok = 1;
			while(e <= a){
				if(s == ss[e]){//给出的某个顺序和火车没进拐角时顺序一样,火车就不用进拐角,直接过去
					e++;
					s++;
				}
				else if(!sta.empty() && sta.top() == ss[e]){//栈不为空,也就是拐角里有车,最后进拐角(栈)的车,就是栈顶元素,输出
					sta.pop();
					e++;
				}
				else if(s <= a){//还没有检测完,就进栈
					sta.push(s);
					s++;
				}
				else{
					ok = 0;
					break;
				}
			}
			printf(ok ? "Yes\n" : "No\n");
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值