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

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;
}

Advertisement

spot_img

Stock market rebound in...

Indian benchmark equity indices witnessed a decisive rebound during...

I Tried Viral Kitchen...

📢 As an Amazon Associate, I earn from qualifying...

Gen Z is spending...

A dramatic transformation is sweeping through the elite corners...

I Tried Viral Kitchen...

📢 As an Amazon Associate, I earn from qualifying...

Live-Action Can You Kiss...

Fans of romantic manga adaptations have an exciting new...

7 Reasons This Viral...

📢 As an Amazon Associate, I earn from qualifying...

Stock market rebound in early trade: Sensex jumps over 400 points, Nifty holds above 22,390

Indian benchmark equity indices witnessed a decisive rebound during early trade, bringing substantial relief to investors following consecutive sessions of choppy price action. The...

I Tried Viral Kitchen Gadgets For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Gen Z is spending more on art than ever before. It’s a key piece of the Great Wealth Transfer, UBS says

A dramatic transformation is sweeping through the elite corners of the global art market. Known largely for digital culture and disruptive social trends, Generation...

I Tried Viral Kitchen Gadgets For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Live-Action Can You Kiss Me First? Series’ Teaser Reveals Theme Song

Fans of romantic manga adaptations have an exciting new television premiere to look forward to, as the upcoming live-action series based on Yamayade's romance...

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Technology Innovation Institute Completes Asteroid-Landing Probe for Landmark Emirates Mission to the Asteroid Belt

In a historic milestone for Middle Eastern space exploration, the Technology Innovation Institute (TII) has officially completed its state-of-the-art asteroid-landing probe. Designed and constructed...

We Tested Viral Kitchen Gadgets Here Is What You Must Know

After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell you exactly what works, what does not, and who...

L&T wins ₹10,000-15,000 crore power transmission orders in India, Middle East

Indian infrastructure and engineering conglomerate Larsen & Toubro (L&T) has achieved another major milestone, securing multiple mega orders within its Power Transmission & Distribution...