牛客竞赛算法入门题单打卡 F Rails


大致题意

利用一个栈使一个1,2,3...n的数组变为输入所要求的数组,若能实现则输出"Yes",不能则输出“No”。

题目输出要求

输入多组数据,每组数据一个n和若干个数组,每组数据由0来作为结尾,若n为0则退出,要求每组数据的输出之间用一个空行隔开,但最后一组数据后不能输出空行。

题目描述

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.

 

牛客竞赛算法入门题单打卡 F Rails_第1张图片



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.

输入描述:

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.

输出描述:

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.

示例1

输入

复制5 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 0

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

输出

复制Yes No Yes

Yes
No

Yes

思路

对于每个输入数组中的数字a[i],创立一个栈,若当前栈顶元素小于a[i],则使1,2...a[i]入栈,每次比较栈顶元素与a[i],若相同则栈顶元素出栈,若不同则说明当前数组不能实现,输出"No",若所有元素均与栈顶元素相同,则输出"Yes"

#include 
using namespace std;

int main() {
	int n, t;
	while (scanf("%d", &n) != EOF) {
		if (n == 0)
			break;
		else if (n != 0) {
			cout << endl;
		}
		while (scanf("%d", &t) != EOF) {
			if (t == 0) {
				break;
			}//以上为止全部用于实现题目的特殊输出要求
			vectora(n, 0);
			a[0] = t;
			int top = -1;
			for (int i = 1; i < n; i++) {//输入
				cin >> a[i];
			}
			vectorz(n, -1);//创建一个栈
			int flagg = 1, k = 0, i = 0;
			for ( i = 0; i < n; i++) {
				while (z[top] < a[i] || top == -1) {
					z[++top] = ++k;//入栈
				}
				if (z[top] == a[i]) {//相同则栈顶元素出栈
					top--;
				} else {//不同则输出No
					printf("No\n");
					break;
				}
			}
			if (i == n)
				printf("Yes\n");
		}
	}
}

你可能感兴趣的:(算法)