C. Pair Programming-Codeforces Round #731 (Div. 3)

原文链接Problem - C - Codeforces

C. Pair Programming

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Monocarp and Polycarp are learning new programming techniques. Now they decided to try pair programming.

It's known that they have worked together on the same file for n+mn+m minutes. Every minute exactly one of them made one change to the file. Before they started, there were already kk lines written in the file.

Every minute exactly one of them does one of two actions: adds a new line to the end of the file or changes one of its lines.

Monocarp worked in total for nn minutes and performed the sequence of actions [a1,a2,…,an][a1,a2,…,an]. If ai=0ai=0, then he adds a new line to the end of the file. If ai>0ai>0, then he changes the line with the number aiai. Monocarp performed actions strictly in this order: a1a1, then a2a2, ..., anan.

Polycarp worked in total for mm minutes and performed the sequence of actions [b1,b2,…,bm][b1,b2,…,bm]. If bj=0bj=0, then he adds a new line to the end of the file. If bj>0bj>0, then he changes the line with the number bjbj. Polycarp performed actions strictly in this order: b1b1, then b2b2, ..., bmbm.

Restore their common sequence of actions of length n+mn+m such that all actions would be correct — there should be no changes to lines that do not yet exist. Keep in mind that in the common sequence Monocarp's actions should form the subsequence [a1,a2,…,an][a1,a2,…,an] and Polycarp's — subsequence [b1,b2,…,bm][b1,b2,…,bm]. They can replace each other at the computer any number of times.

Let's look at an example. Suppose k=3k=3. Monocarp first changed the line with the number 22 and then added a new line (thus, n=2,a=[2,0]n=2,a=[2,0]). Polycarp first added a new line and then changed the line with the number 55 (thus, m=2,b=[0,5]m=2,b=[0,5]).

Since the initial length of the file was 33, in order for Polycarp to change line number 55 two new lines must be added beforehand. Examples of correct sequences of changes, in this case, would be [0,2,0,5][0,2,0,5] and [2,0,0,5][2,0,0,5]. Changes [0,0,5,2][0,0,5,2] (wrong order of actions) and [0,5,2,0][0,5,2,0] (line 55 cannot be edited yet) are not correct.

Input

The first line contains an integer tt (1≤t≤10001≤t≤1000). Then tt test cases follow. Before each test case, there is an empty line.

Each test case contains three lines. The first line contains three integers kk, nn, mm (0≤k≤1000≤k≤100, 1≤n,m≤1001≤n,m≤100) — the initial number of lines in file and lengths of Monocarp's and Polycarp's sequences of changes respectively.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤3000≤ai≤300).

The third line contains mm integers b1,b2,…,bmb1,b2,…,bm (0≤bj≤3000≤bj≤300).

Output

For each test case print any correct common sequence of Monocarp's and Polycarp's actions of length n+mn+m or -1 if such sequence doesn't exist.

Example

input

Copy

5

3 2 2
2 0
0 5

4 3 2
2 0 5
0 6

0 2 2
1 0
2 3

5 4 4
6 0 8 0
0 7 0 9

5 4 1
8 7 8 0
0

output

Copy

2 0 0 5 
0 2 0 6 5 
-1
0 6 0 7 0 8 0 9
-1

---------------------------------------------------------------------------------------------------------------------------------

每次先抓住一个扩展,一个不能扩展就扩展另一个。两个都不能扩展时,显然就不满足条件

首先,对于一个确定的nowk,凡是0或者比它小的,扩展掉没有任何副作用,每次循环让两者都前进到大于nowk的地方,这时每组只能仰仗另一组的扩展来增加nowk,当双方互相“仰仗”时,显然无法扩展。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
# include<map>
# include<iomanip>
# include<vector>
# pragma optimize(2)
using namespace std;
typedef long long int ll;
int a[110],b[110];
int ans[110];
int len;
int main ()
{
    int t;
    cin>>t;

    while(t--)
    {
        int k,n,m;
        cin>>k>>n>>m;
        len=0;
        int nowk=k;

        for(int i=1; i<=n; i++)
            cin>>a[i];


        for(int i=1; i<=m; i++)
            cin>>b[i];



        int left1=1,left2=1,flag=0;

        while(left1<=n||left2<=m)
        {

            int preleft1=left1;
            int preleft2=left2;

            while(a[left1]<=nowk&&left1<=n)
            {
                ans[len]=a[left1];
                if(a[left1]==0)
                    nowk++;
                len++;

                left1++;
            }


            while(b[left2]<=nowk&&left2<=m)
            {
                ans[len]=b[left2];
                len++;
                if(b[left2]==0)
                    nowk++;

                left2++;
            }

            if(preleft1==left1&&left2==preleft2)
            {
                flag=1;
                break;
            }

        }

        if(flag)
        {
            cout<<-1<<'\n';
        }

        else
        {
            for(int i=0; i<len; i++)
            {
                cout<<ans[i]<<" ";
            }

            cout<<'\n';
        }

    }


    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值