问题描述
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;
}