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 此题要求是计算能够看到最多的节目 ,贪心算法即可,首先对结束时间排序,然后在把开 ...
随机推荐
- filter应用案例四:页面静态化
1 说明 你到"当当"搜索Java分类图书时,"当当"会去查询数据库!每天都有很多人去搜索"Java分类"的图书,每次都去访问数据库,这会有 ...
- Portlet简述
一.Portlet是什么? Portlet是基于java的web组件,由portlet容器管理,并由容器处理请求,生产动态内容.Portals使用portlets作为可插拔用户接口组件,提供信息系统的 ...
- Liferay 6.2 改造系列之二:清理不需要的Portlet
一.特殊Portlet: 以下Portlet数据特殊用途的Portlet,去除后会出现运行错误: 1.站点模版 通过com.liferay.portal.events.AddDefaultLayout ...
- BootSrap学习
1.当row1里边有2个或多个col的时候,如果位置在后边的col的高度大于位置在前边的col,就可能会导致下一个row2在满足col-xs-*在满足和为12的情况下错乱分行.解决办法是强行给row1 ...
- canvas 3D运动球效果 多球
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- ZOJ3229 Shoot the Bullet(有源汇流量有上下界网络的最大流)
题目大概说在n天里给m个女孩拍照,每个女孩至少要拍Gi张照片,每一天最多拍Dk张相片且都有Ck个拍照目标,每一个目标拍照的张数要在[Lki, Rki]范围内,问最多能拍几张照片. 源点-天-女孩-汇点 ...
- How to retrieve instance parameters from an uninstantiated (uninserted) family
The trick to be able to read the default values for instance parameters is to get to the FamilyManag ...
- iOS学习36数据处理之SQLite数据库
1. 数据库管理系统 1> SQL语言概述 SQL: SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集, 是一种功能齐全的 ...
- spark-sql访问hive的问题记录
好久没有弄博客了... hive0.14 spark0.12 [hadoop@irs bin]$ ./spark-sql Spark assembly has been built with Hive ...
- 常用JS正则表达式
常用JS正则表达式 收集一些常用的JavaScript正则表达式匹配规则,比如匹配电话号码.Email.中文字符.身份证号.邮编.QQ号.过滤空白行.匹配特定数字等.觉得这玩意是很有用的,只不过自己水 ...