CF--Merging Two Decks--思维+贪心

D. Merging Two Decks

time limit per test

2 seconds

memory limit per test

256 megabytes

input

input.txt

output

output.txt

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

Copy

3
1 0 1
4
1 1 1 1

output

Copy

1 4 5 6 7 2 3
3
5 6 7

input

Copy

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

output

Copy

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

题意:

给出两堆牌,每一堆牌牌的状态有0、1中,现在将它们合并。合并有两种操作,一是不改变每一堆牌的相对位置将其插入另一堆牌。二是取出头部几张牌将其状态反转。

第一行输出插入后牌的标号。

第二行输入需要状态反转的最小次数。

第三行输入每次需要反转几张牌。

思路:

贪心,原则是力求将状态相同的放在相邻的位置。然后再模拟反转过程。

参考:https://www.xuebuyuan.com/1481421.html

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=500000+66;
const ll mod=1e9+7;
const ll INF=9999999;
int A[maxn];
int V[maxn];
int n,m;
vector<int>solve(int type)
{
    int i=1;
    int j=n+1;
    int k=0;
    while(i<=n||j<=n+m)
    {
        while(i<=n&&A[i]==type)
        {
            V[k++]=i++;
        }
        while(j<=n+m&&A[j]==type)
        {
            V[k++]=j++;
        }
        type=1-type;
    }
    vector<int>tmp;
    tmp.clear();
    for(int i=0;i<n+m;i++)
    {
        if(A[V[i]]!=A[V[i+1]])
        {
            tmp.push_back(i+1);
        }
    }
    return tmp;
}
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]);
    }
    vector<int>ans1=solve(0);
    vector<int>ans2=solve(1);
    vector<int>ans;
    if(ans1.size()<=ans2.size())
    {
        ans=solve(0);
    }else
    {
        ans=solve(1);
    }
    for(int i=0;i<n+m;i++)
    {
        printf("%d ",V[i]);
    }
    printf("\n");
    printf("%d\n",ans.size());
    for(int i=0;i<(int)ans.size();i++)
    {
        printf("%d ",ans[i]);
    }
    printf("\n");
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品类型管理页面,此页面提供给管理员的功能有:新增药品类型,修改药品类型,删除药品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值