Codeforces Round #386 (Div. 2), problem: (E) Numbers Exchange Solution in C/C++

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

Codeforces Round #386 (Div. 2), problem: (E) Numbers Exchange Solution in C/C++

#include<stdio.h>
#include<stdlib.h>
typedef unsigned u;
u N[222222],X[222222],C[222222],H[222222];
int F(const void*x,const void*y)
{
	if(N[*(u*)x]>N[*(u*)y])return 1;
	if(N[*(u*)x]<N[*(u*)y])return-1;
	return 0;
}
int main()
{
	u n,m,i,o,e,h,os=1,es=2,g=0;
	scanf("%u%u",&n,&m);h=n>>1;
	if(n&1){printf("-1\n");return 0;}
	for(i=-1;++i<n;X[i]=i)scanf("%u",N+i);
	qsort(X,n,sizeof(u),F);
	for(i=o=e=0;++i<n;)if(N[X[i]]==N[X[i-1]])C[X[i]]=1;
	for(i=n;i--;)if(!C[X[i]])
	{
		if(N[X[i]]&1)
		{
			if(o==h)C[X[i]]=1;
			else++o;
		}
		else
		{
			if(e==h)C[X[i]]=1;
			else++e;
		}
		if(!C[X[i]])
		{
			++g;
			if(N[X[i]]<222222)H[N[X[i]]]=1;
		}
	}
	for(i=-1;++i<n;)if(C[X[i]])
	{
		if(e<h)
		{
			while(H[es])es+=2;
			if(es>m){printf("-1\n");return 0;}
			N[X[i]]=es;es+=2;++e;
		}
		else if(o<h)
		{
			while(H[os])os+=2;
			if(os>m){printf("-1\n");return 0;}
			N[X[i]]=os;os+=2;++o;
		}
	}
	printf("%u\n",n-g);
	for(i=-1;++i<n;)printf("%u%c",N[i],"\n "[i+1<n]);
	return 0;
}

📦 Looking for Codeforces Round 386 Div Problem Numbers? Check the best deals on Amazon.

🛒 Shop Codeforces Round 386 Div Problem Numbers on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

Codeforces Round #386 (Div. 2), problem: (E) Numbers Exchange Solution in C/C++

#include<stdio.h>
#include<stdlib.h>
typedef unsigned u;
u N[222222],X[222222],C[222222],H[222222];
int F(const void*x,const void*y)
{
	if(N[*(u*)x]>N[*(u*)y])return 1;
	if(N[*(u*)x]<N[*(u*)y])return-1;
	return 0;
}
int main()
{
	u n,m,i,o,e,h,os=1,es=2,g=0;
	scanf("%u%u",&n,&m);h=n>>1;
	if(n&1){printf("-1\n");return 0;}
	for(i=-1;++i<n;X[i]=i)scanf("%u",N+i);
	qsort(X,n,sizeof(u),F);
	for(i=o=e=0;++i<n;)if(N[X[i]]==N[X[i-1]])C[X[i]]=1;
	for(i=n;i--;)if(!C[X[i]])
	{
		if(N[X[i]]&1)
		{
			if(o==h)C[X[i]]=1;
			else++o;
		}
		else
		{
			if(e==h)C[X[i]]=1;
			else++e;
		}
		if(!C[X[i]])
		{
			++g;
			if(N[X[i]]<222222)H[N[X[i]]]=1;
		}
	}
	for(i=-1;++i<n;)if(C[X[i]])
	{
		if(e<h)
		{
			while(H[es])es+=2;
			if(es>m){printf("-1\n");return 0;}
			N[X[i]]=es;es+=2;++e;
		}
		else if(o<h)
		{
			while(H[os])os+=2;
			if(os>m){printf("-1\n");return 0;}
			N[X[i]]=os;os+=2;++o;
		}
	}
	printf("%u\n",n-g);
	for(i=-1;++i<n;)printf("%u%c",N[i],"\n "[i+1<n]);
	return 0;
}

📦 Looking for Codeforces Round 386 Div Problem Numbers? Check the best deals on Amazon.

🛒 Shop Codeforces Round 386 Div Problem Numbers on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

What’s new in Gutenberg 24.0? (16 September)

The WordPress open-source community has officially unveiled Gutenberg 24.0, marking another milestone in the project's biweekly development cycle. This release brings a focused batch...

The Real Truth About Viral Kitchen Gadgets Before You Buy

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Is Viral Kitchen Gadgets Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!