Merging Two Decks(CF-234H)

Problem Description

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.

Input

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 a1, a2, ..., an (0 ≤ ai ≤ 1). Value ai 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 b1, b2, ..., bm (0 ≤ bi ≤ 1). Value bi 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.

Output

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: c1, c2, ..., cx (1 ≤ ci ≤ 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.

Examples

Input

3
1 0 1
4
1 1 1 1

Output

1 4 5 6 7 2 3
3
5 6 7

Input

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

Output

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

题意:给出两堆牌,一堆 n 张一堆 m 张,编号从 1~n+m,牌有的朝上有的朝下,现在要将两堆牌合成一堆,合并规则要求相对顺序不变,即同一堆牌中序号小的在上,然后进行翻转,从最上面开始可以翻转任意张连续牌,使得最后状态全都朝下,要求输出合并后的排列顺序,然后输出翻转的最小次数与翻转的牌数

思路:

朝上朝下是两种状态,用 1、0 表示即可

第一次合并时,要么是 0 在最上面,要么是 1 在最上面,为了使得翻转次数最小,那么要尽量让相同状态的在一起,然后进行翻转

根据题意可知,由于要使得所有牌都朝下,因此只需要在相邻的牌不相同时进行翻转,记录两种翻转的次数,然后取最小即可

此外,注意本题要使用文件读写

Source Program

#include<bits/stdc++.h>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 500000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int n,m;
int a[N];
int vis0[N],vis1[N];
int res[N];
void judge(int sta,int vis[]){
    memset(vis,0,sizeof(vis));
    int cnt=0;
    int x=1,y=n+1;
    while(x<=n||y<=n+m){
        while(x<=n&&a[x]==sta)
            vis[++cnt]=x++;
        while(y<=n+m&&a[y]==sta)
            vis[++cnt]=y++;
        sta=1-sta;
    }
}
int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    scanf("%d",&m);
    for(int i=n+1;i<=n+m;i++)
        scanf("%d",&a[i]);

    judge(0,vis0);
    vector<int> res0;
    for(int i=1;i<=n+m;i++)
        if(a[vis0[i]]!=a[vis0[i+1]])
            res0.push_back(i);

    judge(1,vis1);
    vector<int> res1;
    for(int i=1;i<=n+m;i++)
        if(a[vis1[i]]!=a[vis1[i+1]])
            res1.push_back(i);

    vector<int> ans;
    if(res1.size()<=res0.size()){
        memcpy(res,vis1,sizeof(vis1));
        ans=res1;
    }
    else{
        memcpy(res,vis0,sizeof(vis0));
        ans=res0;
    }

    for(int i=1;i<=n+m;i++)
        printf("%d ",res[i]);
    printf("\n");
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
        printf("%d ",ans[i]);
    printf("\n");

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值