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 collections import deque

h,w = map(int, input().split())
n = int(input())
al = list(map(int, input().split()))

q = deque([])
for i,a in enumerate(al):
    c = i+1
    for j in range(a):
        q.append(c)

ans = [ [0]*w for _ in range(h)]
for hi in range(h):
    if hi%2 == 0:
        for wi in range(w):
            c = q.popleft()
            ans[hi][wi] = c
    else:
        for wi in range(w-1,-1,-1):
            c = q.popleft()
            ans[hi][wi] = c

    
for a in ans:
    print(*a)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
alst = list(map(int, input().split()))
cnt = 0
pos = 0
rev = False
for i in range(h):
    ans = [0 for _ in range(w)]
    for j in range(w):
        if rev:
            ans[w - 1 - j] = pos + 1
        else:
            ans[j] = pos + 1
        cnt += 1
        if alst[pos] == cnt:
            cnt = 0
            pos += 1
    print(*ans)
    if rev:
        rev = False
    else:
        rev = True

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
a=list(map(int,input().split()))
cnt = 0
now = 0
c=[[0 for _ in range(W)] for _ in range(H)]
for h in range(H):
  iter_w = range(W)
  if h%2:
    iter_w = reversed(iter_w)
  for w in iter_w:
    c[h][w] = now + 1
    cnt += 1
    if cnt == a[now]:
      now += 1
      cnt = 0

for i in range(H):
  print(*c[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()))
ans = [["" for _ in range(w)] for _ in range(h)]
ptr = 0
for y in range(h):
    for x in range(w):
        if a[ptr] == 0:
            ptr += 1
        ans[y][x] = str(ptr+1)

        a[ptr] -= 1

for y in range(h):
    print(" ".join(ans[y] if y % 2 == 0 else reversed(ans[y])))

Submission status: Accepted

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

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

C=[0]
for a in A:
    C.append(C[-1]+a)
K=0
X,Y=0,0
D=1
while K<H*W:
    K+=1

    B[Y][X]=D

    if Y%2:
        if X==0:
            Y+=1
        else:
            X-=1
    else:
        if X==W-1:
            Y+=1
        else:
            X+=1

    if K==C[D]:
        D+=1

L=[" ".join(map(str,P)) for P in B]
print("\n".join(L))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
h, w, n, *a = map(int,open(0).read().split())
ans = []
for i, j in enumerate(a):
    ans += [i + 1] * j
for i in range(h):
    print(*ans[i*w:(i+1)*w][::-1+2*(i%2)])

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    H, W = [int(item) for item in input().split()]
    N = int(input())
    As = [int(item) for item in input().split()]

    line = []
    for i, val in enumerate(As):
        line += val * [i+1]

    grid = [[] for _ in range(H)]

    for i in range(H):
        if i % 2 == 0:
            grid[i] = line[i*W: W*(i+1)]
        else:
            grid[i] = line[i*W: W*(i+1)][::-1]

    for temp in grid:
        print(*temp)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

res = []

for i, a in enumerate(A):
    res += [str(i+1)]*a

for i in range(H):
    if i%2 == 0:
        print(" ".join(res[i*W:(i+1)*W]))
    else:
        print(" ".join(reversed(res[i*W:(i+1)*W])))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
a=[int(i) for i in input().split()]

c=[0 for _ in range(h*w)]

x=0
for i in range(1,n+1):
    c[x:x+a[i-1]]=[i]*(a[i-1])
    x+=a[i-1]

for i in range(0,h):
    if i%2==0:
        for j in range(w*i,w*(i+1)):
            print(c[j],end=" ")
    else:
        for j in range(w*(i+1)-1,w*i-1,-1):
            print(c[j],end=" ")
    print()

Submission status: Accepted

Solution code:
#create date: 2020-08-07 07:56

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    h, w = na()
    n = ni()
    a = na()
    g = [0] * (h*w)
    gi = 0; ai = 0
    while gi != h * w:
        anow = a[ai]
        for i in range(gi, gi+anow):
            g[i] = (ai+1)
        gi = gi + anow
        ai += 1
    g_rev = g[::-1]
    for i in range(h):
        l = i*w
        r = i*w + w
        if i % 2 == 0:
            print(*g[l:r])
        else:
            l_rev = h*w - r
            r_rev = h*w - l
            print(*g_rev[l_rev:r_rev])
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
N = int(input())
A = list(map(int,input().split()))
A.insert(0,0)
B = [[0 for _ in range(W)] for _ in range(H)]
col = 1
cnt = 0
y = 0
x = 0
d = "R"
while y<H:
    if cnt<A[col]:
        B[y][x] = col
        cnt += 1
    else:
        col += 1
        cnt = 0
        B[y][x] = col
        cnt += 1
    if x<W-1 and d=="R":
        x += 1
    elif x==W-1 and d=="R":
        y += 1
        d = "L"
    elif d=="L" and x>0:
        x -= 1
    elif d=="L" and x==0:
        y += 1
        d = "R"
