cf1138B枚举

本文介绍了一种算法,用于解决马戏团表演人员的分配问题。在一个包含偶数名艺术家的马戏团中,算法的目标是将艺术家分为两组进行表演,确保每组人数相等,且第一组能扮演小丑的艺术家数量等于第二组能扮演杂技演员的艺术家数量。文章详细阐述了算法的输入、输出及实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
Input
4
0011
0101
Output
1 4
Input
6
000000
111111
Output
-1

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
char c[5050];
char a[5050];
int answer[5050];
int x1, x2, x3, x4;	
int main()
{
	int n;
	cin>>n;
	scanf("%s%s", c + 1, a + 1);
		x1 = x2 = x3 = x4 = 0;
		for (int i = 1; i <= n; i++)
		{
			if (c[i] == '0' && a[i] == '0')
				x1++;	
			else if (c[i] == '0' && a[i] == '1')
				x2++;		
			else if (c[i] == '1' && a[i] == '0')
				x3++;	
			else
				x4++;		
		}
		int num1 = 0, num2 = 0, sum3 = 0, sum4 = 0;			 
		bool ok = false;
		for (sum3 = 0; sum3 <= x3;sum3++)			
		{
			if (sum3 > n / 2)break;				
			for (sum4 = 0;sum4 <= x4;sum4++)			
			{
				if (sum3 + sum4 > n / 2)break;		
				num2 = x2 + (x4 - sum4) - (sum3 + sum4);
				num1 = n / 2 - num2 - sum3 - sum4;					
				if (0 <= num1 && num1 <= x1 && 0 <= num2 && num2 <= x2)
				{
					ok = true;
					break;
				}
			}
			if (ok)break;
		}
		if(ok)
		{
			int tot = 0;
			for (int i = 1; i <= n; i++)
			{
				if(num1!=0 && c[i] == '0' && a[i] == '0')
				{
					if(tot==0)		
					{
						cout<<i;
						tot=1;
					}			 					
					else
					cout<<" "<<i;
					num1--;
				}
				else if (num2 != 0 && c[i] == '0' && a[i] == '1')
				{
					if(tot==0)		
					{
						cout<<i;
						tot=1;
					}
					else
					cout<<" "<<i;
					num2--;
				}
				else if (sum3 != 0 && c[i] == '1' && a[i] == '0')
				{
					if(tot==0)		
					{
						cout<<i;
						tot=1;
					}
					else
					cout<<" "<<i;
					sum3--;
				}
				else if (sum4 != 0 && c[i] == '1' && a[i] == '1')
				{
					if(tot==0)		
					{
						cout<<i;
						tot=1;
					}
					else
					cout<<" "<<i;
					sum4--;
				}
			}		
			cout<<endl;
		}
		else
			cout<<-1<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值