<Sicily>1001. Rails

2022-06-23 11:31:57 浏览数 (1)

原题如下

1001. Rails Time Limit: 1sec Memory Limit:32MB

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 Copy

代码语言:javascript复制
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

Sample Output Copy

代码语言:javascript复制
Yes
No

Yes

思路

简单点说就是车厢从右边过来,进入Station,并且必须排队出来,也就是说,如果有几个车厢在站内,则后进的车厢先出站。 这后进先出正好符合栈的特点,可以考虑使用栈解决。 要判断一个序列是否有可能,只需要看是否能够找到一个进站出站的方案满足这个序列,即只需要测试是否能够由输入的序列经过入栈出栈的操作,得到该结果序列。 将待判断的结果序列放到一个vector中(相当于数组),将站内停靠的序号放到stack中。 (1) 当站内为空时,则必须入栈,此时如果栈顶与序列的第一个数相同时,则出栈,并将vector一个元素删去,表示已匹配; (2) 若不匹配,则继续入栈,直到匹配为止,当出栈时还必须考虑出栈之后的栈顶是不是能够跟vector的一个元素匹配。 (3) 重复(1)(2)的操作。如果最终vector的元素为空,即所以的元素都能被匹配上,则说明这个序列是可能的。如果所有车厢号都已经入栈了,且栈顶无法与vector匹配,且vector还没被匹配完,则说明此序列不可能,因为找不到一个方案可以得出这个序列。

代码

代码语言:javascript复制
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int main(){
	int n;
	cin >> n;
	while(n > 0){
		int a;
		cin >> a;
		if(a == 0) {
			cout << endl;
			cin >> n;
			continue;
		}
		vector<int> num;
		num.push_back(a);
		for(int i = 1; i < n; i  ){
			int input;
			cin >> input;
			num.push_back(input);
		}
		int cur = 1;
		stack<int> s;
		/*******以下为关键代码********/
		while(!num.empty()){
			if(cur > n){
				cout << "No" << endl;
				break;
			} 
			s.push(cur);
			cur  = 1;
			while(!s.empty() && s.top() == num.front()){
				s.pop();
				num.erase(num.begin());
			}
		}
		if(num.empty()) cout << "Yes" << endl;
	}
}

0 人点赞