CF234H.Merging Two Decks

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

桌前有两副纸牌,每副牌中有些牌正面朝上,有些牌背面朝上。你想将它们合并为一副牌,使得最终所有牌均背面朝上。整个过程分为两个阶段。

第一阶段是将两副牌合并为一副,要求:同一副牌中任意两张牌的相对顺序保持不变。即,对同一副牌中的任意两张不同牌 ii 和 jj,若合并前牌 ii 位于牌 jj 的上方,则合并后牌 ii 仍必须位于牌 jj 的上方。

第二阶段在第一阶段所得的牌堆上进行,执行“翻转操作”。每次操作可取走最上面若干张牌,将它们整体翻转(即每张牌正反面互换),再按原顺序(但上下颠倒)放回。换言之,被取走的牌中原本位于底部的那张,翻转后将位于顶部。

你的任务是使所有牌均背面朝上。请找出第一阶段的合并顺序,以及第二阶段的一系列翻转操作,使得最终所有牌均背面朝上,且翻转操作次数最少。

输入格式

The first input line contains a single integer n — the number of cards in the first deck (1 ≤ n ≤ 105).

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 ≤ 105).

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.

第一行输入包含一个整数 nn —— 第一副牌中的卡片数量(1≤n≤1051 \leq n \leq 10^5)。

第二行输入包含 nn 个整数,以单个空格分隔:a1, a2, …, ana_1,\ a_2,\ \dots,\ a_n(0≤ai≤10 \leq a_i \leq 1)。若第 ii 张牌正面朝下,则 ai=0a_i = 0;若正面朝上,则 ai=1a_i = 1。这些卡片按从最上方到最下方的顺序给出。

第三行输入包含一个整数 mm —— 第二副牌中的卡片数量(1≤m≤1051 \leq m \leq 10^5)。

第四行输入包含 mm 个整数,以单个空格分隔:b1, b2, …, bmb_1,\ b_2,\ \dots,\ b_m(0≤bi≤10 \leq b_i \leq 1)。若第 ii 张牌正面朝下,则 bi=0b_i = 0;若正面朝上,则 bi=1b_i = 1。这些卡片按从最上方到最下方的顺序给出。

输出格式

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·105.

第一行输出 n+mn + m 个用空格分隔的整数——表示第一阶段结束后,牌从上到下排列的编号顺序。
第一副牌中的牌应按从上到下的顺序对应其原始编号 11 到 nn;第二副牌中的牌应按从上到下的顺序对应其原始编号(每个编号增加 nn),即编号 n+1n+1 到 n+mn+m。

第二行输出一个整数 xx——使整副牌全部正面朝下所需的最少翻转操作次数。
第三行输出 xx 个整数:c1, c2, …, cxc_1,\,c_2,\,\dots,\,c_x(其中 1≤ci≤n+m1 \le c_i \le n + m),每个 cic_i 表示每次翻转操作需从牌堆顶部取走的牌的数量。请按操作执行顺序输出这些数值。

若存在多个最优解,输出任意一个即可。题目保证最小操作次数不超过 6⋅1056 \cdot 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

输入解题思路,AI测评打分。不知道怎么写?

首页