for i in range(H):
    print(*B[i])

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()]

grid = [[None] * w for _ in range(h)]

cnt = 0
i, j = 0, 0
direction = 0  # 0:right, 1:left
ptr = 0
res = A[ptr]
while cnt < h * w:
    cnt += 1
    # fill grid
    if res > 0:
        grid[i][j] = ptr + 1
        res -= 1
    elif res == 0:
        ptr += 1
        grid[i][j] = ptr + 1
        res = A[ptr] - 1
    # next cell
    if direction == 0 and j < w - 1:
        j += 1
    elif direction == 0 and j == w - 1:
        i += 1
        direction = 1
    elif direction == 1 and j > 0:
        j -= 1
    elif direction == 1 and j == 0:
        i += 1
        direction = 0

for line in grid:
    print(" ".join((str(i) for i in line)))

Submission status: Accepted

Solution code:
from collections import deque

h,w = map(int, input().split())
n = int(input())
al = list(map(int, input().split()))
ans = [ [0]*w for _ in range(h) ]

q = deque([])
for i,a in enumerate(al):
    c = i+1
    for _ in range(a):
        q.append(c)


for hi in range(h):
    for wi in range(w):
        if hi%2 == 0: ind = wi
        else: ind = ind = w-1-wi
        ans[hi][ind] = q.popleft()

for row in ans:
    print(*row)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
ans = [[-1]*W for _ in range(H)]
col = []

for i in range(N):
    col += [i+1]*a[i]

idx = 0

for i in range(H):
    if i%2==0:
        for j in range(W):
            ans[i][j] = col[idx]
            idx += 1
    else:
        for j in range(W-1, -1, -1):
            ans[i][j] = col[idx]
            idx += 1

for ans_i in ans:
    print(*ans_i)

Submission status: Accepted

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

ans = [[-1]*W for _ in range(H)]
now = rem = 0
for i in range(H):
    r = range(W)
    if i%2: r = reversed(r)
    for j in r:
        if rem:
            ans[i][j] = now
            rem -= 1
        else:
            now += 1
            ans[i][j] = now
            rem = A[now-1] - 1
for row in ans:
    print(*row)

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)]
b = [0]*(H*W)
now = 0
for i in range(N):
  for j in range(A[i]):
    b[now+j] = i+1
  now += A[i]
for h in range(H):
  for w in range(W):
    if h%2==0:
      ans[h][w] = b[W*h+w]
    else:
      ans[h][w] = b[W*(h+1)-w-1]
for a in ans:
  print(*a)

Submission status: Accepted

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

col=[]
cnt=1
for i in a:
    col.extend([cnt]*i)
    cnt+=1

ans=[[0]*W for _ in range(H)]
idx=0
for h in range(H):
    if h%2==0:
        for w in range(W):
            ans[h][w]=col[idx]
            idx+=1
    elif h%2==1:
        for w in range(W-1,-1,-1):
            ans[h][w]=col[idx]
            idx+=1

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[15]:


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


# In[17]:


c = 0
tmp = 1
for h in range(H):
    line = []
    for w in range(W):
        line.append(c+1)
        tmp += 1
        if tmp > a[c]:
            c += 1
            tmp = 1
    if h%2 == 0:
        print(" ".join(map(str,line)))
    else:
        print(" ".join(map(str,list(reversed(line)))))


# In[ ]:

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
ans = []
for i in range(H):
    ans.append([0] * W)
# print(ans)
now = 0
cnt = 0
for i in range(H):
    if i % 2 == 0:
        for j in range(W):
            ans[i][j] = now + 1
            cnt += 1
            if cnt == A[now]:
                now += 1
                cnt = 0
    else:
        for j in range(W - 1, -1, -1):
            ans[i][j] = now + 1
            cnt += 1
            if cnt == A[now]:
                now += 1
                cnt = 0
for row in ans:
    print(*row, sep=' ')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
