Codeforces Round #145 (Div. 1) D. Merging Two Decks

本文详细解析Codeforces D题“MergingTwoDecks”,探讨如何通过贪心算法和暴力搜索,找到合并两叠扑克牌并使其全部朝下所需的最少翻转次数及翻转位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 题目链接:

http://codeforces.com/problemset/problem/240/D

D. Merging Two Decks

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两面。通过反转可以将0 与 1交换,问最少多少操作可以将扑克牌的0全部朝上,反转的规则是:从当前位置开始前面的k张全部都反转,

有两个操作:

第一步操作: 将两叠扑克牌合并在一起,对每一叠来说相对顺序不变,

第二步操作: 抽出上面k张牌反转后放回去,直到所有扑克牌0朝上。

输出:(满足条件的任意)扑克牌的顺序,需要反转的次数,反转的位置

思路:

贪心+暴力

枚举最上面的0开始 和 最上面1开始的两种情况,比较反转次数,然后输出

This is the code:

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EXP exp(1)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}
const int maxn = 500000+5;
int n,m;
int a[maxn];//原始数据
int vis0[maxn];//存储0开头的序列
int vis1[maxn];//存储1开头的序列
vector<int> ans0,ans1;//分别存储0,1需要转换的位置
void check(int sta,int vis[])
{
    memset(vis,0,sizeof(vis));
    int cnt=0;
    int x=1;
    int 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=sta^1;//交换0和1
    }
}
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]);

    check(0,vis0);//用0开头,排序
    for(int i=1; i<=n+m; i++)
    {
        if(a[vis0[i]]!=a[vis0[i+1]])//最后一个要和0判断,是不是出现全1的情况
            ans0.push_back(i);//计算
    }

    check(1,vis1);//用1开始排序
    for(int i=1; i<=n+m; i++)
    {
        if(a[vis1[i]]!=a[vis1[i+1]])
            ans1.push_back(i);
    }

    if(ans1.size()<=ans0.size())//比较那这种开始方式需要的转换少
    {
        for(int i=1; i<=n+m; i++)
            printf("%d ",vis1[i]);
        printf("\n");
        printf("%d\n",ans1.size());
        for(int i=0; i<ans1.size(); i++)
            printf("%d ",ans1[i]);
    }
    else
    {
        for(int i=1; i<=n+m; i++)
            printf("%d ",vis0[i]);
        printf("\n");
        printf("%d\n",ans0.size());
        for(int i=0; i<ans0.size(); i++)
            printf("%d ",ans0[i]);
    }
    return 0;
}

 

内容概要:论文提出了一种基于空间调制的能量高效分子通信方案(SM-MC),将传输符号分为空间符号和浓度符号。空间符号通过激活单个发射纳米机器人的索引来传输信息,浓度符号则采用传统的浓度移位键控(CSK)调制。相比现有的MIMO分子通信方案,SM-MC避免了链路间干扰,降低了检测复杂度并提高了性能。论文分析了SM-MC及其特例SSK-MC的符号错误率(SER),并通过仿真验证了其性能优于传统的MIMO-MC和SISO-MC方案。此外,论文还探讨了分子通信领域的挑战、优势及相关研究工作,强调了空间维度作为新的信息自由度的重要性,并提出了未来的研究方向和技术挑战。 适合人群:具备一定通信理论基础,特别是对纳米通信和分子通信感兴趣的科研人员、研究生和工程师。 使用场景及目标:①理解分子通信中空间调制的工作原理及其优势;②掌握SM-MC系统的具体实现细节,包括发射、接收、检测算法及性能分析;③对比不同分子通信方案(如MIMO-MC、SISO-MC、SSK-MC)的性能差异;④探索分子通信在纳米网络中的应用前景。 其他说明:论文不仅提供了详细的理论分析和仿真验证,还给出了具体的代码实现,帮助读者更好地理解和复现实验结果。此外,论文还讨论了分子通信领域的标准化进展,以及未来可能的研究方向,如混合调制方案、自适应调制技术和纳米机器协作协议等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值