题目

链接:https://ac.nowcoder.com/acm/contest/28537/D
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

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.

输入描述:

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

输出

Yes
No Yes

题解

这道题目其实不难,重点是输入以及输出有大大的问题

代码

#include <iostream>
#include <stack>
using namespace std;
int s[1005];
int n = 0;
bool check()
{
int j = 0;
stack<int> a;
for (int i = 1; i <= n; i++)
{
a.push(i);
while (!a.empty() && a.top() == s[j])
{
a.pop();
j++;
}
}
if (a.empty())
{
return true;
}
else
return false;
}
int main()
{
int flags = 0;
int buf = 0;
while (1)
{
cin >> buf;
if (!buf)
break;
else
{
n = buf;
if (flags)
cout << endl;
} while (1)
{
cin >> buf;
if (buf)
{
s[0] = buf;
for (int i = 1; i < n; i++)
cin >> s[i];
flags = 1;
if (check())
cout << "Yes" << endl;
else
cout << "No" << endl;
}
else
break;
}
}
return 0;
}

最新文章

  1. CentOS7 编译安装 Mongodb (实测 笔记 Centos 7.0 + Mongodb 2.6.6)
  2. JVM原理讲解和调优
  3. 路由跟踪语法:tracert www.love125.com
  4. 在学Go语言
  5. iOS开发常用的第三方类库
  6. 保存带有emoji的文本报错解决方案
  7. WPF 之 布局(一)
  8. SpringMVC中注解和非注解方式下的映射器和适配器总结
  9. 【原创】QT编程 多线程
  10. 【转】Java 多线程(四) 多线程访问成员变量与局部变量
  11. 兔子--Fragment与ViewPager要切换滑动效果
  12. 物联网框架ServerSuperIO在.NetCore实现跨平台的实践路线
  13. JavaScript 对图像进行(追加,插入,替换,删除)
  14. 大数据实操3 - hadoop集群添加新节点
  15. 使用字面量,比new更加有效
  16. java之LinkedList详细介绍
  17. Python Algorithms – chapter2 基础知识
  18. Imageview 按比例适应屏幕大小
  19. Megcup2017 Dogfood
  20. VC调用DLL

热门文章

  1. 力扣算法JS LC 59-螺旋矩阵2,LC 152-乘积最大子数组
  2. 运维:ITSM
  3. 在MySQL中保存Java对象
  4. 爬取豆瓣喜剧类热门TOP60的电影
  5. [C++STL] vector 容器的入门
  6. UNION 与 UNION ALL 的区别
  7. 最强肉坦:RUST多线程
  8. JAVA - ArrayList是否会越界?
  9. 使用pip安装库或执行pip命令时报错解决方案
  10. Linux文件拷贝脚本