您的位置:首页 > 理论基础 > 数据结构算法

清华数据结构 PA题目--列车调度(Train)

2015-11-06 11:31 302 查看
列车调度(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

95分代码

#include <cstdio>
#include <cstring>
int out[1600010];
char print[3200010][5];
int stack[1600010];
int sHead = 0;

void push( int n )
{
stack[++sHead] = n;
}

int top()
{
return stack[sHead];
}

int pop()
{
return stack[sHead--];
}

int main()
{
int m, n;
int op = 0;
scanf( "%d%d", &n, &m );

int i;
for (i = 1; i <= n; i++)
scanf( "%d", &out[i] );
int j = 1;
i = 1;
while (i <= n)
{
if (j <= n && sHead <= m && out[i] >= j)
{
push( j++ );
strcpy( print[op++], "push" );
}
else
{
if (out[i] == top())
{
pop();
strcpy( print[op++], "pop" );
i++;
}
else
{
puts( "No" );
return 0;
}
}
}

for (int k = 0; k < op; k++)
{
puts( print[k] );
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构