Problem:


Score : 400 points

Problem Statement

We have a grid with H rows and W columns of squares.
Snuke is painting these squares in colors 1, 2, ..., N.
Here, the following conditions should be satisfied:

For each i (1 ≤ i ≤ N), there are exactly a_i squares painted in Color i. Here, a_1 + a_2 + ... + a_N = H W.

For each i (1 ≤ i ≤ N), the squares painted in Color i are 4-connected. That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i.

Find a way to paint the squares so that the conditions are satisfied.
It can be shown that a solution always exists.

Constraints

1 ≤ H, W ≤ 100

1 ≤ N ≤ H W

a_i ≥ 1

a_1 + a_2 + ... + a_N = H W

Input

Input is given from Standard Input in the following format:

H W
N
a_1 a_2 ... a_N

Output

Print one way to paint the squares that satisfies the conditions.
Output in the following format:

c_{1 1} ... c_{1 W}
:
c_{H 1} ... c_{H W}

Here, c_{i j} is the color of the square at the i-th row from the top and j-th column from the left.

Sample Input 1

2 2
3
2 1 1

Sample Output 1

1 1
2 3

Below is an example of an invalid solution:

1 2
3 1

This is because the squares painted in Color 1 are not 4-connected.

Sample Input 2

3 5
5
1 2 3 4 5

Sample Output 2

1 4 4 4 3
2 5 4 5 3
2 5 5 5 3

Sample Input 3

1 1
1
1

Sample Output 3

1



Sample input:
2 2
3
2 1 1


Sample output:
1 1
2 3

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
from operator import itemgetter
#以下てんぷら
def j(q):
    if q==1: print("YES")
    else:print("NO")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())
#n = ip()                                     #入力整数1つ
h,w= (int(i) for i in input().split())       #入力整数横2つ以上
#a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
#こっから本文
n = ip()
a = [int(i) for i in input().split()]
horz = 1
vert = 1
x = 0
y = 0
ans  = [[0 for i in range(w)] for i in range(h)]
pos = 0
for i in range(h*w):
    ans[y][x] = pos+1
    a[pos]-=1
    if a[pos] ==0:
        pos+=1
    if y == h-1 and vert == 1:
        x+=1
        vert = -1
    elif y == 0 and vert == -1:
        x+=1
        vert = 1
    else: y+=vert
for i in range(h):
    for k in range(w):
        if k+1 != w: print(ans[i][k],"",end="")
        else : print(ans[i][k],end="")
    print()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))

l=[]
for i in range(n):
    for j in range(a[i]):
      l.append(i+1)
ans=[]
for i in range(h):
    if i%2==1:
        x=l[i*w:(i+1)*w]
        ans.append(x[::-1])
    else:
        ans.append(l[i*w:(i+1)*w])
for i in range(h):
    print(*ans[i])

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
res = [[-1 for _ in range(W)] for _ in range(H)]
tmp = []
for i in range(N):
    for j in range(A[i]):
        tmp.append(i + 1)
        
for i in range(H):
    result = ''
    for j in range(W):
        if i%2 == 0:
            result = result + str(tmp[W * i + j])
            if j != W - 1:
                result = result + ' '
        else:
            result =  str(tmp[W * i + j]) + result
            if j != W - 1:
                result = ' ' + result 
            
    print(result)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
ANS = [ [0]*W for _ in range(H)]
nc=1
nct=A[0]
ans=[]
for y in range(H):
  if y%2==0:
    for x in range(W):
      nct-=1
      ANS[y][x] = str(nc)
      if nct==0 and nc<N:
        nct=A[nc]
        nc+=1
  else:
    for x in range(W-1,-1,-1):
      nct-=1
      ANS[y][x] = str(nc) 
      if nct==0 and nc<N:
        nct=A[nc]
        nc+=1
        
      
for y in range(H):
    print(" ".join(ANS[y]) )

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
N = int(input())
L = list(map(int,input().split()))
M = [[] for i in range(H)]
c = 0
x = 0
y = 0
for i in range(N):
  while c < L[i]:
    if x != W:
      M[y].append(i+1)
      c += 1
      x += 1
    else:
      y += 1
      x = 1
      M[y].append(i+1)
      c += 1
  c = 0
for i in range(H):
  if i%2 != 0:
    M[i].reverse()
for j in range(H):
  print(' '.join(map(str,M[j])))

