301254: CF234H. Merging Two Decks

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Merging Two Decks

题目描述

There are two decks of cards lying on the table in front of you, some cards in these decks lay face up, some of them lay face down. You want to merge them into one deck in which each card is face down. You're going to do it in two stages. The first stage is to merge the two decks in such a way that the relative order of the cards from the same deck doesn't change. That is, for any two different cards $ i $ and $ j $ in one deck, if card $ i $ lies above card $ j $ , then after the merge card $ i $ must also be above card $ j $ . The second stage is performed on the deck that resulted from the first stage. At this stage, the executed operation is the turning operation. In one turn you can take a few of the top cards, turn all of them, and put them back. Thus, each of the taken cards gets turned and the order of these cards is reversed. That is, the card that was on the bottom before the turn, will be on top after it. Your task is to make sure that all the cards are lying face down. Find such an order of merging cards in the first stage and the sequence of turning operations in the second stage, that make all the cards lie face down, and the number of turns is minimum.

输入输出格式

输入格式


The first input line contains a single integer $ n $ — the number of cards in the first deck $ (1<=n<=10^{5}) $ . The second input line contains $ n $ integers, separated by single spaces $ a_{1},a_{2},...,a_{n} $ $ (0<=a_{i}<=1) $ . Value $ a_{i} $ equals 0, if the $ i $ -th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost one to the bottommost one. The third input line contains integer $ m $ — the number of cards in the second deck $ (1<=m<=10^{5}) $ . The fourth input line contains $ m $ integers, separated by single spaces $ b_{1},b_{2},...,b_{m} $ $ (0<=b_{i}<=1) $ . Value $ b_{i} $ equals 0, if the $ i $ -th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost to the bottommost.

输出格式


In the first line print $ n+m $ space-separated integers — the numbers of the cards in the order, in which they will lie after the first stage. List the cards from top to bottom. The cards from the first deck should match their indexes from $ 1 $ to $ n $ in the order from top to bottom. The cards from the second deck should match their indexes, increased by $ n $ , that is, numbers from $ n+1 $ to $ n+m $ in the order from top to bottom. In the second line print a single integer $ x $ — the minimum number of turn operations you need to make all cards in the deck lie face down. In the third line print $ x $ integers: $ c_{1},c_{2},...,c_{x} $ $ (1<=c_{i}<=n+m) $ , each of them represents the number of cards to take from the top of the deck to perform a turn operation. Print the operations in the order, in which they should be performed. If there are multiple optimal solutions, print any of them. It is guaranteed that the minimum number of operations doesn't exceed $ 6·10^{5} $ .

输入输出样例

输入样例 #1

3
1 0 1
4
1 1 1 1

输出样例 #1

1 4 5 6 7 2 3
3
5 6 7

输入样例 #2

5
1 1 1 1 1
5
0 1 0 1 0

输出样例 #2

6 1 2 3 4 5 7 8 9 10
4
1 7 8 9

Input

题意翻译

## 题目描述 你面前的桌子上放着两副牌,其中有些牌面朝上,有些牌面朝下。你想把它们合并成一副牌,并且每张牌都面朝下。你将分两个阶段进行。 第一阶段是合并两副牌,使同一副牌的相对的顺序不变。也就是说,对于一副牌中的任意两张不同的牌 $i$ 和 $j$,如果牌 $i$ 位于牌 $j$ 之上,则在合并之后,牌 $i$ 也必须位于牌 $j$ 之上。 第二阶段是在第一阶段的基础上进行的。在阶段中,你可以拿几张最上面的牌,把它们全部转回来。因此,每一张被拿走的牌都被翻转,并且这些牌的顺序被颠倒。也就是说,在颠倒前位于底部的牌,在颠倒后将位于顶部。 你的任务是确保所有的牌都面朝下。在第一阶段找到合并牌的顺序,在第二阶段找到翻转操作的顺序,使所有牌面朝下,并且翻转次数最小。 ## 输入格式 第一个输入行包含一个整数$n——$第一副牌组中的牌数$(1<=n<=10^{5})$。 第二个输入行包含$n$个整数。$a_{n}(0<=a_{i}<=1)$。如果第$i$张牌正面朝下,则值$a_{i}$等于0,如果牌正面朝上,则值为1。这些牌是按从最上面到最下面的顺序排列的。 第三输入行包含整数$m$-第二副牌组中的牌数$(1<=m<=10^{5})$。 第四输入行包含$m$个整数,$b_{m}$$(0\le b_{i}\le1)$。如果第$i$张牌正面朝下,则值$b_{i}$等于0,如果牌正面朝上,则值为1。卡片是按从最上面到最下面的顺序排列的。 ## 输出格式 在第一行中,打印$n+m$空格分隔的整数,即第一阶段后卡片的编号。从上到下列出卡片。第一副牌中的牌应该按照从上到下的顺序,从$1$到$n$匹配它们的索引。第二副牌中的牌应该与其索引相匹配,按从上到下的顺序增加$n$,即从$n+1$到$n+m$的数字。 在第二行中,打印一个整数$x$,这是使牌组中的所有牌面朝下所需的最小回合数。在第三行打印$x$整数:$c_{1},c_{2}......c^{x}$$(1\le c_{i}\le n+m)$,它们中的每一个都表示要从牌组顶部取出以执行转弯操作的牌的数量。按应执行的顺序打印操作。 如果有多个最佳解决方案,请打印其中任何一个。保证最低操作次数不超过$6\times10^{5}$。

加入题单

上一题 下一题 算法标签: