cf1417-B. Two Arrays

B. Two Arrays

RedDreamer has an array a consisting of n non-negative integers, and an unlucky integer T.

Let’s denote the misfortune of array b having length m as f(b) — the number of pairs of integers (i,j) such that 1≤i<j≤m and bi+bj=T. RedDreamer has to paint each element of a into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays c and d so that all white elements belong to c, and all black elements belong to d (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that f©+f(d) is minimum possible.

For example:

if n=6, T=7 and a=[1,2,3,4,5,6], it is possible to paint the 1-st, the 4-th and the 5-th elements white, and all other elements black. So c=[1,4,5], d=[2,3,6], and f©+f(d)=0+0=0;
if n=3, T=6 and a=[3,3,3], it is possible to paint the 1-st element white, and all other elements black. So c=[3], d=[3,3], and f©+f(d)=0+1=1.
Help RedDreamer to paint the array optimally!

Input
The first line contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of each test case contains two integers n and T (1≤n≤105, 0≤T≤109) — the number of elements in the array and the unlucky integer, respectively.

The second line contains n integers a1, a2, …, an (0≤ai≤109) — the elements of the array.

The sum of n over all test cases does not exceed 105.

Output
For each test case print n integers: p1, p2, …, pn (each pi is either 0 or 1) denoting the colors. If pi is 0, then ai is white and belongs to the array c, otherwise it is black and belongs to the array d.

If there are multiple answers that minimize the value of f©+f(d), print any of them.

Example
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0

***思路:***把数字分成两份,如果第一份中没有这个k-a[i]那就加进去,如果已经存在就放到另一份中,用map记录是否存过,然后把两边都有的重复数字平均放就行了,比如第三个样例。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t,n;
int a[100007];
map<int,int>mp1;
map<int,int>mp2;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		int k;
		scanf("%d%d",&n,&k);
		mp1.clear();
		mp2.clear();
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		for(int i=1;i<=n;i++)
		{
			if(i==1)
			{
				printf("0");
				mp1[a[i]]++;
			}else{
				if(mp1[k-a[i]]==0)
				{
					printf(" 0");
					mp1[a[i]]++;
				}else if(mp2[k-a[i]]==0){
					printf(" 1");
					mp2[a[i]]++;
				}else
				{
					if(mp1[k-a[i]]>mp2[k-a[i]])
					{
						printf(" 1");
						mp2[a[i]]++;
					}else
					{
						printf(" 0");
						mp1[a[i]]++;
					}
				}
			}
		}
		printf("\n");
	}
	
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值