CF 1138B Circus

版权声明:没什么好说的,版权声明呢 https://blog.csdn.net/weixin_43741224/article/details/88846622

Description

Polycarp is a head of a circus troupe. There are nn  — an even number — artists in the troupe. It is known whether the ii -th artist can perform as a clown (if yes, then ci=1ci=1 , otherwise ci=0ci=0 ), and whether they can perform as an acrobat (if yes, then ai=1ai=1 , otherwise ai=0ai=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 n2n2 ),
  • 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 nn (2≤n≤50002≤n≤5000 , nn is even) — the number of artists in the troupe.

The second line contains nn digits c1c2…cnc1c2…cn , the ii -th of which is equal to 11 if the ii -th artist can perform as a clown, and 00 otherwise.

The third line contains nn digits a1a2…ana1a2…an , the ii -th of which is equal to 11 , if the ii -th artist can perform as an acrobat, and 00 otherwise.

Output

Print n2n2 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−1 .

Examples

Input

4

0011

0101

Output

1 4

Input

6

000000

111111

Output

-1

Input

8

00100101

01111100

Output

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 11 and 44 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 11. And the number of artists in the second performance who can perform as acrobats is 11 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 33 and 44 should take part. Then in the first performance there are 22 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 22 as well.

题目分析

       这个题目比较麻烦,要为第一个表演选择n/2个人,使得这n/2个人中可以表演的人数总数,等于另外n/2个人中可以表演第二个表演的人的总数。

       我实在写不出来了,那在此,我来详细的说明一下大佬们的写法吧。

       为了避免混淆,我们将在队伍中的人称为——这个人在第一个(第二个)表演的队伍中,这个情况下,这个人是否表演不会有影响;我们将值为1也就是可以表演的人称为——这个人进行第一个(第二个)表演。

       首先我们用把<c[i],a[i]> 当作一个人的信息,第一个数表示这个人在第一个表演的队伍中的情况,第二个数表示这个人在第二个表演的队伍中的情况,如果这个值为1,则可以进行表演,否则,就当作在第一个表演的队伍中,我们可以分为<0,0> <0,1> <1,0> <1,1>四类人,

用x1记录<0,0>类人的总数 ,x2 ---<0,1> , x3----<1,0>,x4 --- <1,1> 。

        然后我们就通过枚举这四类人在第一个表演的队伍中的数量来获取答案,用num_1表示<0,0>类人中在第一个表演的队伍中的人数,num_2表示<0,1>类人中在第一个表演的队伍中的人数,num_3表示<1,0>类人中进行第一个表演的人数,num_4表示<1,1>类人中进行第一个表演的人数,

        为了方便,我们先枚举num_3和num_4 (注意,由于人数限制,num_3+num_4< n/2 ) , 那么num_3+num_4表示这次枚举情况下,两个表演的队伍中可以进行表演的总人数,那么x4-num_4就表示<1,1>这一类中进行第二个表演的人数,而x2表示<1,0>这类人进入第一个表演的队伍和第二个表演队伍的总人数,那么 x2+x4-num_4表示可以进行第二个表演的总人数,那么 (x2+x4-num_4)-(num_3+num_4)就代表<0,1>这类人中选择在第一个表演的队伍中的人数;而后又可以通过 num_1 = n/2 - num_2 - num_3 - num_4 得到<0,0>类人在第一个队伍中的人数。最后,如果num_1,num_2,num_3,num_4均在可行范围内的话,这个情况就一个可行解.

        最后就是输出的情况了,我们枚举每一个人,将每类人中在第一个表演队伍的人的下标输出即可(下标从1开始)。

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int Max = 1e6 + 10;

char c[Max];
char a[Max];
int answer[Max];

int x1, x2, x3, x4;		//分别记录pair<c[i],a[i]>为<0,0>,<0,1>,<1,0>,<1,1>的总个数

int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		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++;		//<0,0>的个数
			}
			else if (c[i] == '0' && a[i] == '1')
			{
				x2++;		//<0,1>的个数
			}
			else if (c[i] == '1' && a[i] == '0')
			{
				x3++;		//<1,0>的个数
			}
			else
			{
				x4++;		//<1,1>的个数
			}
		}
		int num_1 = 0, num_2 = 0, num_3 = 0, num_4 = 0;			 //都代表<x,y>选x的情况的各个配对的个数
		bool ok = false;
		for (num_3 = 0; num_3 <= x3;num_3++)				//枚举<1,0>选1的个数,即第一个表演去的人数
		{
			if (num_3 > n / 2)break;				//第一个表演的人数最大为n/2,超过的话不可能成功
			for (num_4 = 0;num_4 <= x4;num_4++)			//枚举<1,1>中选择第一个表演的人数
			{
				if (num_3 + num_4 > n / 2)break;		//由于i+j表示可以进行第一个表演的人数,所以i+j<n/2
				num_2 = x2 + (x4 - num_4) - (num_3 + num_4);	//x2表示<1,0>的总数,x4-num_4表示<1,1>将1贡献给第二个表演的个数,
									        //即x2+x4-num_4表示在分配好了<1,1>的选择后可进行第二个表演的人数
																//num_3+num_4表示第一个表演的人数
																//(x2+x4-num4)-(num_3+num_4)表示<1,0>中选择0的个数
																//最终num_3表示当两个表演人数相同时num_3选择<1,0>中的0的个数
				num_1 = n / 2 - num_2 - num_3 - num_4;	//<0,0>的个数
				
				if (0 <= num_1 && num_1 <= x1 && 0 <= num_2 && num_2 <= x2)
				{
					ok = true;
					break;
				}
			}
			if (ok)break;
		}
		if(ok)
		{
			int tot = 0;
			for (int i = 1; i <= n; i++)
			{
				if(num_1!=0 && c[i] == '0' && a[i] == '0')
				{
					answer[++tot] = i;
					num_1--;
				}
				else if (num_2 != 0 && c[i] == '0' && a[i] == '1')
				{
					answer[++tot] = i;
					num_2--;
				}
				else if (num_3 != 0 && c[i] == '1' && a[i] == '0')
				{
					answer[++tot] = i;
					num_3--;
				}
				else if (num_4 != 0 && c[i] == '1' && a[i] == '1')
				{
					answer[++tot] = i;
					num_4--;
				}
			}
			printf("%d", answer[1]);
			for(int i = 2 ; i <= tot ; i++)
			{
				printf(" %d", answer[i]);
			}
			printf("\n");
		}
		else
		{
			printf("-1\n");
		}

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43741224/article/details/88846622
今日推荐