清华学堂 列车调度(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 ...
随机推荐
- Angular-Chart.js 初接触;;;
可以先看下下面的链接,了解下, 推荐链接 准备工作 JS文件{angular.js.Chart.js.angular-chart.js} 这3个文件我的获取难易程度:Chart.js > ang ...
- JavaScript getComputedStyle
我们使用 element.style 也可以获取元素的CSS样式声明对象,但是其与 getComputedStyle 方法还是有一些差异的. 首先,element.style 是可读可写的,而 get ...
- JSP动作元素之useBean、setProperty、getProperty指令
简介 这三个指令都是与JavaBean相关的指令,其中useBean指令用于在JSP页面中初始化一个Java实例:setProperty指令用于为JavaBean实例的属性设置值:getPropert ...
- KEGG数据库的使用方法与介绍
KEGG数据库的使用方法与介绍 KEGG的数据 KEGG中的pathway是根据相关知识手绘的,这里的手绘的意思可能是指人工以特定的语言格式来确定通路各组件的联系:基因组信息主要是从NCBI等数据库中 ...
- 如何修改 Total commander 配置文件的路径
在官方主页 http://www.ghisler.com/tools.htm#other 上提供了一个名为 INIreloc.exe 的小程序,它可以变更ini文件的保存位置.
- Alpha版本冲刺总结——曙光初现
No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 项目预期计划 界面设计 androi ...
- linux常用命令-帮助命令man,whatis,apropos,info,help
man 命令 man 配置文件,注意这里只需要写文件名称就可以了,不能写文件的绝对路径 man既可以查看命令的帮助信息也可以查看配置文件的帮助信息,如果内容太多,可以输入"/内容" ...
- redmine问题集锦
当我新建LDAP认证模式时,遇到如下错误:
- Python基本数据类型
一.整数 int(整形) 在32位机器上,整数的位数为32位,取值范围为-2**31~2**1-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值范围为-2* ...
- poj 3038
http://poj.org/problem?id=3038 这个题我是在一个关于并查集的博客中找到的,结果我就觉得这个应该是个贪心,真想不出这个与并查集有什么鬼关系,看discuss里面也都是贪心, ...