*A,=map(int,input().split())
A=[0]+A
ans=[[0]*W for _ in range(H)]
countH=0
countW=0
i=0
for a in A:
    for _ in range(a):
        ans[countH][countW]=str(i)
        if countW==W-1 and countH%2==0:
            countH+=1
        elif countW==0 and countH%2==1:
            countH+=1
        elif countH%2==0:
            countW+=1
        elif countH%2==1:
            countW-=1
    i+=1

for s in ans:
    print(*s)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def run():
    H, W = map(int, sysread().split())
    N = int(sysread())
    A = list(map(int, sysread().split()))
    MAP = np.zeros(H * W, dtype=np.int32)
    idx = 0
    for i, a in enumerate(A, 1):
        MAP[idx:idx+a] = i
        idx += a

    MAP = MAP.reshape(H,W)
    for i in range(1, len(MAP), 2):
        MAP[i, :] = MAP[i, ::-1]

    for row in MAP:
        for val in row:
            print(val, end=' ')
        print('')


if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
N = int(input())
a = list(map(int,input().split()))
l = [[0]*W for _ in range(H)]
bol = 1
line,cnt = 0,0

for i in range(1,N+1):
    for j in range(a[i-1]):
        if bol == 1:
            l[line][cnt] = i
        else:
            l[line][-cnt-1] = i
        cnt += 1
        if cnt == W:
            cnt = 0
            line += 1
            bol *= -1

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    
    H,W = map(int,input().split())
    N = int(input())
    A = list(map(int,input().split()))

    map_list = [[-1]*W for i in range(H)]

    pre = A.pop(0)
    idx = 1
    for i in range(H):
        if i%2==0:
            start = 0
            end = W
            step = 1
        else:
            start = W-1
            end = -1
            step = -1

        for j in range(start,end,step):
            if pre==0:
                pre = A.pop(0)
                idx += 1
        
            pre-=1
            map_list[i][j]= idx

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

        

if __name__=='__main__':
    main()

Submission status: Accepted

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

l = []
for i, ai in enumerate(a, 1):
    l += [i] * ai

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

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):
    k = a[i]
    while k > 0:
        k -= 1
        L.append(i + 1)


c = 0
count = 0
for i in range(H):
    if (count % 2 == 0):
        A = []
        while c < W * (i + 1):
            A.append(str(L[c]))
            c += 1
        print(' '.join(A))
    else:
        B = []
        while c < W * (i + 1):
            B.append(str(L[c]))
            c += 1
        print(' '.join(B[:: -1]))

    count += 1

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):
    num = a[i]
    while num > 0:
        l.append(i+1)
        num -= 1
s = []
cnt = 0
for i in range(0, h*w, w):
    if cnt%2 == 0:
        print(*l[i:i+w])
    else:
        print(*l[i+w-1:i-1:-1])
    cnt += 1

Submission status: Accepted

Solution code:
import itertools

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

one_line = []
for i in range(N):
    s = str(i+1)
    one_line.append([s]*A[i])
li = list(itertools.chain.from_iterable(one_line))

for i in range(0, H):
    tmp = li[i*W:i*W+W]
    if i % 2 != 0:
        tmp = tmp[::-1]
    print(' '.join(list(tmp)))

Submission status: Accepted

Solution code:
from collections import deque

H, W = map(int, input().split())
N = int(input())
A = []
B = list(map(int,input().split()))
for i in range(N):
    for j in range(B[i]):
      A.append(i)
flg = True
c = []
for k in range(1,H*W+1):
  c.append(A[k-1]+1)
  if k % W == 0:
    if flg:
      for s in c:
        print(s, end = ' ')
    else:
      for s in reversed(c):
        print(s, end = ' ')
    c = []
    print()
    flg = not flg

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

H,W=list(map(int,input().split()))
N=int(input())
a=list(map(int,input().split()))
ans = []
for i,ai in enumerate(a):
    ans +=[i+1]*ai
ans = np.array(ans).reshape(H,W).tolist()
for i in range(len(ans)):
    if i%2 == 0:
        print(*ans[i])
    else:
        print(*(ans[i][::-1]))

Submission status: Accepted

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

ans = []
for i,ai in enumerate(a,1):
    ans += [i]*ai

for i in range(h):
    tmp = ans[i*w:(i+1)*w]
    if(i%2==0):
        print(' '.join(map(str,tmp)))
    else:
        print(' '.join(map(str,tmp[::-1])))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
l = list(map(int, input().split()))
temp = []
for i in range(n):
    for j in range(l[i]):
        temp.append(i+1)

ans = [[0 for x in range(w)] for y in range(h)]

