Codeforces Contest 1138 problem B Circus —— 死亡1700,暴力

2024-04-07 00:32

本文主要是介绍Codeforces Contest 1138 problem B Circus —— 死亡1700,暴力,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Polycarp is a head of a circus troupe. There are n — an even number — artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then ci=1, otherwise ci=0), and whether they can perform as an acrobat (if yes, then ai=1, otherwise ai=0).

Split the artists into two performances in such a way that:

each artist plays in exactly one performance,
the number of artists in the two performances is equal (i.e. equal to n2),
the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance.
Input
The first line contains a single integer n (2≤n≤5000, n is even) — the number of artists in the troupe.

The second line contains n digits c1c2…cn, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise.

The third line contains n digits a1a2…an, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise.

Output
Print n2 distinct integers — the indices of the artists that should play in the first performance.

If there are multiple answers, print any.

If there is no solution, print a single integer −1.

Examples
inputCopy
4
0011
0101
outputCopy
1 4
inputCopy
6
000000
111111
outputCopy
-1
inputCopy
4
0011
1100
outputCopy
4 3
inputCopy
8
00100101
01111100
outputCopy
1 2 3 6
Note
In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well.

In the second example, the division is not possible.

In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.

题意:

给你n个人的两种技能,1代表会,0代表不会。现在有两场演出,每场需要分配n/2个人,每个人只能去一场,让你输出第一场去的人使得在第一场会第一个技能的人的数量=第二场会第二个技能的人的数量。

题解:

我真的服了,这是B该有的难度吗?而且为什么每个1700难度对我都这么不友好?sun dog。
还是不要看我的代码了,去看别人的吧,我自己写留个纪念。
我设会第一个技能但不会第二个技能为oz:one zero 同理可得:zo,oo,zz
首先如果zo有0个且oz也是0个且oo有奇数个,那么不论怎么分配都不可能相等
然后如果zo的数量-zz的数量>oz的数量+oo的数量,或者相反。也就是说一边无论怎么拿,会这个的人数都比另一边大,那么也是不可能的。
之后要考虑oz.size()和zo.size()的大小,我是直接做小的。如果(int)zo.size()>(int)oz.size()

重点:size()的有关操作最好是强转之后做,否则会re

那么oz肯定全部要取,之后就是while一下,看看要取多少oo,然后这里还有一个判断,我举个栗子:

oo zo oz+oo(给oz的oo,一开始假设全都给zo)
3  4   4

如果oo让给oz一个
那么就变成

oo zo oz+oo
2  4  5

如果再给一个,那么oz+oo就比zo最终所拥有的1多了,但是这是不被允许的,因为我后面并没有把zo的1补回来的办法。那么就要numoo+(int)zo.size()>(int)oz.size()+res+1的时候才能给。
注意这时候oo和zo的1的数量比oz多,那么我们之后把一个zo给oz,这样的话zo+oo的1的数量就会-1,但是oz的1的数量并不会加!
最后只需要加上少掉的zz就可以了。

第二种情况oz>zo。这时候一开始都一样,只要翻转一下,但是注意最后输出不能输出刚才找的这些,因为它是第二场。如果输出了第二场的技能1的话,可能就不对了,因为我们最后还有消去oz的过程。那么我们要用一个vis记录一下那些是第二场的,最后输出的时候判一下即可。

#include<bits/stdc++.h>
using namespace std;
#define pa pair<int,int>
const int N=5e3+5;
int a[N],b[N];
vector<int>ans,zz,zo,oz,oo;
int vis[N];
int main()
{int n,sec=0;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%1d",&a[i]);for(int i=1;i<=n;i++){scanf("%1d",&b[i]);if(a[i]==0&&b[i]==0)zz.push_back(i);else if(a[i]==0&&b[i]==1)zo.push_back(i);else if(a[i]==1&&b[i]==0)oz.push_back(i);else if(a[i]==1&&b[i]==1)oo.push_back(i);}if((int)zo.size()==0&&(int)oz.size()==0&&((int)oo.size())%2==1)return 0*printf("-1\n");if((int)zo.size()-(int)zz.size()>(int)oz.size()+(int)oo.size()||(int)oz.size()-(int)zz.size()>(int)zo.size()+(int)oo.size())return 0*printf("-1\n");//if(n==4990)//printf("%d %d %d %d\n",zo.size(),oz.size(),zz.size(),oo.size());if((int)zo.size()>(int)oz.size()){int i=0;for(int i=0;i<(int)oz.size();i++)printf("%d ",oz[i]);int numoo=(int)oo.size(),res=0;while(numoo+(int)zo.size()>(int)oz.size()+res+1&&numoo){numoo--;printf("%d ",oo[res++]);}while(numoo+(int)zo.size()>(int)oz.size()+res)printf("%d ",zo[i++]),res++;while(i<zo.size()&&oz.size()+res<n/2)printf("%d ",zz[i++]),res++;}else{int azo=0,aoz=0,aoo=0,azz=0;for(int i=0;i<(int)zo.size();i++)vis[zo[i]]=1,azo+=(a[zo[i]]==0&&b[zo[i]]==1);int numoo=oo.size(),res=0;while(numoo+(int)oz.size()>(int)zo.size()+res+1&&numoo){numoo--;vis[oo[res++]]=1,aoo+=(a[oo[res-1]]==1&&b[oo[res-1]]==1);}int i=0;while(i<(int)oz.size()&&numoo+(int)oz.size()>(int)zo.size()+res)vis[oz[i++]]=1,res++,aoz+=(a[oz[0]]==1&&b[oz[0]]==1);//if(n==5000)//printf("%d\n",res);while((int)zo.size()+res<n/2)vis[zz[i]]=1,i++,res++,azz+=(a[zz[i-1]]==0&&b[zz[i-1]]==0);//if(n==4990)//printf("%d %d %d\n",azo+aoo+azz+aoz,azo+aoo,oz.size()-aoz+oo.size()-aoo);for(int i=1;i<=n;i++)if(!vis[i])printf("%d ",i);}printf("\n");return 0;
}