Submission status: Accepted

Solution code:
H, W = (int(i) for i in input().split())
N = int(input())
A = [int(i) for i in input().split()]
C = []
for i in range(N):
  for j in range(A[i]):
    C.append(i+1)
ans = [[0]*W for i in range(H)]
for i in range(H*W):
  y = i // W
  if y%2==0:
    x = i%W
  else:
    x = W-1-i%W
  ans[y][x] = C[i]
for i in range(H):
  ans[i] = [str(a) for a in ans[i]]
  print(" ".join(ans[i]))

Submission status: Accepted

Solution code:
# use faster method than input()
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))

l = [0]*(H*W)
prev = 0
for i, a in enumerate(A):
    color = i+1
    cnt = 0
    while cnt < a:
        l[cnt+prev] = color
        cnt += 1
    prev = prev+a

for i in range(H):
    sub_l = l[W*i:W*(i+1)]
    if i % 2 == 0:
        print(" ".join(map(str, sub_l)))
    else:
        sub_l = sub_l[::-1]
        print(" ".join(map(str, sub_l)))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
N = int(input())
arr = [0] + list(map(int,input().split()))
c = [[-1]*W for _ in range(H)]
goto = [[0,1],[1,0],[0,-1],[-1,0]] #right,down,left,up


q = 1
h = 0
w = 0
cnt = 0
g = 0
count = 0

def judge(tmph,tmpw):
    ok = False
    if tmph >= 0 and tmph < H and tmpw >= 0 and tmpw < W:
        if c[tmph][tmpw] < 0:
            ok = True
    return ok
for _ in range(H*W):
    c[h][w] = q
    cnt += 1
    if cnt == arr[q]:
        q += 1
        cnt = 0
    tmph = h + goto[g][0]
    tmpw = w + goto[g][1]
    for i in range(4):
        if judge(tmph,tmpw):
            break
        g += 1
        g %= 4
        tmph = h + goto[g][0]
        tmpw = w + goto[g][1]
    h = tmph
    w = tmpw

for i in c:
    print(*i)

Submission status: Accepted

Solution code:
H, W = [int(_) for _ in input().split()]
N = int(input())
A = [int(_) for _ in input().split()]
B = []
for i in range(N):
    B += [i + 1] * A[i]
for j in range(H):
    print(' '.join(map(str, B[j * W:(j + 1) * W][::(-1)**(1&j)])))

Submission status: Accepted

Solution code:
a,b= map(int,input().split(" "))
c = int(input())
ar = list(map(int,input().split(" ")))
br = [[0 for i in range(b)] for j in range(a)]
go = "M"
ap = 1
for i in range(a):
    if go == "M":
        for j in range(b):
            if ar[0] == 0:
                del ar[0]
                ap += 1
            br[i][j] = ap
            ar[0] -= 1
        go = "U"
    else:
        for j in range(b-1,-1,-1):
            if ar[0] == 0:
                del ar[0]
                ap += 1
            br[i][j] = ap
            ar[0] -= 1
        go = "M"
for r in br:
    for i in range(b-1):
        print(r[i],end=" ")
    print(r[b-1])

Submission status: Accepted

Solution code:

H,W = list(map(int,input().split()))
N = int(input())
a = list(map(int,input().split()))

ans = []

current_a=a[0]
current_id = 1
for i in range(H):
    w_ans = []
    for j in range(W):
        if current_a == 0:
            current_a = a[current_id]
            current_id += 1
        
        current_a -= 1
        w_ans.append(str(current_id))
    if i%2==1:
        w_ans = w_ans[::-1]
    ans.append(w_ans)

