# [CF1138B]Circus

## Hint:

$n \le 5000$

## Solution:

$AuBao$想出一个O(n)做法力碾标算,这里放出来%一%:

$a_i$所选第一排是1的数
$b_i$表示其对应第二排的数
$sum$表示第二排1的个数

$\sum a_i = sum- \sum b_i$

$\sum d_i = sum$

$cnt0*0+cnt1*1+cnt2*2=sum$
$cnt0+cnt1+cnt2=n/2$

$cnt1*1+cnt2*2=sum$
$cnt0+cnt1+cnt2=n/2$

O(n)枚举即可

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ls p<<1
#define rs p<<1|1
using namespace std;
typedef long long ll;
const int mxn=1e5+5;
int n,m,cnt,hd[mxn];

char c=getchar(); int x=0,f=1;
while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
while(c<='9'&&c>='0') {x=(x<<3)+(x<<1)+(c&15);c=getchar();}
return x*f;
}
inline void chkmax(int &x,int y) {if(x<y) x=y;}
inline void chkmin(int &x,int y) {if(x>y) x=y;}

struct ed {
int to,nxt;
}t[mxn<<1];

inline void add(int u,int v) {
t[++cnt]=(ed) {v,hd[u]}; hd[u]=cnt;
}

char p[5005],q[5005];
int sum,cnt0,cnt1,cnt2;
vector<int > g0,g1,g2;

int main()
{
for(int i=1;i<=n;++i) {
if(p[i]=='0'&&q[i]=='0') ++cnt0,g0.push_back(i);
if(p[i]=='0'&&q[i]=='1') ++cnt1,g1.push_back(i),++sum;
if(p[i]=='1'&&q[i]=='0') ++cnt1,g1.push_back(i);
if(p[i]=='1'&&q[i]=='1') ++cnt2,g2.push_back(i),++sum;
}
int c1,c0;
for(int i=0;i<=cnt2;++i) {
c1=sum-i*2; c0=n/2-i-c1;
if(c1<0||c0<0) continue ;
if(c1<=cnt1&&c0<=cnt0) {
for(int j=0;j<c0;++j)
printf("%d ",g0[j]);
for(int j=0;j<c1;++j)
printf("%d ",g1[j]);
for(int j=0;j<i;++j)
printf("%d ",g2[j]);
exit(0);
}
}
puts("-1");
return 0;
}

posted @ 2019-04-02 08:32 cloud_9 阅读(...) 评论(...) 编辑 收藏