Problem:
 Score : 800 points    Problem Statement You are given an integer sequence x of length N .
Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a .   a is N^2 in length, containing N copies of each of the integers 1 , 2 , ... , N .  For each 1 ≤ i ≤ N , the i -th occurrence of the integer i from the left in a is the x_i -th element of a from the left.       Constraints  1 ≤ N ≤ 500  1 ≤ x_i ≤ N^2  All x_i are distinct.         Input The input is given from Standard Input in the following format:  N  x_1  x_2  ...  x_N       Output If there does not exist an integer sequence a that satisfies all the conditions, print No .
If there does exist such an sequence a , print Yes in the first line, then print an instance of a in the second line, with spaces inbetween.        Sample Input 1 3
1 5 9      Sample Output 1 Yes
1 1 1 2 2 2 3 3 3  For example, the second occurrence of the integer 2 from the left in a in the output is the fifth element of a from the left.
Similarly, the condition is satisfied for the integers 1 and 3 .       Sample Input 2 2
4 1      Sample Output 2 No

Solution (Python):
import sys
N = int(input())
a = [-1]*N**2
x = list(map(lambda x:int(x)-1, input().split()))
x = list(zip(x, list(range(1, N+1))))
x.sort()
now = 0
for v, i in x:
    if a[v]>=0:
        print('No')
        sys.exit()
    a[v] = i
    cnt = 0
    while cnt < i-1:
        if now==v:
            print('No')
            sys.exit()
        if a[now]<0:
            a[now] = i
            cnt+=1
        now+=1
now = N*N-1
for v, i in reversed(x):
    cnt = 0
    while cnt<N-i:
        if now<=v:
            print('No')
            sys.exit()
        if a[now]<0:
            a[now] = i
            cnt+=1
        now-=1
if all(v >= 0 for v in a):
    print('Yes')
    print(*a)
else:
    print('No')

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
X = sorted((x,i) for i,x in enumerate((int(x) for x in input().split()),1))

arr = []
for x,i in X:
    arr += [i] * (i-1)
for x,i in X:
    arr += [i] * (N-i)

x_to_i = dict(X)

arr = arr[::-1]
answer = []
counter = [0] * (N+1)
bl = True
for x in range(1,N*N+1):
    if x in x_to_i:
        i = x_to_i[x]
        counter[i] += 1
        bl &= (counter[i] == i)
    else:
        i = arr.pop()
        counter[i] += 1
    answer.append(i)

if bl:
    print('Yes')
    print(*answer)
else:
    print('No')

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    x,y = LI()
    if x < 0:
        if y < 0:
            if x < y:
                print(y-x)
            else:
                print(x-y+2)
        else:
            if abs(x) < y:
                print(y-abs(x)+1)
            else:
                print(-y-x+(y>0))
    else:
        if y < 0:
            if x < abs(y):
                print(abs(y)-x+1)
            else:
                print(x+y+1)
        else:
            if x < y:
                print(y-x)
            else:
                print(x-y+(x>0)+(y>0))
    return


def B():

    return


