Description:

给你2个长度为n的01串

从中选出\(n/2\)个,使得选出的数中第一排1的个数等于未选出数中第二排1的个数

输出一种方案即可,没有输出-1

Hint:

\(n \le 5000\)

Solution:

这题比赛的时候傻逼了

后面发现其实就是暴力枚举解方程

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

设\(a_i\)为所选第一排是1的数

\(b_i\)表示其对应第二排的数

设\(sum\)表示第二排1的个数

有:

\[\sum a_i = sum- \sum b_i
\]

移过去用d数组代替

\[\sum d_i = sum
\]

现在考虑d的取值,显然有0,1,2三种

\[cnt0*0+cnt1*1+cnt2*2=sum
\]

\[cnt0+cnt1+cnt2=n/2
\]

\[cnt1*1+cnt2*2=sum
\]

\[cnt0+cnt1+cnt2=n/2
\]

O(n)枚举即可

另附被爆踩的官方\(n^2\)的做法:

#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]; inline int read() {
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()
{
n=read(); scanf("%s %s",p+1,q+1);
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;
}

[CF1138B]Circus的更多相关文章

  1. [CareerCup] 11.7 Tower of People in Circus 马戏团的人塔

    11.7 A circus is designing a tower routine consisting of people standing atop one another's shoulder ...

  2. cf------(round)#1 C. Ancient Berland Circus(几何)

    C. Ancient Berland Circus time limit per test 2 seconds memory limit per test 64 megabytes input sta ...

  3. HDU 5515 Game of Flying Circus 二分

    Game of Flying Circus Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

  4. 难部署的taiga,式微的circus——趋势从进程管理到容器管理,简单才是美

    一直需要一个项目管理系统,一直没时间弄. taiga是github上搜project management star最多的项目,又是基于django用python写的后端,所以就用它: 但是,集中精力 ...

  5. Codeforces Beta Round #1 C. Ancient Berland Circus 计算几何

    C. Ancient Berland Circus 题目连接: http://www.codeforces.com/contest/1/problem/C Description Nowadays a ...

  6. AC日记——codeforces Ancient Berland Circus 1c

    1C - Ancient Berland Circus 思路: 求出三角形外接圆: 然后找出三角形三条边在小数意义下的最大公约数; 然后n=pi*2/fgcd; 求出面积即可: 代码: #includ ...

  7. CodeForces - 1C:Ancient Berland Circus (几何)

    Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things ...

  8. circus && web comsole docker-compose 独立部署web console 的一个bug

    如果直接使用以下的docker-compose 文件部署会有通过多播通信获取endpoint 异常的问题(circus 在stats endpoint 获取少了一个c) 这个问题是部分网络情况下会出现 ...

  9. circus security 来自官方的安全建议

    转自:https://circus.readthedocs.io/en/latest/design/security/ Circus is built on the top of the ZeroMQ ...

随机推荐

  1. Aplcloud中 openFrame 和 openWin 的区别

    WIn 占满一屏,但不能设置大小, Frame 可以 一般情况 Frame 依附于win 之上,关了window 通过window打开的frame 也会关闭.

  2. 第31月 第9天 责任链AppDelegate

    1.AppDelegate 到这里我们把解决方案再明确一下:把 AppDelegate 的调用解耦成一个 责任链 模型.并且满足以下特征: 每个模块都可以无限制的实现 AppDelegate 的方法. ...

  3. Supervisor的安装以及使用

    1.supervisor的介绍 Supervisor 是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统,通过配置可以实 ...

  4. sparkRDD相关操作

    RDD(弹性分布式数据集).RDD以分区中的每一行进行分布式计算.父子依赖关系. 一.RDD创建操作 1)数据集合 Val data=Array(1, 2, 3, 4, 5, 6, 7, 8, 9) ...

  5. Java8 Lambda表达式原理扫盲

    背景 在使用Lamdba表达式,一直以为是内部类的方式实现的,但是一想如果每次调用都实例化一个内部类,性能肯定不好,难道Java里的lambda表达式真的是这么实现的吗?也许是该研究下原理了. 正文 ...

  6. sonar6.7.2启动报错

    sonar6.7.2启动报错:错误信息如下: es.log java.lang.RuntimeException: can not run elasticsearch as rootsonar.log ...

  7. java实现二叉树的建立以及实现二叉查找树的查、插、删、遍历

    一.采用存储结构 1.顺序存储:采用数组,顺序存储适配于完全二叉树,对于非完全二叉树并不合适,主要体现在空间上的浪费,所以我们需要用到另一种存储方式——链式存储. 2.链式存储:数据data用键值对的 ...

  8. imageview设置图片时超长超大图片超出限制(OpenGLRenderer: Bitmap too large to be uploaded into a texture (996x9116, max=4096x4096))

    问题:遇到超长图片,宽长等比缩放,比如宽度同屏幕同宽,长度等比放大,放到后遇到长度超出OpenGLRenderer的最大限制,导致图片无法显示出来: 解决办法: //图片超出GPU对于openglRe ...

  9. 【easy】141. Linked List Cycle

    非常简单的题:判断链表有没有环(用快慢指针) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...

  10. 【easy】107. Binary Tree Level Order Traversal II 按层输出二叉树

    按层输出二叉树,广度优先. 3 / \ 9 20 / \ 15 7 [ [15,7], [9,20], [3] ] /** * Definition for a binary tree node. * ...