for i in range(H):
    print(" ".join(ans[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
ret=[["0" for i in range(w)]for j in range(h)]
x=0
y=0
for i in range(n):
    for j in range(a[i]):
        ret[y][x]=str(i+1)
        if y&1:
            if x==0:
                y+=1
            else:
                x-=1
        else:
            if x==w-1:
                y+=1
            else:
                x+=1
for i in range(h):
    print(" ".join(ret[i]))

Submission status: Accepted

Solution code:
import numpy as np
h,w=map(int, input().split())
n=int(input())
a=list(map(int, input().split()))
m=np.arange(w*h)
c=0
for i,j in enumerate(a):
    m[c:c+j]=i+1
    c+=j
m=m.reshape(h,w)
m[::2,:]=m[::2,::-1]
for i in m:print(*i)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
colors = []
for color in range(1, N + 1):
    for j in range(a[color - 1]):
        colors.append(str(color))
canvas = []
for i in range(H):
    if i % 2 == 0:
        canvas.append(colors[i * W: i * W + W])
    else:
        canvas.append(colors[i * W + W - 1: i * W - 1: -1])
for line in canvas:
    print(' '.join(line))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
N = int(input())
L = list(map(int,input().split()))
A = []
for i in range(N) :
    a = [str((i+1))]*L[i]
    A = A + a
for i in range(H) :
    p = A[W*i:W*(i+1)]
    if i % 2 != 0 :
        p = list(reversed(p))
    ans = " ".join(p)
    print(ans)

Submission status: Accepted

Solution code:

def inpl(): return [int(i) for i in input().split()]
def nextin(nwin,prin):
    if prin[1] != nwin[1]+1 and nwin[1] != W-1:
        return [nwin[0],nwin[1]+1]
    if prin[1] != nwin[1]-1 and nwin[1] != 0:
        return[nwin[0],nwin[1]-1]
    else:
        return[nwin[0]+1,nwin[1]]

H, W = inpl()
HW = [[0 for _ in range(W)] for _ in range(H)]
N = int(input())
a = inpl()
previn = [0,-1]
nowin = [0,0]
for i in range(1,N+1):
    for _ in range(a[i-1]):
        nh,nw = nowin
        HW[nh][nw] = i
        nowin = nextin(nowin,previn)
        previn = [nh,nw]
for i in HW:
    print(*i)

Submission status: Accepted

Solution code:
import itertools
h, w=map(int, raw_input().split())
n=input()
a=list(itertools.chain(*[[c+1]*a for c, a in enumerate(map(int, raw_input().split()))]))
for i in xrange(h):
	for j in xrange(w):
		print a[i*w+j if i%2==0 else (i+1)*w-j-1],
	print

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
h,w = map(int, input().split())
n = int(input())

l = []
a = [int(x) for x in input().split()]
for i in range(n):
    l.extend([i+1 for _ in range(a[i])])

for hi in range(h):
    line = []
    for wi in range(w):
        line.append(l[w*hi+wi])
    if hi%2==1:
        line = line[::-1]

    print(*line)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
ans = []
for i, a in enumerate(A, start=1):
    ans += [i] * a
for i in range(H):
    if i % 2:
        print(*ans[i * W: (i + 1) * W][::-1])
    else:
        print(*ans[i * W: (i + 1) * W])

Submission status: Accepted

Solution code:
def getN():
    return int(input())


def getNM():
    return map(int, input().split())


def getList():
    return list(map(int, input().split()))

w,h = getNM()
n = getN()
nums = getList()

seq = []
for i in range(n):
    seq = seq + [(i+1) for kazu in range(nums[i])]

for dan in range(w):
    subset = seq[h*dan:h*(dan+1)]
    if dan % 2 == 0:
        print(" ".join(map(str,subset)))
    else:
        print(" ".join(map(str, subset[::-1])))

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
n = int(input())
list_a = list(map(int, input().split()))
list_a_post = []
for i in range(n):
    for j in range(list_a[i]):
        list_a_post.append(i+1)
list_ans = []
p = 0
for i in range(h):
    memo_list = []
    for j in range(w):
        memo_list.append(list_a_post[p])
        p += 1
    if i % 2 == 1:
        memo_list.reverse()
    list_ans.append(memo_list)
for i in range(h):
    for j in range(w):
        list_ans[i][j] = str(list_ans[i][j])
for row in list_ans:
    print(" ".join(row))

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

H, W = map(int, raw_input().split())
N = input()
a = map(int, raw_input().split())

pos = 0
ans = [[0] * W for i in range(H)]
for i in range(H):
    R = range(W) if i % 2 else range(W - 1, -1, -1)
    ls = []
    for j in R:
        if a[pos] == 0:
            pos += 1
        a[pos] -= 1
        ans[i][j] = pos + 1

for ls in ans:
    print " ".join(map(str, ls))

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
l = []
for i,m in enumerate(a,1):
    l += [i]*m
for i in range(h):
    k = l[:w]
    l = l[w:]
    if i&1:
        k.reverse()
    print(*k)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))

ans = [[0] * W for _ in range(H)]

row_is_even = True

def next_grid(h, w):
	if h % 2 == 0:
		if w == W - 1:
			return h + 1, w
		else:
			return h, w + 1
	else:
		if w == 0:
			return h + 1, w
		else:
			return h, w - 1

def color(h, w, num, cnt):
	for _ in range(cnt):
		ans[h][w] = num
		h, w = next_grid(h, w)
	return h, w

h, w = color(0, 0, 1, a[0])
for i in range(1, n):
	h, w = color(h, w, i + 1, a[i])

for i in range(H):
	print(*ans[i])

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

H, W = reads()
N = read()
A = reads()

field = [[0] * W for _ in range(H)]
ls = sorted(product(range(H), range(W)), key=lambda p: (p[0], (-1)**p[0] * p[1]))
for i, j in ls:
  field[i][j] = len(A)
  A[-1] -= 1
  if A[-1] == 0:
    A.pop()

for i in range(H):
  print(" ".join(str(x) for x in field[i]))

Submission status: Accepted

Solution code:
s=input().split()
h,w=int(s[0]),int(s[1])
n=int(input())
s=input().split()
l=[int(s[i]) for i in range(n)]
res=[]
for i in range(1,n+1):
    res+=[i]*l[i-1]
for i in range(h):
    ll=res[i*w:i*w+w]
    if i%2==1:
        ll=ll[-1::-1]
    for j in ll[:-1]:
        print(j,end=" ")
    print(ll[-1])

Submission status: Accepted

Solution code:

h,w = map(int,input().split())	
n = int(input())
a = list(map(int,input().split()))
ass = []

for i in range(n):
    for j in range(a[i]):
        ass.append(i+1)

#print(ass)

k = 1
l = 1
ans = ''

for i in range(h):
    ans = ''
    for j in range(w):
        if l == 1:
            if i == h-1 and j == w-1:
                ans += str(ass[i*w+j])
            else:
                ans += str(ass[i*w+j]) + ' '
        else:
            if i == h-1 and j == w-1:
                ans += str(ass[(i+1)*w-j-1])
            else:
                
                ans += str(ass[(i+1)*w-j-1]) + ' '
    l = l * (-1)
    print(ans)

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))

    grid = [[0 for j in range(w)] for i in range(h)]
    i, j, idx = 0, 0, 0
    while idx < n:
        while 0 < a[idx]:
            grid[i][j] = idx + 1
            a[idx] -= 1

            if i % 2 == 0:
                j += 1
                if j == w:
                    j = w - 1
                    i += 1
            else:
                j -= 1
                if j == -1:
                    j = 0
                    i += 1
        idx += 1

    for i in range(h):
        print(" ".join([str(j) for j in grid[i]]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h,w = map(int,raw_input().split())

n = int(input())

arr = map(int,raw_input().split())

field = [[0 for i in range(w)] for j in range(h)]


x = 0
y = 0
d = 1
p = 0

for i in range(h*w):

	if arr[p] == 0:
		p += 1

	field[y][x] = str(p + 1)
	arr[p] -= 1

	x = x + d
	if x == -1 or x == w:
		x -= d
		y += 1
		d *= -1

for i in range(h):
	print ' '.join(field[i])

Submission status: Accepted

Solution code:
'''input
2 2
3
2 1 1
'''
h,w = map(int,raw_input().split())
n  =input()
arr = map(int,raw_input().split())
ans = [[-1]*w for i in range(h)]
ind = 0
for i in xrange(h):
	if i&1:
		for j in xrange(w-1,-1,-1):
			if arr[ind] == 0:
				ind += 1
				ans[i][j] = ind+1
				arr[ind] -= 1
			else:
				ans[i][j] = ind+1
				arr[ind] -= 1
	else:
		for j in xrange(w):
			if arr[ind] == 0:
				ind += 1
				ans[i][j] = ind+1
				arr[ind] -= 1
			else:
				ans[i][j] = ind+1
				arr[ind] -= 1
for i in xrange(h):
	for j in xrange(w):
		print ans[i][j],
	print

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
A=[int(i) for i in input().split()]
table=[[-1]*W for i in range(H) ]
u=0
v=0
t=0
for i in range(N):
    s=A[i]
    while s>0:
        if t==0 and (u<W-1):
            table[v][u]=i+1
            u+=1
            s-=1
        elif t==0 and u==W-1:
            table[v][u]=i+1
            #u-=1
            v+=1
            t=(t+1)%2
            s-=1
        elif t==1 and u>0:
            table[v][u]=i+1
            u-=1
            s-=1
        elif t==1 and u==0:
            table[v][u]=i+1
            #u-=1
            v+=1
            t=(t+1)%2
            s-=1 
for i in range(H):
    print(' '.join(map(str,table[i])))

Submission status: Accepted

Solution code:
def read():
    return int(input())


def readlist():
    return list(map(int, input().split()))


def readmap():
    return map(int, input().split())


H, W = readmap()
N = read()
A = readlist()

C = [[0] * W for _ in range(H)]

i = 0
j = 0
for n in range(N):
    a = A[n]
    while a:
        C[i][j] = n + 1
        if (j == 0 and i % 2 == 1) or (j == W - 1 and i % 2 == 0):
            i += 1
        elif i % 2 == 0:
            j += 1
        else:
            j -= 1
        a -= 1

for i in range(H):
    print(" ".join(list(map(str, C[i]))))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
ret=[["0" for i in range(w)]for j in range(h)]
x=0
y=0
for i in range(n):
    for j in range(a[i]):
        ret[y][x]=str(i+1)
        if y&1:
            if x==0:
                y+=1
            else:
                x-=1
        else:
            if x==w-1:
                y+=1
            else:
                x+=1
for i in range(h):
    print(" ".join(ret[i]))

Submission status: Accepted

Solution code:
grid_condition = input().split()
height = int(grid_condition[0])
width = int(grid_condition[1])
color_variety = int(input())
input_condition = input().split()

list_in_list = list()

for i in range(height):
    list_in_list.append(list())

pointer = 0
number = 1

for i in range(1, color_variety + 1):
    times = int(input_condition[i-1])
    for j in range(times):
        list_in_list[pointer].append(i)
        if len(list_in_list[pointer]) == width:
            pointer += 1


for i in range(len(list_in_list)):
    t = ''
    if i%2 == 1:
        list_in_list[i].sort(reverse=True)
    for j in list_in_list[i]:
        t = t + str(j) + " "
    print(t)

Submission status: Accepted

Solution code:
tmp = list(map(int,input().split()))
h , w = tmp[0],tmp[1]
n=int(input())
t=[]
ans =[]
As = list(map(int,input().split()))
for i in range(1,n+1):
  for j in range(As[i-1]):
    t.append(i)
for i in range(h):
  j = t[:w]
  if i %2:
    j.reverse()
  print(*j)
  t=t[w:]

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))

