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 此题要求是计算能够看到最多的节目 ,贪心算法即可,首先对结束时间排序,然后在把开 ...
随机推荐
- Action处理请求参数
第一种 :Action 本身作为model对象,通过成员setter封装 (属性驱动 ) 第一种方式:<br> <form action="${pageContext.re ...
- python运算符和表达式
算术运算符: 比较运算符: 赋值运算符: 位运算符: 逻辑运算符: 身份运算符: 成员运算符: 运算符优先级:
- 《DSP using MATLAB》示例Example4.15
代码: b = [1/3, 1/3, 1/3]; a = [1, -0.95, 0.9025]; % x(n) y(n) coefficient [R, p, C] = residuez(b,a) M ...
- configSections
由于最近一个项目的数据库变动比较频繁, 为了减少数据层的负担, 打算采用.net的MVC框架, 使用LINQ对付数据层. 这个框架的web.config文件里出现了configS ...
- ajax 异步交互
<script> $(function(){ $("#sub").click(function () { $.ajax( ...
- js 数组(Array)
一.数组 稠密数组(非稀疏数组) 稀疏数组 二.数组的添加和删除 我们已经常见过添加数组元素最简单的方法:为新索引赋值;也可以使用push()方法在数组末尾增加一个或者多个元素: a = []; a. ...
- linux查看和修改当前系统时间
一.查看和修改Linux的时区 1. 查看当前时区 命令 : "date -R" 2. 修改设置Linux服务器时区 方法 A 命令 : "tzselect" ...
- ural 1249. Ancient Necropolis
1249. Ancient Necropolis Time limit: 5.0 secondMemory limit: 4 MB Aerophotography data provide a bit ...
- ural 1343. Fairy Tale
1343. Fairy Tale Time limit: 1.0 secondMemory limit: 64 MB 12 months to sing and dance in a ring the ...
- 20145304 Java第六周学习报告
20145304<Java程序设计>第六周学习总结 教材学习内容总结 1.InputStream与OutputStream: 在Java中,输入串流的代表对象为java.io.InputS ...