Codeforces 1138B Circus (构造方程+暴力)
题意:
给你两个01串,要你选n/2个位置,使得选的位置在s1中"1"的数量等于未选的s2中"1"的数量
n<=5000,1s
思路:
设两个串中出现"00""01""10""11"的总数是A,B,C,D,我们选的个数分别是a,b,c,d
数据最多能承受n^2的暴力
根据题意,有a+b+c+d=n/2 ①
c+d=B-b+D-d ②
联立得d=B+D+a-n/2
于是我们暴力a,可以得到d
然后将a,d带入①可得b+c=n/2-a-d
暴力b可得c
最后只要a,b,c,d是否全部合法,输出方案即可
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
#include<cmath> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
//#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f; int n;
char s1[maxn];
char s2[maxn];
int A,a,B,b,C,c,D,d;
int main(){
scanf("%d", &n);
scanf("%s", s1+);
scanf("%s", s2+);
for(int i = ; i <= n; i++){
if(s1[i]==''){
if((s2[i]==''))A++;
else B++;
}
else{
if(s2[i]=='')C++;
else D++;
}
}
int mm = min(A, n/);
a = b = c = d = -;
int ys = ;
for(a = ; a <= A && a <= n/; a++){
if(B+D-n/+a<=D&&B+D-n/+a>=){
d = B+D-n/+a;
//int m = min(min(n/2-a-d,B),n/2);
//printf("%d %d\n",a,d);
for(b = ; b <= B&&b<=n/-a-d; b++){
//printf(" %d\n",b);
if(n/-a-b-d<=C&&n/-a-b-d>=){
c = n/-a-b-d;
ys=;break;
}
}
if(ys)break;
}
}
//printf("%d %d %d %d\n", a,b,c,d);
if(!ys){
return printf("-1"),;
}
for(int i = ; i <= n; i++){
if(s1[i]==''){
if(s2[i]==''&&a){
a--;
printf("%d ",i);
}
else if(s2[i]==''&&b){
b--;
printf("%d ",i);
}
}
else if(s1[i]==''){
if(s2[i]==''&&c){
c--;
printf("%d ",i);
}
else if(s2[i]==''&&d){
d--;
printf("%d ",i);
}
}
}
return ;
}
Codeforces 1138B Circus (构造方程+暴力)的更多相关文章
- Codeforces 1138B(列方程枚举)
构造模拟要分情况讨论感觉不是够本质,然后官解是因为只有四个量所以可以根据限制条件列两个方程,再枚举一下解就可以了. const int maxn = 5000 + 5; int n, c[maxn], ...
- codeforces 1041 e 构造
Codeforces 1041 E 构造题. 给出一种操作,对于一棵树,去掉它的一条边.那么这颗树被分成两个部分,两个部分的分别的最大值就是这次操作的答案. 现在给出一棵树所有操作的结果,问能不能构造 ...
- Codeforces 959 树构造 暴力求最小字典序互质序列
A B C 题目给你一个结论 最少需要min((odd,even)个结点可以把一棵树的全部边连起来 要求你输出两颗树 一棵树结论是正确的 另外一棵结论是正确的 正确结论的树很好造 主要是错误的树 题目 ...
- Codeforces Gym 100015H Hidden Code 暴力
Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...
- Codeforces gym 100685 A. Ariel 暴力
A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...
- Codeforces Gym 100637G G. #TheDress 暴力
G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...
- [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力
Couple Cover Time Limit: 3000MS Memory Limit: 524288KB 64bit IO Format: %I64d & %I64u Descri ...
- Codeforces 626D Jerry's Protest(暴力枚举+概率)
D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- Sonya and Matrix CodeForces - 1004D (数学,构造)
http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...
随机推荐
- ipaclient 4.5.4 requires jinja2, which is not installed. rtslib-fb 2.1.63 has requirement pyudev>=0.16.1, but you'll have pyudev 0.15 which is incompatible. ipapython 4.5.4 has requirement dnspython>=
- UGUI源码之Selectable
Selectable是Button.InputField.Toggle.ScrollBar.Slider.Dropdown的基类. Selectable的继承的类与接口如下: public class ...
- JS怎样做四舍五入
1 .tofixed方法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字.例如将数据Num保留2位小数,则表示为:toFixed(Num):但是其四舍五入的规则与数学中的规则 ...
- vue状态管理vuex从浅入深详细讲解
1.vuex简介以及创建一个简单的仓库 vuex是专门为vue框架而设计出的一个公共数据管理框架,任何组件都可以通过状态管理仓库数据沟通,也可以统一从仓库获取数据,在比较大型的应用中,数据交互庞大的情 ...
- 云资源中的低成本战斗机——竞价实例,AWS、阿里云等六家云厂商完全用户使用指南
云端资源价格 预留实例:长期持有,批发路线,价格最便宜. 按需实例:即买即用,零售路线,价格最贵. 这两种资源,基于不同区域/价格的六家云厂商价格对比,连同原始数据文档我们已经打包成了一份电子文档,有 ...
- background-position和position
1.background-position:表示背景定位的属性.描述属性值时,有两种方式:一是像素描述:而是单位描述. (1)像素描述: 格式如下: background-position:向右偏移量 ...
- MyEclipse导出war包丢失文件问题解决
这两天忙于一项目的上线,现总结一下遇到的一个奇怪问题的解决方案. 公司用的是Windows系统的服务器,所以省去了很多linux的繁琐命令.部署工作简单了很多.一切准备结束,放上War包启动服务器后, ...
- gitbub 基本使用
一.环境 git:https://git-scm.com/ 申请github账号:https://github.com/ 二.安装git 一直next即可 三.创储存建库 1.选择New reposi ...
- springboot中使用logback
原文地址:https://blog.csdn.net/tianyaleixiaowu/article/details/73321610 Springboot默认集成的就是logback,logback ...
- HGE引擎改进
基于HGEDX9版本修改. hge库: 1.全UNICODE化 2.增加时间模块:Timer_StartTick(),Timer_NowTick()等六个函数 3.增加服从正态分布的随机数生成函数:R ...