列车调度(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)的更多相关文章

  1. PAT L2-014 列车调度(最长上升nlogn)

    火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道进入,最后从出口离开.在图中有 ...

  2. L2-014 列车调度 (25 分)

    L2-014 列车调度 (25 分)   火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择 ...

  3. PAT L2-014 列车调度

    https://pintia.cn/problem-sets/994805046380707840/problems/994805063166312448 火车站的列车调度铁轨的结构如下图所示. 两端 ...

  4. L2-014. 列车调度(set)*

    L2-014. 列车调度 参考博客 #include <iostream> #include <cstdio> #include <set> #include &l ...

  5. PTA 7-2 列车调度(25 分)

    7-2 列车调度(25 分) 火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道 ...

  6. L2-014. 列车调度

    L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示. Figure ...

  7. 天梯赛 L2-014 列车调度 (模拟)

    火车站的列车调度铁轨的结构如下图所示. Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道进入,最后从出口 ...

  8. 车厢调度(train.cpp)

    车厢调度(train.cpp) [问题描述]        有一个火车站,铁路如图所示,每辆火车从A驶入,再从B方向驶出,同时它的车厢可以重新组合.假设从A方向驶来的火车有n节(n<=1000) ...

  9. L2-014. 列车调度(set的使用,最长递增子序列)

    L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示. Figure ...

随机推荐

  1. vim编辑器的使用

    I 在光标所在行的行首插入 A 在光标所在行的行尾插入 { 移动到上一段 } 移动到下一段 空格向后移动一格 H 屏幕顶部 M 屏幕中间 L 屏幕下方 n| 使光标移动到第几个字符处 ngg 移动到制 ...

  2. SDL文字和图形

    SDL本身没有显示文字功能,它需要用扩展库SDL_ttf来显示文字.ttf是True Type Font的缩写,ttf是Windows下的缺省字体,它有美观,放大缩小不变形的优点,因此广泛应用很多场合 ...

  3. IO

    文件过滤 http://codego.net/9245/ C# 文件处理 http://wenku.baidu.com/link?url=yXKiIA_OZYR4MIynDgz-qhOnfJoCyOQ ...

  4. CSS3中的变形处理

    在css3中,可以利用transform功能来实现文字或者图像的旋转.缩放.倾斜.移动这四种类型的变形处理. 旋转 旋转功能使用rotate方法参数中加入角度值,方向为顺时针旋转.示例清单如下: &l ...

  5. Nginx下wordpress伪静态规则(rewrite)

    当我们从apache服务器转向Nginx服务器的时候,它们的伪静态规则就不一样了,所以你熟悉Nginx服务器的伪静态规则,自己写当然也好.但很多网友还是不太了解Nginx服务器的伪静态规则的,而如果你 ...

  6. maven install Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project web_nanchang

    maven打包成war时,报错:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default- ...

  7. 自定义cell自适应高度

    UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...

  8. 0、Web基本概念

    一.Web的概念: 本意是蜘蛛网和网的意思,在网页设计中我们称为网页的意思. 二.Web的分类:Internet上供外界访问的Web资源分为静态Web资源和动态Web资源两种. 1.静态Web资源:W ...

  9. Android高手速成--第三部分 优秀项目

    主要介绍那些Android还不错的完整项目,目前包含的项目主要依据是项目有意思或项目分层规范比较好.Linux项目地址:https://github.com/torvalds/linuxAndroid ...

  10. Linux的用户及用户组

    一./etc/group下存储当前系统中所有的用户组信息 -Group:   x   : 123  :abc,def,xyz -组名称:组密码占位符:组编号:组中用户名列表 二./etc/gshado ...