清华学堂 列车调度(Train)
列车调度(Train)
Description
Figure 1 shows the structure of a station for train dispatching.
Figure 1
In this station, A is the entrance for each train and B is the exit. S is the transfer end. All single tracks are one-way, which means that the train can enter the station from A to S, and pull out from S to B. Note that the overtaking is not allowed. Because the compartments can reside in S, the order that they pull out at B may differ from that they enter at A. However, because of the limited capacity of S, no more that m compartments can reside at S simultaneously.
Assume that a train consist of n compartments labeled {1, 2, …, n}. A dispatcher wants to know whether these compartments can pull out at B in the order of {a1, a2, …, an} (a sequence). If can, in what order he should operate it?
Input
Two lines:
1st line: two integers n and m;
2nd line: n integers separated by spaces, which is a permutation of {1, 2, …, n}. This is a compartment sequence that is to be judged regarding the feasibility.
Output
If the sequence is feasible, output the sequence. “Push” means one compartment goes from A to S, while “pop” means one compartment goes from S to B. Each operation takes up one line.
If the sequence is infeasible, output a “no”.
Example 1
Input
5 2
1 2 3 5 4
Output
push
pop
push
pop
push
pop
push
push
pop
pop
Example 2
Input
5 5
3 1 2 4 5
Output
No
Restrictions
1 <= n <= 1,600,000
0 <= m <= 1,600,000
Time: 2 sec
Memory: 256 MB
感觉很简单的题目,就是难以下手,还是自己不行,加油吧!
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std; const int MAX_SIZE = + ;
int Stack1[MAX_SIZE], Stack2[MAX_SIZE];
int out[MAX_SIZE];
int pointer = ; void push(int a)
{
pointer++;
Stack1[pointer] = a;
Stack2[pointer] = a;
} void pop()
{
pointer--;
} int top1()
{
return Stack1[pointer];
} int top2()
{
return Stack2[pointer];
} int main()
{
int n, m;
scanf("%d %d", &n, &m); for(int i = ; i <= n; ++i)
{
scanf("%d", &out[i]);
} int j = ;
for(int i = ; i <= n; ++i)
{
///3个if 1个while 就模拟了栈混洗过程
if(out[i] < top1())
{
printf("No");
return ;
} while(j < out[i])
{
push(++j);
printf("push(%d)\n", j);
} if(m < pointer)
{
printf("No");
return ;
} if(out[i] == top1())
{
printf("pop\n");
pop();
}
} j = ;
for(int i = ; i <= n; ++i)
{
while(j < out[i])
{
push(++j);
puts("push");
} if(out[i] == top2())
{
pop();
puts("pop");
}
}
return ;
}
清华学堂 列车调度(Train)的更多相关文章
- PAT L2-014 列车调度(最长上升nlogn)
火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道进入,最后从出口离开.在图中有 ...
- L2-014 列车调度 (25 分)
L2-014 列车调度 (25 分) 火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择 ...
- PAT L2-014 列车调度
https://pintia.cn/problem-sets/994805046380707840/problems/994805063166312448 火车站的列车调度铁轨的结构如下图所示. 两端 ...
- L2-014. 列车调度(set)*
L2-014. 列车调度 参考博客 #include <iostream> #include <cstdio> #include <set> #include &l ...
- PTA 7-2 列车调度(25 分)
7-2 列车调度(25 分) 火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道 ...
- L2-014. 列车调度
L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示. Figure ...
- 天梯赛 L2-014 列车调度 (模拟)
火车站的列车调度铁轨的结构如下图所示. Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道进入,最后从出口 ...
- 车厢调度(train.cpp)
车厢调度(train.cpp) [问题描述] 有一个火车站,铁路如图所示,每辆火车从A驶入,再从B方向驶出,同时它的车厢可以重新组合.假设从A方向驶来的火车有n节(n<=1000) ...
- L2-014. 列车调度(set的使用,最长递增子序列)
L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示. Figure ...
随机推荐
- Linux硬链接和软链接
1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...
- 暴力枚举N级子域名
#!/usr/bin/env python# -*- encoding: utf-8 -*-# A simple and fast sub domains brute tool for pentest ...
- [NHibernate]存储过程的使用(三)
目录 写在前面 文档与系列文章 查询 总结 写在前面 前面的文章介绍了在nhibernate中使用存储过程进行增删改的操作,当然查询也是可以的,在nhibernate中也可以执行任意的存储过程.本篇文 ...
- [UML]UML系列——类图class的关联关系(聚合、组合)
关联的概念 关联用来表示两个或多个类的对象之间的结构关系,它在代码中表现为一个类以属性的形式包含对另一个类的一个或多个对象的应用. 程序演示:关联关系(code/assocation) 假设:一个公司 ...
- javascript 高级程序设计 -有感
本来我想写一个高级程序设计总结的,结果发现我进入了一扇门,里面所有的字都要逐字逐句的理解,所有描述已经是非常精炼了,我最初的想法无异于老鼠吃大象. 我现在记录的是我在看这本时的感想. 2015.4月9 ...
- C#学习笔记
1.C#中[],List,Array,ArrayList的区别 [] 是针对特定类型.固定长度的. List 是针对特定类型.任意长度的. Array 是针对任意类型.固定长度的. ArrayList ...
- Android通过名称找图片
开发中往往会遇到这种情况:在Drawable文件夹中有若干张相似的图片(这里的相似指的是不仅图片名称相似,用途也相似),现在要根据用户的某个操作选出其中的一张.例如,在类似微信语音功能的开发中,按住“ ...
- PHP写文件函数
/** * 写文件函数 * * @param string $filename 文件名 * @param string $text 要写入的文本字符串 * @param string $openmod ...
- Linux tar (打包.压缩.解压缩)命令说明 | tar如何解压文件到指定的目录?
打包举例:将 /usr/local/src/zlib-1.2.5目录下的文件打包成 zlib-1.2.5.tar.gz cd /usr/local/src tar -czvf ./zlib-1.2.5 ...
- Java poi读取,写入Excel,处理row和cell可能为空的情况
首先需要导入包 import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.NP ...