2016HUAS暑假集训训练题 E - Rails
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 file 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 file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains ‘Yes’ if it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains ‘No’. In addition, there is one empty line after the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last “null” block of the input file.
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
分析:
本题的中转战符合先进后出的原则和栈十分像 因此可以用栈的方法写
ac代码:
#include <iostream>
#include <stack>
using namespace std;
const int Main = +;
int n,t[Main];
int main()
{
int ii = ;
while(cin>>n )
{ if(n == ) break; stack<int> s; while() //由于可以不断输入测试数据
{ int a = , b = ; int ok = ; cin>>t[];if(t[] == ) break;
for(int i = ; i <= n; i++)
{
cin>>t[i]; } while(b<=n)
{
if(a == t[b]) {a++;b++;} //一进一出
else if(!s.empty()&&s.top() == t[b]) {s.pop();b++;} //出栈
else if(a<=n) s.push(a++); //入栈
else {ok = ; break;}
} if(ok) {cout<<"Yes"<<endl;ok = ;}
else {cout<<"No"<<endl;ok = ;}
}
cout<<endl;
}
return ;
}
2016HUAS暑假集训训练题 E - Rails的更多相关文章
- 2016huas暑假集训训练题 G-Who's in the Middle
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/G 此题大意是给定一个数n 然后有n个数 要求求出其中位数 刚开始以为是按数学中的 ...
- 2016HUAS暑假集训训练题 G - Oil Deposits
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- 2016HUAS暑假集训训练题 F - 简单计算器
Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运 ...
- 2016HUAS暑假集训训练题 B - Catch That Cow
B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...
- 2016HUAS暑假集训训练题 D - Find a way
F ...
- 2016HUAS暑假集训训练2 O - Can you find it?
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/O 这道题是一道典型二分搜素题,题意是给定3个数组 每个数组的数有m个 再给定l个s ...
- 2016HUAS暑假集训训练2 L - Points on Cycle
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/L 这是一道很有意思的题,就是给定一个以原点为圆心的圆,然后给定 一个点 求最大三 ...
- 2016HUAS暑假集训训练2 K - Hero
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/K 这也是一道贪心题,刚开始写时以为只要对每一敌人的攻击和血的乘积进行从小到大排序即 ...
- 2016HUAS暑假集训训练2 J - 今年暑假不AC
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/J 此题要求是计算能够看到最多的节目 ,贪心算法即可,首先对结束时间排序,然后在把开 ...
随机推荐
- thinkphp几个表的数据合并,并用数组分页
控制器: //金币扣除 public function jbkc(){ $map['UG_dataType']= 'xtkc'; $list1 = M ( 'userget' )->where ...
- 防止ViewPager和Fragment结合使用时候的数据预加载
不知道你们使用ViewPager和Fragment结合的时候发现一个问题没,如果你的每个Fragment都需要请求网络数据,并且你在请求网络数据的时候会加入进度对话框的加载显示效果,当你显示第一个Fr ...
- C#多线程编程总结
VS2008.C#3.0在WinForm开发中,我们通常不希望当窗体上点了某个按钮执行某个业务的时候,窗体就被卡死了,直到该业务执行完毕后才缓过来.一个最直接的方法便是使用多线程.多线程编程的方式在W ...
- react-基础(1)
props 创建组件 React.createClass; 直接继承React.Component;与上面不同的是初始化props和state的方法: export class Counter ext ...
- Smart原则
遵循smart原则,必须是具体的.可衡量的.可达到的.与岗位职责相关的.有明确达成期限的.
- [技术学习]js继承
今天又看了一遍js的面向对象方面的知识,重点看了继承相关内容,已经记不得看了第几次这个内容,终于觉得自己好像懂了,特记录下来过程. js面向对象继承分为两大类,主要分为对象继承和非对象继承(拷贝继承) ...
- 【主席树】【bzoj2161】[hdu4348]
#include<cstdio> #include<algorithm> #include<cstring> #define N 400000 using name ...
- http://blog.csdn.net/u010246789/article/details/52539576
http://blog.csdn.net/u010246789/article/details/52539576
- [转]OpenVPN 安装与配置
一.服务器端安装及配置 服务器环境:干净的CentOS6.3 64位系统 内网IP:10.143.80.116 外网IP:203.195.xxx.xxx OpenVPN版本:OpenVPN 2.3.2 ...
- 纯CSS打造银色MacBook Air(完整版)
上一篇:<selection伪元素小解> ::selection{ background:blue; color:red; }p{font-size:14px;} 作者主页:myvin 博 ...