I have written the code in java but i think logic is clear.
i tried to first add no of buildings A, then added no of buildings seen niether by A or B. then c buildings of height n and then B buildings. I have taken care of overlap of c a and b.
if someone could help with test cases or logic
import java.util.Scanner;
class Solution
{
public static void main(String args[])
{
int t,n,a1,b,c,w,k,i,j,v;
Scanner sc= new Scanner (System.in);
t=sc.nextInt();
w=t;
while(t>0)
{
t--;
n=sc.nextInt();
a1=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
k=n;v=0;
long a[] =new long[n];
System.out.print("Case #"+(w-t)+": ");
if((a1+b+1)!=(n+c))
{System.out.print("IMPOSSIBLE");
System.out.println();}
else
{
i=0;
for(i=0;i<(a1-c);i++)
{
a[i]=2;
}
for(j=0;j<(n+c-(a1+b)) && (a1-c)>0;j++)
{
a[i]=1;
i++;
v++;
}
for(j=1;j<=c;j++)
{
a[i]=n;
i++;
}
for(j=0;j<(n+c-(a1+b)-v) && (b-c)>0;j++)
{
a[i]=1;
i++;
}
for(i=i;i<n;i++)
a[i]=n-2;
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println();
}
}
}
}