UVa 514 Rails (water ver.)
来源:互联网 发布:2017中国网络文化产业 编辑:程序博客网 时间:2024/05/20 01:44
514 - Rails
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=455
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 coaches numbered in increasing order. 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 . 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 integerN described above. In each of the next lines of the block there is a permutation of 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 containsYes if it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it containsNo. 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
51 2 3 4 55 4 1 2 3066 5 4 3 2 100
Sample Output
YesNoYes
完整代码:
/*0.092s*/#include<bits/stdc++.h>using namespace std;stack<int> s;int g[1005];int main(){int n, i, now;while (scanf("%d", &n), n){while (scanf("%d", &g[0]), g[0]){while (!s.empty()) s.pop();for (i = 1; i < n; ++i) scanf("%d", &g[i]);now = 0;for (i = 1; i <= n; ++i){if (i == g[now]){++now;while (!s.empty()){if (s.top() == g[now]){s.pop();++now;}else break;}}else s.push(i);}puts(s.empty() ? "Yes" : "No");}putchar(10);}return 0;}
- UVa 514 Rails (water ver.)
- UVa 10082 WERTYU (water ver.)
- UVa 10696 f91 (water ver.)
- UVa 79 ClockHands (water ver.)
- UVa 11340 Newspaper (water ver.)
- UVa 11150 Cola (water ver.)
- UVa 10300 Ecological Premium (water ver.)
- UVa 414 Machined Surfaces (water ver.)
- UVa 490 Rotating Sentences (water ver.)
- UVa 445 Marvelous Mazes (water ver.)
- UVa 488 Triangle Wave (water ver.)
- UVa 10783 Odd Sum (water ver.)
- UVa 457 Linear Cellular Automata (water ver.)
- UVa 621 Secret Research (water ver.)
- UVa 11044 Searching for Nessy (water ver.)
- UVa 102 Ecological Bin Packing (water ver.)
- UVa 272 TEX Quotes (water ver.)
- UVa 10370 Above Average (water ver.)
- JavaScript世界的一等公民 - 函数
- .net创建XML文件的两种方法
- Android:如何使用addJavaScriptInterface
- JAVA集合框架图
- Android 实现书籍翻页效果----升级篇
- UVa 514 Rails (water ver.)
- DNN调试利器DNNDebug.aspx--如何调试出错信息不具体的程序错误
- 架构图收集
- 工作的方法、态度
- Qt项目编码详解
- DirectDraw读书笔记
- 从JAVA多线程理解到集群分布式和网络设计的浅析
- 云计算学习无忧:云平台技术架构与应用案例
- Oracle DataGuard物理备库的数据模式