B. Two Arrays (Thinking + Greedy Distribution)

https://codeforces.com/contest/1417/problem/B


Idea: I feel that it can be done by map in theory.

Greedy consideration, for a number in the sequence, if the number is greater than T/2, you will find that all of them are thrown into black to ensure that none of them will add up to T. If this number is less than T/2, all are thrown into white. Similarly, when it is strictly less than T/2, the sum is not T. For example, T=7, T/2=3. Strictly less than 3+3 will not be reached.

The remaining consideration == T/2.

For example, 4 T=4

2 2 2 2

Manually found that it is the best to throw one white and one pair, that is, evenly distribute to minimize the sum of total logarithms. (Similar to inequality)

If T%2!=0, just throw the white one. The example is sample 1.

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
LL a[maxn];
bool vis[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--)
  {
  	LL n,k;cin>>n>>k;
  	for(LL i=0;i<=n+10;i++) a[i]=vis[i]=0;
  	for(LL i=1;i<=n;i++) cin>>a[i];
  	LL num=0;
  	for(LL i=1;i<=n;i++)
  	{
  		if(a[i]>k/2) vis[i]=1;
		else if(a[i]<k/2) vis[i]=0;
		else if(a[i]==k/2&&k%2!=0)
		{
			vis[i]=0;
		}
		else if(a[i]==k/2&&k%2==0)
		{
			if(num&1) vis[i]=1,num++;
			else if(num%2==0) vis[i]=0,num++;
		}  	
	}
	for(LL i=1;i<=n;i++)
	{
		if(vis[i]==1) cout<<0<<" ";
		else cout<<1<<" ";
	}
	cout<<endl;
  }
return 0;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/108854646