c = [[0 for i in range(w)] for j in range(h)]
fromL = True
x = 0
y = 0

for i in range(1, n+1) :
    for j in range(a[i-1]) :
        c[y][x] = i
        if w == 1 :
            y += 1
        elif fromL :
            if x == w - 1 :
                y += 1
                fromL = False
            else :
                x += 1
        else :
            if x == 0 :
                y += 1
                fromL = True
            else :
                x -= 1

for ci in c :
    print(*ci)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))

cells = [0] * (h * w)
p = 0
for i in range(n):
    for j in range(p, p + a[i]):
        cells[j] = i + 1
    p += a[i]

for i in range(h):
    if i % 2 == 0:
        print(*cells[i * w: i * w + w])
    else:
        print(*cells[i * w: i * w + w][::-1])

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))

ans = [[0 for j in range(W)] for h in range(H)]
flg = True
h = 0
w = 0
for i, a in enumerate(A):
    for _ in range(a):
        ans[h][w] = i + 1
        if flg:
            if w < W - 1:
                w += 1
            else:
                h += 1
                flg = False
        else:
            if w > 0:
                w -= 1
            else:
                h += 1
                flg = True
for l in ans:
    print(" ".join(map(str, l)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

H, W = map(int, input().split())
N = int(input())
As = list(map(int, input().split()))

status = 0 #0:下方向、1:右方向、2:上方向、3:左方向
x = 0
y = 0
switch = 0

ans = [[0 for i in range(H)] for j in range(W)]

for i in range(N):
    tmp = As[i]
    while tmp > 0:
        if status == 0:
            ans[x][y] = i+1
            if y == H-1-switch:
                x += 1
                status = 1
            else:
                y += 1
        elif status == 1:
            #print(x,y)
            ans[x][y] = i+1
            if x == W-1-switch:
                y -= 1
                status = 2
            else:
                x += 1
        elif status == 2:
            ans[x][y] = i+1
            if y == switch:
                x -= 1
                status = 3
                switch += 1
            else:
                y -= 1
        else:
            ans[x][y] = i+1
            if x == switch:
                y += 1
                status = 0
            else:
                x -= 1
        tmp -= 1

for h in range(H):
    for w in range(W):
        print(ans[w][h],end=' ')
        # if w != W-1:
        #     print(' ')
    print()

Submission status: Accepted

Solution code:
H,W=[int(i) for i in input().split()]
N=int(input())
a=[int(i)for i in input().split()]
tile=[[0 for i in range(W)]for j in range(H)]

x=0
y=0
for idA in range(1,len(a)+1):
        num=a[idA-1]
        while num>0:
                tile[y][x]=idA
                if y%2==0:
                        x+=1
                else:
                        x-=1

                if x==-1:
                        y+=1
                        x=0
                elif x==W:
                        y+=1
                        x=W-1
                num-=1
for y in range(H):
        for x in range(W):
                if x!=0:
                        print(" ",end="")
                print(tile[y][x],end="")
        print()

Submission status: Accepted

Solution code:
# Grid Coloring
H,W = list(map(int, input().split()))
N = int(input())
a = list(map(int, input().split()))

from itertools import chain
b = list(chain.from_iterable( [[i+1]*a[i] for i in range(N)] ))
#print(b)
c = [b[i*W:(i+1)*W] if i%2==0 else b[i*W:(i+1)*W][::-1] for i in range(H)]

for i in range(H):
    print(' '.join(map(str, c[i])))

Submission status: Accepted

Solution code:

def draw(i):
    global p,q,hd,wd
    B[p][q] = i
    if (q < W-1 and hd != -1):
        hd = 1
        wd = 0
    elif (hd == 1 and q == W-1) or (hd == -1 and q == 0):
        hd = 0
        wd = 1
    elif (q > 0 and hd != 1):
        hd = -1
        wd = 0
    p += wd
    q += hd

if __name__ == '__main__':
    p = 0
    q = 0
    hd = 1
    wd = 0
    H, W = list(map(int, input().split()))
    N = int(input())
    As = list(map(int, input().split()))
    B = [[0 for i in range(W)] for j in range(H)]

    for i in range(N):
        for j in range(As[i]):
            draw(i+1)

    for i in range(H):
        s = list(map(str, B[i]))
        print(' '.join(s))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import random
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
i = 0
for y in range(h):
    row = []
    for _ in range(w):
        row += [ i + 1 ]
        a[i] -= 1
        if a[i] == 0:
            i += 1
    if y % 2 == 1:
        row = reversed(row)
    print(*row)

Submission status: Accepted

Solution code:
h, w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
c = []

for i in range(n):
    while a[i] > 0:
        a[i] -= 1
        c.append(str(i + 1))

for i in range(h):
    if i % 2 == 0:
        print(" ".join(c[i * w:(i + 1) * w]))
    else:
        print(" ".join(reversed(c[i * w:(i + 1) * w])))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))

