列车调度(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. Wifi代替数据线调试

    我们通常都是使用USB线连接设备和电脑进行的程序调试. 有的程序本身就要使用USB口,例如一个文件浏览器,它可以列出通过USB接口接入的U盘. 在调试这样的应用的时候,USB接口被占用了,但设备也只有 ...

  2. System类和Random类

    System类 成员方法: public static void gc():运行垃圾回收器 public static void exit(int status):退出垃圾回收器 public sta ...

  3. Django基础,Day9 - 静态文件目录与路径设置说明(eg. images, JavaScript, CSS)

    静态文件路径设置官方说明 1. Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS. 2. In ...

  4. Npoi导入导出Excel操作

    之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...

  5. JVM Management API

    JVM本身提供了一组管理的API,通过该API,我们可以获取得到JVM内部主要运行信息,包括内存各代的数据.JVM当前所有线程及其栈相关信 息等等.各种JDK自带的剖析工具,包括jps.jstack. ...

  6. [NHibernate]O/R Mapping基础

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) 引言 对象和关系数据库 ...

  7. javascript parseJSON

    解析json: 前台和后台做ajax交互,后台返回的json字符串,我之前都是通过eval来解析,后来慢慢的知道eval这货是魔鬼,eval要尽量避免,是出于安全考虑,因为eval过于强大,他可以把s ...

  8. UVA-11997 K Smallest Sums

    UVA - 11997 K Smallest Sums Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & ...

  9. Mac Pro 编译安装 Redis-3.2.3

    Redis官方下载地址:http://redis.io/download Redis安装 cd /usr/local/src/redis-3.2.3 sudo make sudo make insta ...

  10. PYTHON 写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容。

    def shifou_space(args): ret = True for a in args: if a.isspace(): ret = False break return ret resul ...