[CF1138B]Circus
Description:
给你2个长度为n的01串
从中选出\(n/2\)个,使得选出的数中第一排1的个数等于未选出数中第二排1的个数
输出一种方案即可,没有输出-1
Hint:
\(n \le 5000\)
Solution:
这题比赛的时候傻逼了
后面发现其实就是暴力枚举解方程
\(AuBao\)想出一个O(n)做法力碾标算,这里放出来%一%:
设\(a_i\)为所选第一排是1的数
\(b_i\)表示其对应第二排的数
设\(sum\)表示第二排1的个数
有:
\]
移过去用d数组代替
\]
现在考虑d的取值,显然有0,1,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的更多相关文章
- [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 ...
- 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 ...
- HDU 5515 Game of Flying Circus 二分
Game of Flying Circus Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
- 难部署的taiga,式微的circus——趋势从进程管理到容器管理,简单才是美
一直需要一个项目管理系统,一直没时间弄. taiga是github上搜project management star最多的项目,又是基于django用python写的后端,所以就用它: 但是,集中精力 ...
- Codeforces Beta Round #1 C. Ancient Berland Circus 计算几何
C. Ancient Berland Circus 题目连接: http://www.codeforces.com/contest/1/problem/C Description Nowadays a ...
- AC日记——codeforces Ancient Berland Circus 1c
1C - Ancient Berland Circus 思路: 求出三角形外接圆: 然后找出三角形三条边在小数意义下的最大公约数; 然后n=pi*2/fgcd; 求出面积即可: 代码: #includ ...
- CodeForces - 1C:Ancient Berland Circus (几何)
Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things ...
- circus && web comsole docker-compose 独立部署web console 的一个bug
如果直接使用以下的docker-compose 文件部署会有通过多播通信获取endpoint 异常的问题(circus 在stats endpoint 获取少了一个c) 这个问题是部分网络情况下会出现 ...
- circus security 来自官方的安全建议
转自:https://circus.readthedocs.io/en/latest/design/security/ Circus is built on the top of the ZeroMQ ...
随机推荐
- python 学习 leetcode ---number of island
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 将应用部署到Tomcat根目录的方法
将应用部署到Tomcat根目录的目的是可以通过“http://[ip]:[port]”直接访问应用,而不是使用“http://[ip]:[port]/[appName]”上下文路径进行访问. 方法 ...
- Linux 默认日志类型
nginx 默认日志类型有两个 access.log http 记录访问日志. error.log server 操作记录日志
- ztree树应用
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ZtreeListVi ...
- 利用request和re抓取猫眼电影排行
import requests import re import time def get_one_page(url): headers = { 'User-Agent': 'Mozilla/5.0 ...
- 区别 chown和chmod的用法
本人总是习惯使用chmod,而把chown混淆. chown就是修改 第一列内容的 ,chmod是修改 第3,4列内容的. chown用法用来更改某个目录或文件的用户名和用户组的chown 用户名:组 ...
- L2-011 玩转二叉树 (25 分) (树)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805065406070784 题目: 给定一棵二叉树的中序遍历和前序 ...
- L2-010 排座位 (25 分) (最短路)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805066135879680 题目: 布置宴席最微妙的事情,就是给前 ...
- centos6.8编译安装mysql
1.安装编译代码需要的包 yum -y install make gcc-c++ cmake bison-devel ncurses-devel 2.创建mysql用户(但是不能使用mysql账号登陆 ...
- FTP、FTPS和SFTP
FTP 一.两种传输方式 ASCII传输方式 假定用户正在拷贝的文件包含的简单ASCII码文本,如果在远程机器上运行的不是UNIX,当文件传输时ftp通常会自动地调整文件的内容以便于把文件解释成另外那 ...