HW = [[0 for w in range(W)]for h in range(H)]
C = []

count = 1
for a in A:
    for i in range(a):
        C.append(count)
    count += 1

count = 0
for h in range(H):
    if h % 2 == 0:
        for w in range(W):
            HW[h][w] = C[count]
            count += 1
    else:
        for w in range(W):
            HW[h][W-w-1] = C[count]
            count += 1

for hw in HW:
    for s in hw:
        print(s, end=" ")
    print()

Submission status: Accepted

Solution code:
height, width = [int(s) for s in raw_input().split(" ")]
n = int(raw_input())
colorcounts = [int(s) for s in raw_input().split(" ")]
table = [[-1 for c in range(width)] for r in range(height)]


def direction(row):
    return 1 if even(i)else -1


def new_current_column(next_row, width):
    return 0 if even(next_row)else width - 1


def even(i):
    return i % 2 == 0


def nopainted_count(row, current_column, width):
    return width - current_column if even(row)else current_column + 1


def paint(table, row, cc, count, color):
    if even(row):
        table[row][cc:cc + count] = [color + 1] * count
    else:
        table[row][cc - count + 1:cc + 1] = [color + 1] * count


cur_color = 0
for i in xrange(0, height):
    column = new_current_column(i, width)
    while nopainted_count(i, column, width) > colorcounts[cur_color]:
        paint(table, i, column, colorcounts[cur_color], cur_color)
        column += direction(i) * colorcounts[cur_color]
        cur_color += 1
    paint(table, i, column, nopainted_count(i, column, width), cur_color)
    colorcounts[cur_color] -= nopainted_count(i, column, width)
