QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 1024 MB Puntuación total: 100

#23871. Merge

Estadísticas

問題文

長さ $N$ の正整数列 $A=(A_{1}, A_{2}, ..., A_{N})$ と,長さ $M$ の正整数列 $B=(B_{1}, B_{2}, ..., B_{M})$ が与えられる. これらの数列は,共に広義単調増加数列である.つまり,$A_{1} ≦ A_{2} ≦ … ≦ A_{N}$, $B_{1} ≦ B_{2} ≦ … ≦ B_{M}$ を満たす.

以下のアルゴリズムを用いて,これらの数列から,長さ $N+M$ の正整数列 $C=(C_{1}, C_{2}, ..., C_{N+M})$ を生成する.

  1. はじめ $C$ は空とする.
  2. $A$ と $B$ がどちらも空の場合,終了する.
  3. $A$ と $B$ のどちらかが空の場合,そうでない数列を $t$ とおく.どちらも空でない場合,先頭の要素が小さい数列を $t$ とおく.ただし,$A$ と $B$ の先頭の要素が同じ値のときは $A$ を $t$ とおく.
  4. $t$ の先頭の要素を $C$ の末尾に追加する.
  5. $t$ の先頭の要素を削除する.
  6. 2. に戻る.

広義単調増加な正整数列 $A$, $B$ が与えられたとき,このアルゴリズムにより生成される正整数列 $C$ を出力するプログラムを作成せよ.

制約

  • $1 ≦ N ≦ 500$.
  • $1 ≦ M ≦ 500$.
  • $1 ≦ A_{1} ≦ A_{2} ≦ … ≦ A_{N} ≦ 2000$.
  • $1 ≦ B_{1} ≦ B_{2} ≦ … ≦ B_{M} ≦ 2000$.

入力

入力は以下の形式で標準入力から与えられる.
$N$ $M$
$A_{1}$ $A_{2}$ $…$ $A_{N}$
$B_{1}$ $B_{2}$ $…$ $B_{M}$

出力

標準出力に $N + M$ 行出力せよ.
$k$ 行目 ($1 ≦ k ≦ N + M$) には,$C_{k}$ を出力せよ.

入出力例

入力例 1
2 1
1 2
2

出力例 1
1
2
2

アルゴリズムを行う前,$A=(1,2), B=(2)$ である. 以下のように数列 $C$ が生成される.

  • 数列 $A$ の先頭の要素は $1$,数列 $B$ の先頭の要素は $2$ なので,数列 $A$ の先頭の要素を数列 $C$ に追加しこれを数列 $A$ から削除する.
  • 数列 $A$ の先頭の要素は $2$,数列 $B$ の先頭の要素は $2$ なので,数列 $A$ の先頭の要素を数列 $C$ に追加しこれを数列 $A$ から削除する.
  • 数列 $A$ は空なので,数列 $B$ の先頭の要素を数列 $C$ に追加しこれを数列 $B$ から削除する.
  • 数列 $A$ も数列 $B$ も空なので,アルゴリズムを終了する.

アルゴリズムが終了した後,数列 $C=(1,2,2)$ である.


入力例 2
3 8
1 3 8
3 3 4 5 6 7 8 9

出力例 2
1
3
3
3
4
5
6
7
8
8
9

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.