tt = 0
for i in range(h):
    if i % 2 == 0:
        for j in range(w):
            ans[i][j] = temp[tt]
            tt+=1
    else:
        for j in range(w-1, -1, -1):
            ans[i][j] = temp[tt]
            tt+=1

for i in range(h):
    for j in range(w):
        print(ans[i][j], end=' ')
    print()

Submission status: Accepted

Solution code:
import numpy as np
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
ans = []

for i in range(N):
  for j in range(a[i]):
    ans.append(i+1)

n = np.array(ans).reshape(H, W)
for i in range(H):
  if i % 2 == 0:
    p = n[i]
    print(*p)
  else:
    p = n[i][::-1]
    print(*p)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

H, W = mapint()
N = int(input())
As = list(mapint())
query = [0]*W*H

idx = 0
for i in range(N):
    a = As[i]
    for j in range(a):
        query[idx] = i+1
        idx += 1

idx = 0
for h in range(H):
    if h%2==0:
        print(*query[idx:idx+W])
        idx += W
    else:
        print(*query[idx:idx+W][::-1])
        idx += W

Submission status: Accepted

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

C = []
for i,a in enumerate(A,1):
    C += [i]*a

for i in range(H):
    if i%2==0:
        print(*C[i*W:i*W+W])
    else:
        print(*C[i*W:i*W+W][::-1])

Submission status: Accepted

Solution code:
import numpy as np
import collections

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

Graph = np.array([[0]*W]*H)

Currentx = 0
Currenty = 0
colour = 0

while A:
	D = A.popleft()
	colour+=1
	for i in range (0, D):
		if Currentx == W-1 and Currenty%2 == 0:
			Graph[Currenty][Currentx] = colour
			Currenty+= 1
		elif Currentx == 0 and Currenty%2 == 1:
			Graph[Currenty][Currentx] = colour
			Currenty+= 1
		elif Currenty%2 == 0:
			Graph[Currenty][Currentx] = colour
			Currentx+= 1			
		else:
			Graph[Currenty][Currentx] = colour
			Currentx-= 1				

for i in range (0, H):
	print(*Graph[i], sep = " ")

Submission status: Accepted

Solution code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    g = []
    for i, x in enumerate(a):
        g += [i+1]*x
    import numpy as np
    g = np.array(g, dtype=int).reshape((h,w))
    for i in range(h):
        if i%2 == 1:
            g[i,:] = g[i, ::-1]
        print(' '.join(map(str, g[i])))

    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

cnt = 0
indexA = 0
for i in range(H*W):
    x = i // W 
    y = i % W
    if cnt == 0:
        cnt = A[indexA] - 1
        indexA += 1
    else:
        cnt -= 1
    if x % 2 == 0:
        ans[x][y] = indexA
    else:
        ans[x][-y-1] = indexA

for c in ans:
    print(*c)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil, atan, pi
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left
MOD = int(1e9)+7
INF = float('inf')


def solve():
    # n, m = [int(x) for x in input().split()]
    n, m = [int(x) for x in input().split()]
    t = int(input())
    a = [int(x) for x in input().split()]
    k = 0
    for i in range(n):
        r = []
        while len(r) != m:
            if not a[k]:
                k += 1
            x = min(m - len(r), a[k])
            a[k] -= x
            r += [k+1] * x
        if i % 2:
            r.reverse()
        print(*r)  


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
N = INT()
a = LIST()[::-1]

ans = [[0]*W for _ in range(H)]
tmp = a.pop()
color = 1
cnt = 0
for i in range(H):
    for j in range(W):
        if i%2:
            j = W-j-1
        ans[i][j] = color
        cnt += 1
        if cnt == tmp and a:
            cnt = 0
            tmp = a.pop()
            color += 1

for i in ans:
    print(" ".join([str(x) for x in i]))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    H, W = MI()
    N = II()
    A = LI()

    q = deque([])
    for i, a in enumerate(A):
        for _ in range(a):
            q.append(i+1)
    # print(q)

    even = False
    for h in range(H):
        tmp = []
        for w in range(W):
            tmp.append(q.popleft())
        if even:
            print(*tmp[::-1])
        else:
            print(*tmp)
        even = not even


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
n=int(input())
l=list(map(int,input().split()))
lst=[]
for i in range(n):
    for j in range(l[i]):
        lst.append(i+1)
