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

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

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

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

Stop Buying Cheap Alternatives This Viral Kitchen Gadgets Is the Real Deal

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

The Agentic Shift: How Meta Is Building the Infrastructure for AI-Powered Marketing

The digital advertising landscape is standing at the precipice of a massive structural evolution known as the agentic shift. As artificial intelligence advances beyond...

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!