校省选赛第一场D题TwoDecks题解
今天晚上第二场比赛,现在还是赛后刷上次的题目,越刷越伤心,发现我赛后一次AC的功力很强大啊!!!(希望今晚变成是赛中一次AC啊!!)
好啦,回归正题。
看题目
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.
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.
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.
3
1 0 1
4
1 1 1 1
1 4 5 6 7 2 3
3
5 6 7
5
1 1 1 1 1
5
0 1 0 1 0
6 1 2 3 4 5 7 8 9 10
4
1 7 8 9
同样,先解释一下题目:
有两叠扑克牌,我们要将他们合并在一起,并且让所有扑克牌都朝下
有两个操作:
第一步操作: 将两叠扑克牌合并在一起,但是要保持原来的子顺序,比如
A牌是 正反正 B牌是反正正
1 2 3 4 5 6
合并之后不能是 3 1 2 4 5 6(3跑到1,2上面了!)但是可以是1 2 3 4 5 6 , 4 5 6 1 2 3,1 4 2 5 6 3,……等等,子顺序要保持。
第二步操作: 抽出上面k张牌反转后放回去,直到所有扑克牌都朝下。
比如合并后是 1 0 1 1 1
那么我们抽第一张牌反转后放回去 0 0 1 1 1 ,再抽出前两张,反转放回1 1 1 1 1 ,再5张反转就变成0 0 0 0 0(0 表示向下,1表示向上)
问最少操作次数。
我的思路就是贪心啦!
合并的时候,以第一叠牌的第一张为基准形成情况1 frm,以第二叠牌第一张为基准形成情况2 lat.
之后对于每个情况,如果其中一叠的当前牌是相同状态就直接放进,不然放另外一叠牌的当前牌,力求最相同连续。
这样就得出两个情况对叠后的一副牌啦。
之后就从头开始反转,不同就翻转,最后使得所有的牌都一样状态,此时如果是1状态,就再做一次反转。
这样得出的结果是最优的,这个可以自己想象。
我的代码:
/*******************************************************************************/
/* OS : 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 UTC 2013 GNU/Linux
* Compiler : g++ (GCC) 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
* Encoding : UTF8
* Date : 2014-04-03
* All Rights Reserved by yaolong.
*****************************************************************************/
/* Description: ***************************************************************
*****************************************************************************/
/* Analysis: ******************************************************************
*****************************************************************************/
/*****************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
class Pair
{
public:
int ind,stat;
Pair() {};
Pair & operator =(Pair rhs)
{
this->ind=rhs.ind;
this->stat=rhs.stat;
return *this;
} };
int main()
{ freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n,m,i,j,cntf,cntl;
vector<Pair> a,b;
vector<Pair> frm,lat;
int a_i,b_i,f_i,l_i;
while(cin>>n)
{
a.clear();
b.clear();
frm.clear();
lat.clear(); a.resize(n); for(i=0; i<n; i++)
{
cin>>j;
a[i].ind=i+1;
a[i].stat=j;
}
cin>>m;
b.resize(m);
frm.resize(n+m);
lat.resize(n+m); for(i=0; i<m; i++)
{
cin>>j;
b[i].ind=n+i+1;
b[i].stat=j;
} //合并,以a为标准
frm[0]=a[0];
f_i=0;
a_i=1;
b_i=0; while(f_i<n+m-1)
{
//cout<<"sb"<<f_i<<a_i<<b_i;;
if((a_i<n&&frm[f_i].stat==a[a_i].stat)||b_i==m)
{
f_i++;
frm[f_i]=a[a_i];
a_i++;
}
else
{
f_i++;
frm[f_i]=b[b_i];
b_i++;
}
}
//合并以b为标准
lat[0]=b[0];
l_i=0;
a_i=0;
b_i=1; while(l_i!=n+m-1)
{
if((b_i<m&&lat[l_i].stat==b[b_i].stat)|| (a_i==n))
{
l_i++;
lat[l_i]=b[b_i];
b_i++; }
else
{
l_i++;
lat[l_i]=a[a_i];
a_i++; }
}
/*
for(i=0;i<n+m;i++){
cout<<frm[i].stat<<" ";
}
cout<<endl; for(i=0;i<n+m;i++){
cout<<lat[i].stat<<" ";
}
cout<<endl;
*/
//对frm测试
cntf=cntl=0;
int stat=frm[0].stat;
for(f_i=1; f_i<n+m; f_i++)
{ if(frm[f_i].stat!=stat)
{
stat=frm[f_i].stat;
cntf++;
} }
if(stat==1)
{
cntf++;
} stat=lat[0].stat;
for(l_i=1; l_i<n+m; l_i++)
{ if(lat[l_i].stat!=stat)
{
stat=lat[l_i].stat;
cntl++;
} }
if(stat==1)
{
cntl++;
} if(cntl<cntf)
{
for(i=0;i<n+m;i++){
cout<<lat[i].ind<<" ";
}
cout<<endl;
cout<<cntl<<endl;
stat=lat[0].stat;
for(l_i=1; l_i<n+m; l_i++)
{ if(lat[l_i].stat!=stat)
{
stat=lat[l_i].stat;
cout<<l_i<<" ";
} }
if(stat==1)
{
cout<<l_i;
}
cout<<endl; }
else
{
for(i=0;i<n+m;i++){
cout<<frm[i].ind<<" ";
}
cout<<endl;
cout<<cntf<<endl;
stat=frm[0].stat;
for(f_i=1; f_i<n+m; f_i++)
{ if(frm[f_i].stat!=stat)
{
stat=frm[f_i].stat;
cout<<f_i<<" "; } }
if(stat==1)
{
cout<<f_i<<" ";
}
cout<<endl; } } fclose(stdin);
fclose(stdout); return 0; }
校省选赛第一场D题TwoDecks题解的更多相关文章
- 校省选赛第一场A题Cinema题解
今天是学校省选的第一场比赛,0战绩收工,死死啃着A题来做,偏偏一直WA在TES1. 赛后,才发现,原来要freopen("input.txt","r",stdi ...
- 校省选赛第一场C题解Practice
比赛时间只有两个小时,我没有选做这题,因为当时看样例也看不懂,比较烦恼. 后来发现,该题对输入输出要求很低.远远没有昨天我在做的A题的麻烦,赛后认真看了一下就明白了,写了一下,一次就AC了,没问题,真 ...
- hdu 5288||2015多校联合第一场1001题
pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...
- 2019年牛客多校第一场B题Integration 数学
2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...
- 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学
牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...
- HDU6578 2019HDU多校训练赛第一场 1001 (dp)
HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...
- HDU6579 2019HDU多校训练赛第一场1002 (线性基)
HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...
- 【2019多校第一场补题 / HDU6578】2019多校第一场A题1001Blank——dp
HDU6578链接 题意 有一串字符串,仅由 {0,1,2,3}\{0, 1, 2, 3\}{0,1,2,3} 组成,长度为 nnn,同时满足 mmm 个条件.每个条件由三个整数组成:l.r.xl.r ...
- 2014多校第一场J题 || HDU 4870 Rating(DP || 高斯消元)
题目链接 题意 :小女孩注册了两个比赛的帐号,初始分值都为0,每做一次比赛如果排名在前两百名,rating涨50,否则降100,告诉你她每次比赛在前两百名的概率p,如果她每次做题都用两个账号中分数低的 ...
随机推荐
- C# 4.0 新特性
http://www.cnblogs.com/webabcd/archive/2010/05/27/1744899.html 在MVC中Controller的action方法 常用的:可选参数和参数默 ...
- HW4.30
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 手动进行Excel数据和MySql数据转换
今天是全国数学建模比赛,同学选的一个题目需要对一个large的Excel表格进行统计,好哥们儿嘛--便帮助他完成了数据从Excel到MySql的转化.记下具体步骤分享给大家,也免得大家到网上到处乱找了 ...
- 关于ASSERT(断言)的作用
程序一般分为Debug 版本和Release 版本,Debug 版本用于内部调试,Release 版本发行给用户使用.断言assert 是仅在Debug 版本起作用的宏,它用于检查“不应该”发生的情况 ...
- hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- typeof和instanceof 运算符
instanceof运算符与typeof运算符相似,用于识别正在处理的对象的类型,但是在使用 typeof 运算符时采用引用类型存储值会出现一个问题. 无论引用的是什么类型的对象,它都返回 " ...
- javascript和php中的正则
正则: var subStr=str.replace(reg/str,''); 不改变原来的字符串返回替换后的字符窜; 如果不用正则,只能替换第一个匹配到的; var subStr=str.mat ...
- GWT事件与ELEMENT绑定
GWT提供了DOM工具,利用美工做好的HTML页面,后台人员结合GWT来开发. Element button = DOM.getElementById("button_a"); D ...
- poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新
题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...
- TR069协议向导——一个帮助你了解TR069协议的简明教程(一)
您也能够到下面地址下载: http://download.csdn.net/source/993034 1.为什么须要TR069 随着VoIP.IPTV等越来越多IP终端设备的普及(尤其在家庭中 ...