这篇关于Codeforces Contest 1138 problem B Circus —— 死亡1700,暴力的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/881235

相关文章

Codeforces 158B

很久没有刷题了,刚刚有小学弟问了这道题,AC之后贴上来吧。水~~~ #include <cstdio>#include <cmath>int main() {int n;while(scanf("%d", &n) != EOF) {int a = 0, b = 0, c = 0, d = 0;int arr[100001];for (int i = 0; i < n; ++

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee

Codeforces April Fools Day Contest 2013

2013年愚人节的坑题。。。 A. Mysterious strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Input The input contains a sin

Atcoder Beginner Contest 359

传送门 A - Count Takahashi 时间限制:2秒        内存限制:1024MB 分数:100分 问题描述 给定 N 个字符串。 第 i 个字符串  () 要么是 Takahashi 要么是 Aoki。 有多少个 i 使得  等于 Takahashi ? 限制 N 是整数。每个字符串  是 Takahashi 或者 Aoki。() 输入格式

AtCoder Beginner Contest 359 A~C(D~F更新中...)

A.Count Takahashi 题意 给出 N N N个字符串,每个字符串为以下两种字符串之一: "Takahashi" "Aoki" 请你统计"Takahashi"出现了多少次。 分析 输入并统计即可。 代码 #include <bits/stdc++.h>using namespace std;typedef long long ll;void solve() {i

约瑟夫问题(Josephus Problem)4:第k个出列的人是谁

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/16813419 本文是论述约瑟夫问题的第四部分,约瑟夫问题的描述在第一部分,本文用到了第三部分的算法。请先阅读第一部分和第三部分。 现在要求输出第k个出列的人的编号。 由第三部分的算法分析可知,规模为i-1的队列的任意一人的编号为p,规

约瑟夫问题(Josephus Problem)3:谁最后一个出列

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/16813349 本文是论述约瑟夫问题的第三部分,约瑟夫问题的描述在第一部分。请先阅读第一部分。 现在要求输出最后一个出列的人的编号。 第一次见到这个问题是在我高一的时候,那时候搞NOIP,培训的时候碰到了这个题目,当时没想到好的方法,

装箱(背包)问题(Packing Problem)

装箱问题也叫背包问题,简单来说,就是把小货物往大箱子里装,要如何才能装得多。个人常见的经历就是“装冰箱”,很有趣的现象就是常常感觉冰箱再也装不下了,但是经过一翻折腾之后又神奇的装下了。   从企业运作角度来看就是尽量让每个容器(仓库、车辆、集装箱、船等)装的尽量多,可以节约企业的费用。通常,装载率85%左右,使用装箱优化方法后,可以达到90~95%左右。海尔做过一个海运装箱的项目,节约了大量运

[Codeforces 451B] Sort the Array (实现)

Codeforces - 451B 给定一个序列,其中每个数都不相同 问是否能在翻转其中一段后,整个序列变得单调递增 实现题 首先设一个 B B数组为 AA数组排序后的结果 由于只能翻转一个区间,那么我假装 A是满足要求的 找到最小的 A[l]≠B[l] A[l] \ne B[l],最大的 A[r]≠B[r] A[r] \ne B[r], 翻转的区间将会是 [l,r

[Codeforces 451A] Game With Sticks (博弈)

Codeforces - 451A N根横向木棍,M根纵向木棍组成了一个网格图 每次可以选择一个交点,去掉所有通过这个交点的木棍 两个人交替进行这个游戏,问最后谁能胜利 每次选择的一个交点,必然去掉了一根横向木棍和纵向木棍 所以每次 N和 M都减一 当其中有一个为 0的时候,就是先手必败态 所以只和 N、M中较小的那个的奇偶性有关 #pragma comment(link