for i in table:
    print " ".join([str(s) for s in i])

Submission status: Accepted

Solution code:
x = list(map(int, input().split()))
y = list(map(int, input().split()))
z = list(map(int, input().split()))
res=[]


for i in range(1,y[0]+1):
    for j in range(z[i-1]):
        res=res+[i]

sw=0
for j in range(0,x[0]):
    if sw==0:  
        for i in range(j*x[1],(j+1)*x[1]):
            print(res[i],end=' ')
        sw=1
        print('')
        continue
    if sw==1:
        for i in range((j+1)*x[1]-1,j*x[1]-1,-1):
            print(res[i],end=' ')
        sw=0
        print('')
        continue
    

quit()

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
n = int(input())
a = map(int, input().split())

l = []

for i,x in enumerate(a):
	for j in range(x):
		l.append(i+1)

for i in range(h):
	t = l[i*w:(i+1)*w]
	if i % 2 == 1:
		t.reverse()
	print(' '.join(map(str, t)))

Submission status: Accepted

Solution code:
import sys
import numpy as np

h,w = [int(x) for x in sys.stdin.readline().split()]
n = int(input())
a = [int(x) for x in sys.stdin.readline().split()]
s = h*w

l = []

for i,x in enumerate(a):
	for j in range(x):
		l.append(i+1)

