W1——Rail Problem

问题描述

在这里插入图片描述

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

Code

/*
 * @Description: Rail
 * @version:
 * @Author:
 * @Date: 2021-03-30 20:19:51
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-03-30 21:08:08
 */
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main(void)
{
	int n;
	cin >> n;
	if (n == 0)
		return 0;
	bool first = true;
	while (true)
	{
		if (!first)
			cout << endl;
		while (true)
		{
			bool judge = true;
			vector<int> source, target;
			stack<int> s;
			for (int i = 0; i < n; i++)
			{
				source.push_back(i + 1);
				int num;
				cin >> num;
				if (num == 0 && i == 0)
					goto out;
				target.push_back(num);
			}
			int index = 0;
			for (int i = 0; i < n; i++)
			{
				if (target[index] == source[i])
				{
					index++;
					continue;
				}
				else
					s.push(source[i]);
			}
			while (!s.empty())
			{
				if (target[index] == s.top())
				{
					s.pop();
					index++;
				}
				else
				{
					judge = false;
					break;
				}
			}
			if (judge)
				cout << "Yes" << endl;
			else
				cout << "No" << endl;
		}
	out:
		cin >> n;
		first = false;
		if (n == 0)
			break;
	}
    system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian圣楠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值