题目链接:
http://codeforces.com/problemset/problem/240/D
D. Merging Two Decks
input input.txtoutput 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 1output
Copy
1 4 5 6 7 2 3 3 5 6 7input
Copy
5 1 1 1 1 1 5 0 1 0 1 0output
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;
}