Sicily 1509Rail
来源:互联网 发布:中国阶级 知乎 编辑:程序博客网 时间:2024/06/04 19:04
题目
1509. Rails
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
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.
Output
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.
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
Problem Source
Central Europe 1997
//////////////////////////////////////////////////////////////////////////////
思路
对3个火车和4个火车在纸上分析不难发现规律:若输入的序列任意一个数,它后面的且比它小的数字 是降序排列的话,这个序列就可以由顺序入栈出栈构成。 因为一个大的数字若先出栈,表示后面的要更早入栈,那较小的数字组入栈后是升序, 再出栈时就只能是降序。 如(5 4 1 2 3),4后面比它小的是(1 2 3),那么说明4出栈前123要先顺序进栈 导致4出栈后最早出来的只能是3
当然也可以用模拟,直接模拟过程,遇到问题就表示是NO
但是写起来可能比较麻烦
这样直接对每一个数字进行检测,若它后面比它小的数字是降序排列的话,就是YES。
这样写时间复杂度大概是O(n^2),有点慢
例如(…,i,i-1,…),考虑若对一个较大的数字i检测后,再检测它后面的比它小的数字就有重复的部分
如果i检测失败,就直接结束了,如果成功,那说明它后面所有的小的数字都是降序排列
当然也包括接下来要去检验的i-1,所以用一个变量key保存当前检测过的最大的数,因为是顺序检测
那后面的比key小的就可以直接跳过了
PS:写完后提交的版本把“Yes”写成了“YES”,no也一样,思考了很久也不知道为什么WA…
总之多谢后世人,戒之慎勿忘
代码
#include <iostream>using namespace std;int main() { int n,out[1001];//out用来保存输入的出栈顺序的数组 while (cin >> n) { if (!n) break; bool over = false, succ = true;//over表示对某一个数字的检测是否结束 //succ表示整个序列的合法性 while (!over) { succ = true; for (int i = 1; i <= n; i++) { cin >> out[i]; if (!out[i]) { cout << endl; over = true; break; } } if (over) break; int key=out[1], pre, pos;//pre是先前的一个小数,pos是当前的,key是迄今检测过得最大的数 for (int i = 1; i <= n; i++) { if (out[i] >= key) { key = out[i]; key = pre = pos = out[i]; for (int j = i; j <= n; j++) { if (out[j] < key) { pre = pos; pos = out[j]; } if (pos > pre) { succ = false; break; } } } if (!succ) break; } if (succ) cout << "Yes\n"; else cout << "No\n"; } }}
- Sicily 1509Rail
- 什么是 rail-to-rail运算放大器?
- 运放【rail to rail】
- OPA343 轨至轨(rail to rail) 运放
- 轨对轨运放(rail-to-rail)
- 轨对轨运放(rail-to-rail)
- 轨对轨运放(rail-to-rail) .
- sicily 1509 Rails
- vim on rail
- POJ 1363 : Rail - 栈
- Rail station(栈)
- UVA 514 Rail
- 硬件设计基础:什么是 rail-to-rail运算放大器?
- part-13 轨至轨输入(Rail to Rail)
- part-22 轨至轨输出(rail to rail)
- Ruby on Rail学习开始
- 轻轨列车 light rail train
- rail创建增删改功能
- iOS 10获取当前定位信息,根据位置获取天气
- Neural Machine translation中的Attention机制
- 欢迎使用CSDN-markdown编辑器
- static inline
- caffe学习资源汇总
- Sicily 1509Rail
- leetCode练习(69)
- 三种硬件平台运行Laxcus大数据系统的表现
- Android开发中Scrollview嵌套recyclerview时,recyclerview不显示数据
- Android进阶之解决RecyclerView notifyItem闪屏问题
- 读《程序员思维修炼》总结和读后感
- JFrame类中的setContentPane()和getContentPane()的使用方法
- 关于C语言中的Sleep函数
- 数组 排序 可变参数 面向对象