l = np.array(l).reshape((h,w))

for i,x in enumerate(l):
	if i % 2 == 0:
		print(' '.join(map(str, x)))
	else:
		print(' '.join(map(str, reversed(x))))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    h,w = LI()
    n = I()
    a = LI()

    r = [0] * (h*w)
    t = 0
    for i in range(1,n+1):
        c = a[i-1]
        for j in range(c):
            r[t+j] = i
        t += c
    rr = []
    for i in range(h):
        t = r[i*w:i*w+w]
        if i % 2 == 1:
            t.reverse()
        rr.append(t)

    return '\n'.join([' '.join(map(str, r)) for r in rr])



print(main())

Submission status: Accepted

Solution code:
import numpy as np

def grid_coloring():
	h,w = map(int, raw_input().split(" "))
	n = int(raw_input())
	a = map(int, raw_input().split(" "))
	_current_color = 1
	ans = np.zeros((h,w), dtype=int)
	for i in range(h):
		if i%2==0:
			for j in reversed(range(w)):
				if a[_current_color-1]!=0:
					ans[i][j]=_current_color
					a[_current_color - 1]-=1
				else:
					_current_color +=1
					ans[i][j] = _current_color
					a[_current_color - 1] -= 1
		else:
			for j in range(w):
				if a[_current_color-1]!=0:
					ans[i][j]=_current_color
					a[_current_color - 1]-=1
				else:
					_current_color +=1
					ans[i][j] = _current_color
					a[_current_color - 1] -= 1
	for i in range(h):
		print " ".join(map(str, ans[i]))


if __name__ == "__main__":
	grid_coloring()

Submission status: Accepted

Solution code:
#D_GridColoring
import numpy as np
def GridColoring(H, W, N, a):
    a = np.array(map(int, a))
    #print(np.argsort(a)[::-1])
    c = np.zeros((H,W))
    direct = True
    x = 0
    y = 0
    for color in np.argsort(a)[::-1]:
        for i in range(a[color]):
            c[y,x] = color+1
            if ((x == 0) and not(direct)) or ((x == (W-1)) and direct):
                y += 1
                direct = not(direct)
            else:
                if direct:
                    x += 1
                else:
                    x -= 1
    return c

def output(X):
    import sys
    for i in range(X.shape[0]):
        for j in range(X.shape[1]):
            sys.stdout.write("%d " %X[i,j])
        sys.stdout.write("\n")



if __name__ == "__main__":
    l = raw_input().split()
    H = int(l[0])
    W = int(l[1])
    N = int(raw_input())
    a = raw_input().split()
    c = GridColoring(H,W,N,a)
    output(c)