当前位置:网站首页><Sicily>1001. Rails
<Sicily>1001. Rails
2022-06-23 12:01:00 【梦飞】
原题如下
1001. Rails Time Limit: 1sec Memory Limit:32MB
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 Copy
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0Sample Output Copy
Yes
No
Yes思路
简单点说就是车厢从右边过来,进入Station,并且必须排队出来,也就是说,如果有几个车厢在站内,则后进的车厢先出站。 这后进先出正好符合栈的特点,可以考虑使用栈解决。 要判断一个序列是否有可能,只需要看是否能够找到一个进站出站的方案满足这个序列,即只需要测试是否能够由输入的序列经过入栈出栈的操作,得到该结果序列。 将待判断的结果序列放到一个vector中(相当于数组),将站内停靠的序号放到stack中。 (1) 当站内为空时,则必须入栈,此时如果栈顶与序列的第一个数相同时,则出栈,并将vector一个元素删去,表示已匹配; (2) 若不匹配,则继续入栈,直到匹配为止,当出栈时还必须考虑出栈之后的栈顶是不是能够跟vector的一个元素匹配。 (3) 重复(1)(2)的操作。如果最终vector的元素为空,即所以的元素都能被匹配上,则说明这个序列是可能的。如果所有车厢号都已经入栈了,且栈顶无法与vector匹配,且vector还没被匹配完,则说明此序列不可能,因为找不到一个方案可以得出这个序列。
代码
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main(){
int n;
cin >> n;
while(n > 0){
int a;
cin >> a;
if(a == 0) {
cout << endl;
cin >> n;
continue;
}
vector<int> num;
num.push_back(a);
for(int i = 1; i < n; i++){
int input;
cin >> input;
num.push_back(input);
}
int cur = 1;
stack<int> s;
/*******以下为关键代码********/
while(!num.empty()){
if(cur > n){
cout << "No" << endl;
break;
}
s.push(cur);
cur += 1;
while(!s.empty() && s.top() == num.front()){
s.pop();
num.erase(num.begin());
}
}
if(num.empty()) cout << "Yes" << endl;
}
}边栏推荐
- Oversampling Series III: quantization error and oversampling rate
- Design of routing service for multi Activity Architecture Design
- [basic knowledge] - data bit width converter
- [no title] 2022 pressure pipeline patrol inspection and maintenance test questions and online simulation test
- QT5知识:QT绘制图形
- Easy to understand soft route brushing tutorial
- Oracle数据库的主导地位被云竞争对手逐渐侵蚀
- Qt5 knowledge: QT drawing graph
- 09 -- 回文对
- The median annual salary exceeds 300000, and the salary of the first AI major graduate of Nanjing University is exposed
猜你喜欢

Halcon原理:auto_threshold算子

Common fault analysis and Countermeasures of MySQL in go language

机器学习系列5:距离空间(1)

安装Rstudio Desktop和Rstudio Server免费版本

Leetcode 1209. 删除字符串中的所有相邻重复项 II(牛逼,终于过了)

Use monotone stack to solve problems

Use xtradiagram Diagramcontrol for drawing and controlling process graphics

Meta said that the UK security law may "scan all private information" or infringe privacy

Redis 入门-第二篇-数据结构与对象-链表
![Halcon principle: one dimensional function_ 1D type [2]](/img/54/570c6e739be1ab9caa9df805965b57.png)
Halcon principle: one dimensional function_ 1D type [2]
随机推荐
【UVM入门 ===> Episode_7 】~ sequence、sequence item、sequencer、driver
[cloud native & microservice viii] source code analysis of weightedresponsetimerule of ribbon load balancing strategy (response time weighting)
Open classes are short videos! Tonight, I will teach you how to realize accurately!
开源之夏中选名单已公示,基础软件领域成为今年的热门申请
Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
Oracle database's dominant position is gradually eroded by cloud competitors
@Dark horse fans, haven't you received this "high temperature subsidy"?
国产化信息 | 爱可生与中科方德完成产品兼容互认证
Go 语言使用 MySQL 的常见故障分析和应对方法
链表5 - 234. 回文链表
The country has entered the main flood season. The Ministry of transport: the lines that do not meet the conditions for safe operation will be resolutely shut down!
记录
"Dream of children's travel" in 2022, GAC Honda children's road safety charity travel entered the Northeast
QT5知识:QT绘制图形
ROS knowledge: point cloud files PCD format
Redis 入门-第二篇-数据结构与对象-链表
Runtime application self-protection (rasp): self-cultivation of application security
Analysis of six dimensional chart: analysis of enterprise growth of CSCEC
Introduction to redis - Chapter 1 - data structures and objects - simple dynamic string (SDS)
HMS Core 视频编辑服务开放模板能力,助力用户一键Get同款酷炫视频