Codeforces Round #386 (Div. 2), problem: (G) New Roads Solution in C/C++

Codeforces Round #386 (Div. 2), problem: (G) New Roads Solution in C/C++

#include<stdio.h>
#include<stdlib.h>
typedef unsigned u;
u D[222222],H[222222],P[222222],A[222222];
int main()
{
	u n,d,x,i=-1,j,k,mi=0,ma=1;
	for(scanf("%u%u%u",&n,&d,&x);++i<d;)scanf("%u",D+i);
	for(i=d;i--;ma+=D[i]-1)if(D[i]>D[i+1])mi+=D[i]-D[i+1];
	if(x<mi||x>ma){printf("-1\n");return 0;}
	for(A[i=0]=2;i++<d;)A[i]=A[i-1]+D[i-1];
	for(i=1;++i<A[1];)P[i]=1;
	for(i=0;++i<d;)
	{
		k=D[i-1]-1;
		if(k>D[i]-1)k=D[i]-1;
		j=ma-x;
		if(j>k)j=k;
		ma-=j;
		j=A[i]-1-j;
		for(k=A[i]-1;++k<A[i+1];)
		{
			P[k]=j;
			if(j+1<A[i])++j;
		}
	}
	for(printf("%u\n",A[d]-1);n>1;--n)printf("%u %u\n",n,P[n]);
	return 0;
}

Codeforces Round #386 (Div. 2), problem: (G) New Roads Solution in C/C++

#include<stdio.h>
#include<stdlib.h>
typedef unsigned u;
u D[222222],H[222222],P[222222],A[222222];
int main()
{
	u n,d,x,i=-1,j,k,mi=0,ma=1;
	for(scanf("%u%u%u",&n,&d,&x);++i<d;)scanf("%u",D+i);
	for(i=d;i--;ma+=D[i]-1)if(D[i]>D[i+1])mi+=D[i]-D[i+1];
	if(x<mi||x>ma){printf("-1\n");return 0;}
	for(A[i=0]=2;i++<d;)A[i]=A[i-1]+D[i-1];
	for(i=1;++i<A[1];)P[i]=1;
	for(i=0;++i<d;)
	{
		k=D[i-1]-1;
		if(k>D[i]-1)k=D[i]-1;
		j=ma-x;
		if(j>k)j=k;
		ma-=j;
		j=A[i]-1-j;
		for(k=A[i]-1;++k<A[i+1];)
		{
			P[k]=j;
			if(j+1<A[i])++j;
		}
	}
	for(printf("%u\n",A[d]-1);n>1;--n)printf("%u %u\n",n,P[n]);
	return 0;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

NBA Trade Rumors: Potential RFA Targets, Fallout for Jalen Duren, & the Big Board

The NBA landscape is constantly shifting, and recent contract updates regarding Detroit Pistons center Jalen Duren have created significant ripples across the league’s transactional...

The Ultimate Viral Kitchen Gadgets Guide You Didnt Know You Needed

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

Is it OK to play bird sounds for my lovebird?

Lovebirds are famous for their vibrant personalities, affectionate nature, and expressive vocalizations. Because they are highly social flock animals in the wild, many owners...

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!