def C():
    a = LI()
    if a[0] > 0 and a[3] > 0 and a[4] > 0:
        print(max(2*(a[0]//2)+a[1]+2*(a[3]//2)+2*(a[4]//2), 2*((a[0]-1)//2)+a[1]+2*((a[3]-1)//2)+2*((a[4]-1)//2)+3))
    else:
        print(2*(a[0]//2)+a[1]+2*(a[3]//2)+2*(a[4]//2))
    return


def D():
    n = I()
    x = LI()
    f = [(x[i],i) for i in range(n)]
    f.sort()
    b = []
    for xi,i in f:
        b += [i+1]*i
    for xi,i in f:
        b += [i+1]*(n-i-1)
    ans = []
    l = 0
    for xi,i in f:
        while len(ans)+1 < xi:
            if l == len(b):
                break
            ans.append(b[l])
            l += 1
        ans.append(i+1)
    for i in range(l,len(b)):
        ans.append(b[i])
    f = [0]*n
    for i in range(len(ans)):
        ai = ans[i]-1
        if f[ai] == ai:
            if i+1 != x[ai]:
                print("No")
                return
        f[ai] += 1
    print("Yes")
    print(*ans)
    return


def E():

    return


def F():

    return


if __name__ == "__main__":
    D()

Solution (Python):
import itertools as ite
import math
import random

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
x = map(int, raw_input().split())

ls = []
for i in range(N):
    ls += [(x[i] - 1, i + 1)]
ls.sort()
ans = [-1] * (N * N)

pos = 0
for id, num in ls:
    ans[id] = num
    cnt = num - 1
    while cnt > 0:
        if pos >= id:
            print "No"
            exit()
        if ans[pos] == -1:
            ans[pos] = num
            cnt -= 1
        pos += 1

pos = N * N - 1

for id, num in ls[::-1]:
    cnt = N - num
    while cnt > 0:
        if pos <= id:
            print "No"
            exit()
        if ans[pos] == -1:
            ans[pos] = num
            cnt -= 1
        pos -= 1

print "Yes"
print " ".join(map(str, ans))

Solution (Python):
N=int(input())
X=[]
for i,x in enumerate(input().split()):
  X.append((int(x)-1,i+1))
X.sort()
a,b,c,k,l=0,0,0,0,0
ans=[]
for i in range(N*N):
  if b<=N-1:
    x,p=X[b]
    if x==i:
      if a<=b and p!=1:
        print("No")
        exit()
      ans.append(p)
      b+=1
      continue
  if a<=N-1:
    y,q=X[a]
    if q==1:
      a+=1
      if a==N:
        z,r=X[c]
        if c>=b:
          print("No")
          exit()
        if r==N:
          c+=1
          l=0
          z,r=X[c]
        ans.append(r)
        l+=1
        if l==N-r:
          c+=1
          l=0
        continue
      else:
        y,q=X[a]
        k=0
    ans.append(q)
    k+=1
    if k==q-1:
      a+=1
      k=0
  else:
    z,r=X[c]
    if c>=b:
      print("No")
      exit()
    if r==N:
      c+=1
      l=0
      z,r=X[c]
    ans.append(r)
    l+=1
    if l==N-r:
      c+=1
      l=0
  
print("Yes")
print(" ".join(map(str,ans)))

Solution (Python):
N = int(input())
X = list(map(int,input().split()))
Y = sorted([(x-1,i+1) for i,x in enumerate(X)])

ans = [None] * (N*N)
must = []
for i,n in Y:
    ans[i] = n
    must += [n]*(n-1)
must.reverse()

used = [0] * (N+1)
free = [1]
for i in range(N*N):
    if ans[i]:
        a = ans[i]
        if used[a] != a-1:
            print('No')
            exit()
        free += [a]*(N-a)
    elif must:
        a = must.pop()
        ans[i] = a
    else:
        if free:
            a = free.pop()
            ans[i] = a
        else:
            print('No')
            exit()
    used[ans[i]] += 1

for i in range(1,N+1):
    if used[i] != N:
        print('No')
        exit()

print('Yes')
print(*ans)

Solution (Python):
from collections import deque
from collections import defaultdict
n = int(input())
x = list(map(int,input().split()))
for i in range(n):
  if x[i]-1 < i or x[i]-1 > n*n-n+i:
    print("No")
    exit()
    
ans = [-1]*n*n
for i in range(n):
  ans[x[i]-1] = i+1
x2 = []
for i in range(n):
  x2.append((x[i],i+1))
x2.sort()
yusen = deque([])
ato = deque([])
for i in range(n):
  yusen.extend([x2[i][1]]*(x2[i][1]-1))
  ato.extend([x2[i][1]]*(n-x2[i][1]))
for i in range(n*n):
  if ans[i] == -1:
    if yusen:
      ko = yusen.popleft()
      ans[i] = ko
    else:
      ko = ato.popleft()
      ans[i] = ko
d = defaultdict(int)
for i in range(n*n):
  d[ans[i]] += 1
  if d[ans[i]] == ans[i]:
    if i+1 != x[ans[i]-1]:
      print("No")
      exit()
print("Yes")
print(*ans)

Solution (Python):
N = int(input())
X = list(map(int, input().split()))
A = [-1]*N*N
sorted_X = sorted(X)
first_empty = 0
last_empty = N*N-1
fail_flag = 0
for i in range(N):
	x = sorted_X[i]
	K = X.index(x)+1
	if A[x-1]==-1:
		A[x-1] = K
	else:
		print("No")
		fail_flag = 1
		break
	K_rest = K-1
	while K_rest>0:
		while True:
			if A[first_empty]==-1:
				break
			first_empty = first_empty+1
		if first_empty<x-1:
			A[first_empty] = K
			K_rest = K_rest-1
			first_empty = first_empty+1
		else:
			print("No")
			fail_flag = 1
			break
	if fail_flag==1:
		break

if fail_flag==0:
	for i in range(N-1,-1,-1):
		x = sorted_X[i]
		K = X.index(x)+1
		K_rest = N-K
		while K_rest>0:
			while True:
				if A[last_empty]==-1:
					break
				last_empty = last_empty-1
			if last_empty>x-1:
				A[last_empty] = K
				K_rest = K_rest-1
				last_empty = last_empty-1
			else:
				print("No")
				fail_flag = 1
				break
		if fail_flag==1:
			break

if fail_flag==0:
	print("Yes")
	for a in A:
		print(str(a), end=" ")

Solution (Python):
n=int(raw_input())
x=map(int,raw_input().split())
ans=[-1]*(n**2+1)
for i in xrange(n):
    if ans[x[i]]!=-1:
        print("No")
        exit()
    ans[x[i]]=i+1
for i in xrange(1,n**2+1):
    if ans[i]!=-1:
        cnt=ans[i]-1
        for j in xrange(1,i):
            if cnt==0:break
            if ans[j]==-1:
                cnt-=1
                ans[j]=ans[i]
        if cnt>0:
            print("No")
            exit()

check=[False]*(n+1)
for i in xrange(n**2,0,-1):
    if ans[i]!=-1 and (not check[ans[i]]):
        cnt=n-ans[i]
        check[ans[i]]=True
        for j in xrange(n**2,i,-1):
            if cnt==0:break
            if ans[j]==-1:
                cnt-=1
                ans[j]=ans[i]
        if cnt>0:
            print("No")
            exit()
print("Yes")
print(" ".join(map(str,ans[1:])))

Solution (Python):
N = int(input())
ans = [-1] * (N ** 2)
X = [int(x)-1 for x in input().split()]
for i, x in enumerate(X):
    ans[x] = i+1
left, right = [], []
for i in range(N):
    l_cnt, r_cnt = i, N - (i+1)
    left.extend([(X[i], i+1)] * l_cnt)
    right.extend([(X[i], i+1)] * r_cnt)
l, r = 0, N ** 2 - 1
left.sort()
right.sort(reverse=True)
for _, x in left:
    while ans[l] != -1:
        l += 1
    ans[l] = x
for _, x in right:
    while ans[r] != -1:
        r -= 1
    ans[r] = x
counter = [[] for _ in range(N)]
for i, a in enumerate(ans):
    counter[a-1].append(i)
if all(counter[i].index(x) == i for i, x in zip(range(N), X)):
    print("Yes")
    print(*ans)
else:
    print("No")

Solution (Python):
N = int(input())
ans = [-1] * (N ** 2)
X = [int(x)-1 for x in input().split()]
for i, x in enumerate(X):
    ans[x] = i+1
left, right = [], []
for i in range(N):
    l_cnt, r_cnt = i, N - (i+1)
    left.extend([(X[i], i+1)] * l_cnt)
    right.extend([(X[i], i+1)] * r_cnt)
l, r = 0, N ** 2 - 1
left.sort()
right.sort()
for _, x in left:
    while ans[l] != -1:
        l += 1
    ans[l] = x
for _, x in reversed(right):
    while ans[r] != -1:
        r -= 1
    ans[r] = x
counter = [[] for _ in range(N)]
for i, a in enumerate(ans):
    counter[a-1].append(i)
if all(counter[i].index(x) == i for i, x in zip(range(N), X)):
    print("Yes")
    print(*ans)
else:
    print("No")
assert(all(a != -1 for a in ans))
assert(all(len(row) == N for row in counter))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7


def main():
    n = int(input())
    a = list(map(int, input().split()))
    nn = n*n
    r = [-1] * nn
    ms = []
    us = []
    for i in range(n):
        c = i+1
        ri = a[i] - 1
        r[ri] = c
        ms.append([ri, i, c])
        us.append([ri, n-i-1, c])
    ms.sort()
    si = 0
    for ri, k, c in ms:
        for _ in range(k):
            while r[si] > 0 and si < (nn-1):
                si += 1
            if si > ri or r[si] > 0:
                print('No')
                return
            r[si] = c

    si = nn-1
    us.sort()
    us.reverse()
    for ri, k, c in us:
        for _ in range(k):
            while r[si] > 0 and si > 0:
                si -= 1
            if si < ri or r[si] > 0:
                print('No')
                return
            r[si] = c

    print('Yes')
    print(' '.join(map(str, r)))


main()

Solution (Python):
def main():
    n = int(raw_input())
    b = map(int, raw_input().split())
    m = n * n
    a = [0] * m
    for i, x in enumerate(b):
        a[x-1] = i + 1
    j = 0
    for i in xrange(m):
        if a[i] > 0:
            x = a[i]
            for k in xrange(x - 1):
                while a[j] != 0:
                    if j >= i:
                        print "No"
                        return
                    j += 1
                a[j] = -x
    j = m - 1
    for i in xrange(m - 1, -1, -1):
        if a[i] > 0:
            x = a[i]
            for k in xrange(n - x):
                while a[j] != 0:
                    if j <= i:
                        print "No"
                        quit()
                        return
                    j -= 1
                a[j] = -x
    print "Yes"
    for x in a:
        print x if x > 0 else -x,
    print
main()