2089: Bit String Reordering
Submit Page Summary Time Limit: 1 Sec Memory Limit: 512 Mb Submitted: 178 Solved: 83
Description
You have to reorder a given bit string as specified. The only operation allowed is swapping adjacent bit pairs. Please write a program that calculates the minimum number of swaps required. The initial bit string is simply represented by a sequence of bits, while the target is specified by a run-length code. The run-length code of a bit string is a sequence of the lengths of maximal consecutive sequences of zeros or ones in the bit string. For example, the run-length code of “011100” is “1 3 2”. Note that there are two different bit strings with the same run-length code, one starting with zero and the other starting with one. The target is either of these two. In Sample Input 1, bit string “100101” should be reordered so that its run-length code is “1 3 2”, which means either “100011” or “011100”. At least four swaps are required to obtain “011100”. On the other hand, only one swap is required to make “100011”. Thus, in this example, 1 is the answer.
Input
The input consists of several tests case. For each test, the test case is formatted as follows. NM b1 b2 ...bN p1 p2 ...pM ThefirstlinecontainstwointegersN (1≤N ≤15)andM (1≤M ≤N). Thesecondline specifies the initial bit string by N integers. Each integer bi is either 0 or 1. The third line contains the run-length code, consisting of M integers. Integers p1 through pM represent the lengths of consecutive sequences of zeros or ones in the bit string, from left to right. Here, 1≤pj for1≤j≤M and Mj=1pj =N hold. Itisguaranteedthattheinitialbitstringcanbe reordered into a bit string with its run-length code p1, . . . , pM .
Output
Output the minimum number of swaps required.
Sample Input
6 3 1 0 0 1 0 1 1 3 2 7 2 1 1 1 0 0 0 0 4 3 15 14 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 0 1
Sample Output
1 12 7 0
Hint
Source
Asia Regional Contest, Tokyo, 2014
题目大意:给一个长度为n的01串,并给出m个数字对应希望这个数字区间段内全0或全1,交错下去。求最少经过几次相邻交换,达到目标。
这个题目有多种解法,但都需要想一想。
法一:状压BFS
注意n的范围:n<=15。
哇!好小!并且,是01串,这就很自然而然的想到状态压缩了。由一个初始状态到某个目标态,就bfs跑一遍即可。注目标态有两种。
有点儿大材小用???
代码:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=16;
int n,m;
int ans;
int state,zero,one;
int vst[1<<16],two[1<<16];
void init()
{
two[0]=1;
for(int i=1;i<maxn;++i){
two[i]=(two[i-1]<<1);
}
}
void bfs()
{
if(state==zero||state==one){
ans=0;
return;
}
int t[maxn];
memset(vst,0x3f,sizeof(vst));
queue <int> q;
q.push(state);
vst[state]=0;
while(!q.empty()){
int x=q.front();q.pop();
int y=x;
memset(t,0,sizeof(t));
for(int i=0;i<n;++i){ //可优化
if(y&1) t[i]=1;
y>>=1;
}
for(int i=n-1;i;--i){
int newx=x;
if(t[i]&&!t[i-1]||t[i-1]&&!t[i]){
newx-=(t[i]*two[i]+t[i-1]*two[i-1]);
newx+=(t[i-1]*two[i]+t[i]*two[i-1]);
}
else continue;
if(vst[x]+1<vst[newx]){
vst[newx]=vst[x]+1;
q.push(newx);
}
if(newx==zero||newx==one){
ans=vst[newx];
return;
}
}
}
}
int main()
{
int a;
init();
while(~scanf("%d%d",&n,&m)){
state=one=zero=0;
for(int i=0;i<n;++i){
scanf("%d",&a);
if(a) state+=two[i];
}
int now=0;
for(int i=0;i<m;++i){
scanf("%d",&a);
for(int j=now;j<now+a;++j){
if(i%2) zero+=two[j];
else one+=two[j];
}
now+=a;
}
bfs();
printf("%d\n",ans);
}
return 0;
}
法二:模拟&贪心
首先可以根据读入的m个数,奇数位偶数位分别是期望相同数字的,有一些组的数据可以根据这个判断0开始或1开始。
找到第一个位置不符合的,贪心来想,与后面第一个和它不一样的数字交换过去即为最小交换次数。
参考了ljq姑娘的炒鸡精简漂亮的写法:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=16;
const int inf=0x3f3f3f3f;
int n,m;
int a[maxn],aa[maxn],b[maxn],c[maxn];
int solve(int flag)
{
int now=0;
for(int i=1;i<=m;++i){
for(int j=1;j<=b[i];++j){
c[++now]=flag;
}
flag=!flag; //真是巧妙的状态转换!太傻了连!都想不到
}
for(int i=1;i<=n;++i)
aa[i]=a[i]; //之后的aa[i]有可能改变所以需要复制666
int res=0;
for(int i=1;i<=n;++i){
if(aa[i]!=c[i]){
int j;
for(j=i+1;j<=n;++j){
if(aa[j]==c[i]){
aa[j]=aa[i];
aa[i]=c[i];
break;
}//并不是说aa[i]与aa[j]交换,而是把j放到i的位置上,剩下往后推移,因为i到j-1之间都为aa[i]嘛
}
res+=(j-i);
}
}
return res;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
int one=0,sum=0;
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
if(a[i]) ++one;
}
for(int i=1;i<=m;++i){
scanf("%d",&b[i]);
if(i%2) sum+=b[i];
}
int x,y,ans;
x=y=inf;
if(one==sum){
x=solve(1);
}
if(one==(n-sum)){
y=solve(0);
}
ans=min(x,y);
printf("%d\n",ans);
}
return 0;
}