ans=[[None]*W for _ in range(H)]
nuri=[-1,0]
switch=0
for num in lst:
    if switch==0:
        if nuri[0]+1>H-1:
            nuri[1]+=1
            ans[nuri[0]][nuri[1]]=num
            switch=1
        else:
            nuri[0]+=1
            ans[nuri[0]][nuri[1]]=num
    else:
        if nuri[0]-1<0:
            nuri[1]+=1
            ans[nuri[0]][nuri[1]]=num
            switch=0
        else:
            nuri[0]-=1
            ans[nuri[0]][nuri[1]]=num
for i in ans:
    print(" ".join(map(str,i)))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

h,w,n,*a = map(int,read().split())

res = []
for i in range(n):
    res += [i+1]*a[i]

ans = [[0]*w for _ in range(h)]
for x,i in zip(range(h*w),res):
    q,r = x//w,x%w
    if q%2:
        ans[q][-1-r] = i
    else:
        ans[q][r] = i
        
for i in ans:
    print(*i)

Submission status: Accepted

Solution code:
def main():
    h, w = list(map(int, input().split()))
    n = int(input())
    A = list(map(int, input().split()))
    ans = [str(i + 1) for i, a in enumerate(A) for _ in range(a)]
    for i in range(h):
        if i % 2 == 0:
            print(' '.join(ans[i * w:i * w + w]))
        else:
            print(' '.join(ans[i * w + w - 1:i * w - 1:-1]))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt = 0
c = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
    if(i%2 == 0):
        for j in range(W):
            if(a[cnt] > 1):
                c[i][j] = cnt+1
                a[cnt] -= 1
            elif(a[cnt] == 1):
                c[i][j] = cnt+1
                a[cnt] -= 1
                cnt += 1
    else:
        for j in range(W-1, -1, -1):
            if(a[cnt] > 1):
                c[i][j] = cnt+1
                a[cnt] -= 1
            elif(a[cnt] == 1):
                c[i][j] = cnt+1
                a[cnt] -= 1
                cnt += 1
                
for i in range(H):
    print(*c[i])

Submission status: Accepted

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

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

for i in range(h):
    row = ans[i * w: (i + 1) * w]
    if i % 2 == 1:
        row = row[::-1]
    for x in row:
        print(x, end = " ")
    print()

Submission status: Accepted

Solution code:
from collections import deque

# 行列の高さH、幅W
h, w = map(int, input().split())
# 色の数N
n = int(input())
# それぞれの色を塗る枚数のリストa
a = list(map(int, input().split()))

# bに答えとなる行列を構築していく、最初は0埋めしておく
ans = [[0] * w for i in range(h)]


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

# i = 0,1,2,...,h-1
# j = 0,1,2,...,w-1
# 偶数行目は左から詰める、奇数行目は右から詰める
for i in range(h):
    for j in range(w):
        if i % 2 == 0:
            ans[i][j] = colors.popleft()
        else:
            ans[i][w - 1 - j] = colors.popleft()

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

Submission status: Accepted

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

h, w = MAP()
n = INT()
a = LIST()
b = [[0]*w for i in range(h)]
c = deque()

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

for i in range(h):
    for j in range(w):
        if i % 2 == 0:
            b[i][j] = c.popleft()
        else:
            b[i][w-1-j] = c.popleft()

for i in range(h):
    print(*b[i][:])

Submission status: Accepted

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

import numpy as np
ans = np.zeros((h, w))

r = 0
c = 0
is_down = True
for i, a in enumerate(A):
    x = i + 1
    for _ in range(a):
        ans[r, c] = x
        if is_down:
            if r < h-1:
                r += 1
            else:
                c += 1
                is_down = False
        else:
            if r > 0:
                r -= 1
            else:
                c += 1
                is_down = True

for x in ans:
    print(' '.join(x.astype(int).astype(str)))

Submission status: Accepted

Solution code:
import numpy as np

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

f = np.zeros(h*w, dtype=int)

j = 0
for i in range(n):
    m = a[i]
    k = i+1
    f[j:j+m] = k
    j += m
f = f.reshape(h, w).tolist()
for i in range(1, h, 2):
    f[i].reverse()
for i in range(len(f)):
    b = [str(i) for i in f[i]]
    print(' '.join(b))

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


inf = 10 ** 18
mod = 10 ** 9 + 7

h,w = lr()
n = ir()
a = lr()
ans = [[-1 for j in range(w)] for i in range(h)]
now = 1
nowa = 0
i = 0
j = 0
for i in range(h):
    for j in range(w):
        if i%2==0:
            ans[i][j] = now
        else:
            ans[i][w-1-j] = now
        a[nowa]-=1
        if a[nowa] == 0:
            now+=1
            nowa+=1
for i in range(h):
    print(*ans[i], sep = ' ')