清华学堂 列车调度(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

感觉很简单的题目,就是难以下手,还是自己不行,加油吧!

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int MAX_SIZE = 1600000 + 10;
 7 int Stack1[MAX_SIZE], Stack2[MAX_SIZE];
 8 int out[MAX_SIZE];
 9 int pointer = 0;
10
11 void push(int a)
12 {
13     pointer++;
14     Stack1[pointer] = a;
15     Stack2[pointer] = a;
16 }
17
18 void pop()
19 {
20     pointer--;
21 }
22
23 int top1()
24 {
25     return Stack1[pointer];
26 }
27
28 int top2()
29 {
30     return Stack2[pointer];
31 }
32
33 int main()
34 {
35     int n, m;
36     scanf("%d %d", &n, &m);
37
38     for(int i = 1; i <= n; ++i)
39     {
40         scanf("%d", &out[i]);
41     }
42
43     int j = 0;
44     for(int i = 1; i <= n; ++i)
45     {
46         ///3个if 1个while 就模拟了栈混洗过程
47         if(out[i] < top1())
48         {
49             printf("No");
50             return 0;
51         }
52
53         while(j < out[i])
54         {
55             push(++j);
56             printf("push(%d)\n", j);
57         }
58
59         if(m < pointer)
60         {
61             printf("No");
62             return 0;
63         }
64
65         if(out[i] == top1())
66         {
67             printf("pop\n");
68             pop();
69         }
70     }
71
72     j = 0;
73     for(int i = 1; i <= n; ++i)
74     {
75         while(j < out[i])
76         {
77             push(++j);
78             puts("push");
79         }
80
81         if(out[i] == top2())
82         {
83             pop();
84             puts("pop");
85         }
86     }
87     return 0;
88 }

时间: 2024-08-05 19:30:04

清华学堂 列车调度(Train)的相关文章

L2-014. 列车调度

火车站的列车调度铁轨的结构如下图所示. Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道进入,最后从出口离开.在图中有9趟列车,在入口处按照{8,4,2,5,3,9,1,6,7}的顺序排队等待进入.如果要求它们必须按序号递减的顺序从出口离开,则至少需要多少条平行铁轨用于调度? 输入格式: 输入第一行给出一个整数N (2 <= N <= 105),下一行给出从1到N的整数序号的一个重排列.数字间以空格

车厢调度(train.cpp)

车厢调度(train.cpp) [问题描述]        有一个火车站,铁路如图所示,每辆火车从A驶入,再从B方向驶出,同时它的车厢可以重新组合.假设从A方向驶来的火车有n节(n<=1000),分别按照顺序编号为1,2,3,…,n.假定在进入车站前,每节车厢之间都不是连着的,并且它们可以自行移动到B处的铁轨上.另外假定车站C可以停放任意多节车厢.但是一旦进入车站C,它就不能再回到A方向的铁轨上了,并且一旦当它进入B方向的铁轨,它就不能再回到车站C. 负责车厢调度的工作人员需要知道能否使它以a1

【PAT L2-014】列车调度(Dilworth定理)

[PAT L2-014]列车调度(Dilworth定理) L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示. Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道进入,最后从出口离开.在图中有9趟列车,在入口处按照{8,4,2,5,3,9,1,6,7}的顺序排队等待进入.如

列车调度

火车站的列车调度铁轨的结构如下图所示. Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道进入,最后从出口离开.在图中有9趟列车,在入口处按照{8,4,2,5,3,9,1,6,7}的顺序排队等待进入.如果要求它们必须按序号递减的顺序从出口离开,则至少需要多少条平行铁轨用于调度? 输入格式: 输入第一行给出一个整数N (2 <= N <= 105),下一行给出从1到N的整数序号的一个重排列.数字间以空格

pat l2-14 列车调度 dilworth+nlog(n)最长上升子序列

关于dilworth定理 这里引用一个大神的(http://blog.csdn.net/xuzengqiang/article/details/7266034) 偏序的概念: 设A是一个非空集,P是A上的一个关系,若关系P是自反的.反对称的.和传递的,则称P是集合A上的偏序关系.即P适合下列条件:(1)对任意的a∈A,(a,a)∈P;(2)若(a,b)∈P且(b,a)∈P,则a=b;(3)若(a,b)∈P,(b,c)∈P,则(a,c)∈P,则称P是A上的一个偏序关系.带偏序关系的集合A称为偏序集

7-2 列车调度 (25 分)

题目: 样例输入: 98 4 2 5 3 9 1 6 7 样例输出: 4 思路: 要想得到最少的调度序列,那就要找出最少的下降序列的个数.拿上边的例子来说:有如下四个下降序列 8 4 2 1 5 3 9 6 7 所以只需要四个调度队列就可以了. 又根据定理:最小的下降序列的个数等于最长上升子序列的长度.(这个定理证明没看懂,直接懵逼,菜是原罪啊!!)剩下的就是一个裸的最长上升子序列问题了. 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f

清华学堂 LightHouse

灯塔(LightHouse) Description As shown in the following figure, If another lighthouse is in gray area, they can beacon each other. For example, in following figure, (B, R) is a pair of lighthouse which can beacon each other, while (B, G), (R, G) are NOT

清华学堂 Range

Descriptioin Let S be a set of n integral points on the x-axis. For each given interval [a, b], you are asked to count the points lying inside. Input The first line contains two integers: n (size of S) and m (the number of queries). The second line e

ACM/ICPC 之 用双向链表 or 模拟栈 解“栈混洗”问题-火车调度(Tshing Hua OJ - 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.