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:
# import bisect
# from collections import Counter, defaultdict, deque, namedtuple
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

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

    grid = [[0] * w for _ in range(h)]
    x = 0
    y = 0
    direction = 1

    for i in range(n):
        color = i + 1
        rem = a[i]
        while rem > 0:
            grid[y][x] = color
            rem -= 1
            x += direction
            if x == w:
                x -= 1
                direction *= -1
                y += 1
            if x == -1:
                x += 1
                direction *= -1
                y += 1

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for x in range(n):
  for iter in range(a[x]):
    ans.append(str(x+1))

for y in range(h):
  inp = ans[w * y:w * (y+1)]
  if y % 2 == 0:
    inp.reverse()
  print(' '.join(inp))

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(0,W)] for i in range(0,H)]
grid=(0,0)
for i in range(0,N):
    color=i+1
    for j in range(0,a[i]):
        h,w=grid
        c[h][w]=color
        if W-1>w and h%2==0:
            grid=(h,w+1)
        if w>0 and h%2==1:
            grid=(h,w-1)
        if h%2==0 and w==W-1:
            grid=(h+1,W-1)
        if h%2==1 and w==0:
            grid=(h+1,0)

for i in range(0,H):
    print(*c[i])

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
cam = []
for i in range(n):
    cam.extend([str(i+1)] * a[i])
for i in range(h):
    if i % 2 == 0:
        print(*cam[i*w:(i+1)*w])
    else: print(*cam[(i+1)*w-1:i*w-1:-1])

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()]
seen = [[False] * W for _ in range(H)]
ans = [0 for _ in range(H*W)]
from numpy import cumsum
a_sum = list(cumsum(a))
colour = 0
for i in range(H*W):
    if i >= a_sum[colour]:
        colour +=1
    ans[i] = colour +1
for h in range(H):
    if h%2 == 0:
        print(" ".join(map(str, ans[h*W:(h+1)*W])))
    else:
        print(" ".join(map(str, reversed(ans[h*W:(h+1)*W]))))

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
N = int(input())
A = [int(x) for x in input().split()]

L = []
for i,a in enumerate(A):
	for j in range(a):
		L.append(i+1)

T = [[0]*W for i in range(H)]

idx = 0
for i in range(H):
	k = 1
	if i%2:
		k = -1
	for j in range(W)[::k]:
		T[i][j] = L[idx]
		idx += 1
for t in T:
	print(*t)

Submission status: Accepted

Solution code:
import math
import numpy as np
H, W = list(map(int, input().split()))
N = input()
colors = list(map(int, input().split()))

cumsum_colors = np.cumsum(colors)

color_ind = 0
arr = [0 for _ in range(H*W)]
for i in range(H*W):
    if i >= cumsum_colors[color_ind]:
        color_ind += 1
    arr[i] = color_ind + 1

for h in range(H):
    if h%2 == 0:
        print(" ".join(map(str, arr[h*W:(h+1)*W])))
    else:
        print(" ".join(map(str, reversed(arr[h*W:(h+1)*W]))))

Submission status: Accepted

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

g = []
for i, v in enumerate(a):
    g.extend([i+1]*v)

k = 0
for i in range(h):
    l = []
    for j in range(w):
        l.append(g[k])
        k += 1
    if i%2:
        print(*l[::-1])
    else:
        print(*l)

Submission status: Accepted

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

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

alternate = True
index = 0
for i in range(H):
    if alternate:
        for j in range(W):
            if A[index] != 0:
                grid[i][j] = index
                A[index] -= 1
            else:
                index += 1
                grid[i][j] = index
                A[index] -= 1
    else:
        for j in reversed(range(W)):
            if A[index] != 0:
                grid[i][j] = index
                A[index] -= 1
            else:
                index += 1
                grid[i][j] = index
                A[index] -= 1
    alternate = not alternate

for gr in grid:
    for g in gr:
        print('{} '.format(g+1),end='')
    print()

Submission status: Accepted

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

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

now_x=0
now_y=0
move_r=True
for num in range(len(a)):
    for i in range(a[num]):
        arr[now_y][now_x] = num+1
        if move_r:
            if now_x == w-1:
                now_y+=1
                move_r=False
            else:
                now_x+=1
        else:
            if now_x == 0:
                now_y+=1
                move_r=True
            else:
                now_x-=1

for tmp in arr:
    print(' '.join(map(str, tmp)))

Submission status: Accepted

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

CC = [[-1] * w for _ in range(h)]
col = 1
count = 0
dir = 0
x, y = 0, 0
cands = ((1, 0), (0, 1), (-1, 0), (0, -1))
for _ in range(h * w):
    CC[y][x] = col
    count += 1
    if count == A[col - 1]:
        count = 0
        col += 1
    dx, dy = cands[dir]
    nx, ny = x + dx, y + dy
    if nx < 0 or nx >= w or ny < 0 or ny >= h or CC[ny][nx] != -1:
        dir = (dir + 1) % 4
        dx, dy = cands[dir]
        nx, ny = x + dx, y + dy
    x, y = nx, ny

for C in CC:
    print(*C)

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)]
k=0 #color
for i in range(h):
    if i%2==0:
        j=0
        while j<w:
            if a[k]>0:
                c[i][j]=k+1
                a[k]-=1
                j+=1
            else:
                k+=1
    else:
        j=w-1
        while j>=0:
            if a[k]>0:
                c[i][j]=k+1
                a[k]-=1
                j-=1
            else:
                k+=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()))

C = [0 for i in range(w*h)]
idx = 0

for i in range(n):
    a = A[i]
    for j in range(a):
        C[idx] = i+1
        idx += 1

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

Submission status: Accepted

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

c = [[0] * w for _ in range(h)]
li = [[0,0]]
hw = [0,0]
dir = 'r'

while len(li) < h*w:
    if dir == 'r':
        if hw[1] < h-1:
            if [hw[0], hw[1]+1] not in li:
                hw[1] += 1
                li.append([hw[0], hw[1]])
            else:
                dir = 'd'
        else:
            dir = 'd'
    elif dir == 'd':
        if hw[0] < w-1:
            if [hw[0]+1, hw[1]] not in li:
                hw[0] += 1
                li.append([hw[0], hw[1]])
            else:
                dir = 'l'
        else:
            dir = 'l'
    elif dir == 'l':
        if hw[1] > 0:
            if [hw[0], hw[1]-1] not in li:
                hw[1] -= 1
                li.append([hw[0], hw[1]])
            else:
                dir = 'u'
        else:
            dir = 'u'
    else:
        if hw[0] > 0:
            if [hw[0]-1, hw[1]] not in li:
                hw[0] -= 1
                li.append([hw[0], hw[1]])
            else:
                dir = 'r'
        else:
            dir = 'r'

count = 0

for i in range(n):
    for _ in range(a[i]):
        c[li[count][1]][li[count][0]] = i+1
        count += 1

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

Submission status: Accepted

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

a = list(map(int,input().split()))

d = [[0]*w for i in range(h)]

I = 0
J = 0
for i in range(n):
    while a[i] > 0:
        d[I][J] = i+1
        a[i] -= 1
        if I%2 == 0 and J == w-1:
            I += 1
        elif I %2 == 1 and J == 0:
            I += 1
        else:
            if I%2 == 0:
                J += 1
            else:
                J -= 1

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

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    h,w = inpm()
    n = inp()
    a = inpl()
    ans = [[0 for _ in range(w)] for _ in range(h)]
    x,y,key = 0,0,1
    for i in range(n):
        for j in range(a[i]):
            ans[x][y] = str(i+1)
            if key:
                x += 1
                if x == h:
                    key = 0
                    x -= 1
                    y += 1
            else:
                x -= 1
                if x == -1:
                    key = 1
                    x += 1
                    y += 1
    for i in range(h):
        print(' '.join(ans[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()))

a_list=[]
for i,j in enumerate(a):
 a_list+=[i+1]*j

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

 print(*ans)

Submission status: Accepted

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

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

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

 print(*ans)

Submission status: Accepted

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

M=[[0 for _ in range(W)] for __ in range(H)]
i=1
for y in range(H):
    if y&1:
        for x in range(W):
            if A[i]==0: i+=1
            M[y][x]=i
            A[i]-=1
    else:
        for x in range(W-1,-1,-1):
            if A[i]==0: i+=1
            M[y][x]=i
            A[i]-=1

for line in M: print(' '.join(map(str,line)))

Submission status: Accepted

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

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

for i in range(1,n):
    lst1[i] += lst1[i-1]

res = 1

for i in range(h):
    if i%2 == 0:
        for j in range(w):
            lst2[i][j] = bisect.bisect_left(lst1,res) + 1
            res += 1

    else:
        for j in range(w-1,-1,-1):
            lst2[i][j] = bisect.bisect_left(lst1,res) + 1
            res += 1

for i in lst2:
    print(*i)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N, = map(int, input().split())
As = list(map(int, input().split()))
c = 1
r = [[0]*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if i%2:
            r[i][j] = c
        else:
            r[i][W-j-1] = c
        As[c-1] -= 1
        if As[c-1] == 0:
            c += 1

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

Submission status: Accepted

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

ret = [[0 for i in range(W+2)] for j in range(H+2)]
for i in range(H+2):
  for j in range(W+2):
    if i == 0:
      ret[i][j] = 1
      continue
    if i == H+1:
      ret[i][j] = 1
      continue
    if j == 0:
      ret[i][j] = 1
      continue
    if j == W+1:
      ret[i][j] = 1
      continue

ret[1][1] = 1

x = 1
y = 1
dx = 1
dy = 0

for i, a in enumerate(A, 1):
  if i == 1:
    a -= 1
  while a > 0:
    _x = x + dx
    _y = y + dy
    if ret[_y][_x]:
      if dx == 1:
        dx = 0
        dy = 1
        continue
      if dx == -1:
        dx = 0
        dy = -1
        continue
      if dy == 1:
        dx = -1
        dy = 0
        continue
      if dy == -1:
        dx = 1
        dy = 0
        continue
    else:
      ret[_y][_x] = i
      x = _x
      y = _y
      a -= 1
      
for i in range(H):
  print(*ret[i+1][1:W+1])

Submission status: Accepted

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

b = []
for i, j in enumerate(a):
    b += [i + 1] * j

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

Submission status: Accepted

Solution code:
H,W = list(map(int,input().split()))
N = int(input())
a = list(map(int,input().split()))
ansline = []
for i in range(N):
    ansline += [i+1 for j in range(a[i])]
def split_list(l, n):
    for idx in range(0, len(l), n):
        yield l[idx:idx + n]
ans = list(split_list(ansline,W))
for i in range(1,H,2):
    ans[i].reverse()
for i in ans:
    print(*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()))
d = [i+1 for i,x in enumerate(a) for _ in range(x)]
d = np.array(d).reshape((h,w))
for i,x in enumerate(d):
  if i%2==0:
    print(*x,sep=" ")
  else:
    print(*x[::-1],sep=" ")

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

num = 0
for x in range(n):
    for y in range(a[x]):
        i = num // w
        if i%2:
            j = w-1 - (num % w)
        else:
            j = num % w
        ans[i][j] = x+1
        num += 1
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()))
ans = []
for i in range(N):
  ans.extend([i+1 for _ in range(a[i])])
ans = [ans[i:i+W] for i in range(0, H*W, W)]
for i in range(1, H, 2):
  ans[i] = ans[i][::-1]
for r in ans:
  print(' '.join(map(str,r)))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
ans=[]
c=0
cnt=0
flg=True
for i in range(h):
    t=[]
    for j in range(w):
        if a[c]<=cnt:
            c+=1
            cnt=0
        if flg:
            t.append(c+1)
        else:
            t.insert(0,c+1)
        cnt+=1
    flg=not flg
    ans.append(t)
for i in ans:
    print(' '.join(list(map(str,i))))

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

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

H,W = two_int()
N=one_int()
A=many_int()

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

output=[]
for i in range(1,H+1):
    output.append(lists[W*(i-1):W*i])

for i,o in enumerate(output):
    temp = list(map(str,o))
    if i%2==0:
        print(" ".join(temp))
    else:
        print(" ".join(temp[::-1]))

Submission status: Accepted

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

count = 1
tmp_w = 0
tmp_h = 0
for i in a:
    for j in range(i):
        #print(hw)
        hw[tmp_h][tmp_w] = str(count)
        if tmp_w != w - 1:
            tmp_w += 1
        else:
            tmp_h += 1
            tmp_w = 0
    count += 1
y = 1
for i in hw:
    if y % 2 == 0:
        i.reverse()
    y += 1
    print(' '.join(i))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
b=list(map(int,input().split()))
a=[]
for i in range(n):
  for j in range(b[i]):
    a.append(i+1)
g=[[0 for i in range(w)] for j in range(h)]
for i in range(h):
  if i%2==0:
    for j in range(w):
      g[i][j]=a[0]
      a.remove(a[0])
  else:  
    for j in range(w):
      g[i][w-j-1]=a[0]
      a.remove(a[0])
for i in range(h):
  print(*g[i])

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
b=list(map(int,input().split()))
a=[]
for i in range(n):
  for j in range(b[i]):
    a.append(i+1)
g=[[0 for i in range(w)] for j in range(h)]
for i in range(h):
  if i%2==0:
    for j in range(w):
      g[i][j]=a[0]
      a.remove(a[0])
  else:  
    for j in range(w):
      g[i][w-j-1]=a[0]
      a.remove(a[0])
for i in range(h):
  print(*g[i])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def main():
    h, w = list(map(int, input().strip().split()))
    n = int(input().strip())
    a = list(map(int, input().strip().split()))
    c = [[0] * w for _ in range(h)]
    k = 1
    l = 0
    for i in range(h):
        for j in range(w):
            if i % 2 == 0:
                c[i][j] = k
            else:
                c[i][w-1-j] = k
            a[l] -= 1
            if a[l] == 0:
                k += 1
                l += 1
    for i in range(h):
        print(' '.join(map(str, c[i])))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for i in range(n):
  for j in range(aa[i]):
    nums.append(i+1)
  
for i in range(len(nums)):  
  ans[i//w][i%w] = nums[i]  
  
for hi in range(h):
  if hi % 2 == 1:    
    print(' '.join(map(str,list(reversed(ans[hi])))))
  else:
    print(' '.join(map(str,ans[hi])))

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
A=[int(i) for i in input().split()]
M=[[-1 for _ in range(W)] for _ in range(H)]
i,j=0,0
direc=0
circ=0

class solve:
    def __init__(self, L):
        self.L=L
        self.i=0
        self.len=len(L)
    def get(self):
        if self.L[self.i] != 0 :
            self.L[self.i] -= 1
            return self.i+1
        else:
            self.i += 1
            self.L[self.i] -= 1
            return self.i+1
ans=solve(A)    
#螺旋状のiteration
for _ in range(H*W):
    M[i][j]=ans.get()
    if direc == 0:
        if i == H-1-circ:
            direc = 1
            j += 1
        else:
            i += 1
    elif direc == 1:
        if j == W-1-circ:
            direc = 2
            i -= 1
        else:
            j += 1
    elif direc == 2:
        if i == circ:
            direc = 3
            j -= 1
        else:
            i -= 1
    elif direc == 3:
        if j == 1+circ:
            direc = 0
            circ += 1
            i += 1
        else:
            j -= 1
for m in M:
    print(*m)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
B = [[] for i in range(H)]
k = 0

for i, a in enumerate(A):
    for j in range(a):
        B[k].append(i + 1)
        if len(B[k]) == W:
            k += 1

for i, row in enumerate(B):
    if i % 2 == 0:
        print(*row)
    else:
        print(*row[::-1])

Submission status: Accepted

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

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

h = 0
w = 0
for idx, a in enumerate(A):
    num = idx + 1
    for _ in range(a):
        H_W[h][w] = num
        if w % 2 == 0:
            if 0 <= h + 1 < H:
                h += 1
            else:
                w += 1
        else:
            if 0 <= h - 1 < H:
                h -= 1
            else:
                w += 1

for a in H_W:
    print(" ".join(map(str, a)))

Submission status: Accepted

Solution code:
H,W,N,*A = map(int,open(0).read().split())
h,w = 0,0
D = [[0]*W for i in range(H)]
for i in range(N):
    c=0
    while c<A[i]:
        D[h][w] = i+1
        if h%2==0:w+=1
        else:w-=1
        if w<0 or w>=W:
            h+=1
            w = 0 if h%2==0 else W-1
        c+=1
for d in D:print(*d)

Submission status: Accepted

Solution code:
H, W, N, *a = map(int, open(0).read().split())
ans = []
for i in range(N):
  ans.extend([i+1 for _ in range(a[i])])
ans = [ans[i:i+W] for i in range(0, H*W, W)]
for i in range(1, H, 2):
  ans[i] = reversed(ans[i])
for r in ans:
  print(' '.join(map(str,r)))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
l=list(map(int,input().split()))
count=0
ans=[["0" for i in range(w)] for j in range(h)]
now=0
index=0
locate=[0,0]
while count<h*w:
  count+=1
  if now==0:
    now+=l[index]
    index+=1
  x=locate[0]
  y=locate[1]
  ans[x][y]=str(index)
  if x%2==0:
    if y<w-1:
      y+=1
    else:
      x+=1
  else:
    if y!=0:
      y-=1
    else:
      x+=1
  now-=1
  locate=[x,y]
for i in range(h):
  print(" ".join(ans[i]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())  # H行W列のgridgraph
N = int(input())  # N色に塗り分ける
A = list(map(int, input().split()))  # 色iのマス数はA[i]

# W個ごとにループを反対にする
ans = [[-1]*W for _ in range(H)]
colors = []
for i in range(len(A)):
    colors.extend([i+1]*A[i])
pointa = 0
for i in range(H):
    # print(i)
    if i % 2 == 0:
        for j in range(W):
            ans[i][j] = colors[pointa]
            pointa += 1
    else:
        for j in reversed(range(W)):
            ans[i][j] = colors[pointa]
            pointa += 1
for a in ans:
    print(*a)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
L=list(map(int,input().split()))
col=0
M=[[0]*W for _ in range(H)]



for i in range(H):
    if i%2==0:
        for j in range(W):
        # print(i)
        # print(j)
            if L[col]>0:
                M[i][j]=col+1
                L[col]-=1
            if L[col]==0:
                col+=1
    else:
        for j in range(W-1,-1,-1):
        # print(i)
        # print(j)
            if L[col]>0:
                M[i][j]=col+1
                L[col]-=1
            if L[col]==0:
                col+=1

for q in range(H):
    for w in range(W):
        print(M[q][w],end=" ")
    print()

Submission status: Accepted

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

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

now = 0
for i in range(H):
    if i % 2 == 0:
        for j in range(W):
            B[i][j] = now+1
            A[now] -= 1
            if A[now] == 0:
                now += 1
    else:
        for j in range(W)[::-1]:
            B[i][j] = now+1
            A[now] -= 1
            if A[now] == 0:
                now += 1

for line in B:
    print(*line)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
C=[[0 for w in range(W)] for h in range(H)]

i=1
for h in range(H):
    for w in (range(W) if h%2==0 else range(W-1,-1,-1)):
        #print(h,w)
        C[h][w]=i
        A[i-1]-=1
        if not A[i-1]:
            i+=1
for c in C:
    c=list(map(str,c))
    print(" ".join(c))

Submission status: Accepted

Solution code:
# 2019-11-20 23:47:42(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    H, W, n, *a = [int(x) for x in sys.stdin.read().split()]
    color_count = [None] + a 
    
    canvas = [[None for _ in range(W)] for _ in range(H)]
    
    h, w = 0, 0
    for i in range(1, n+1):
        while color_count[i] > 0:
            canvas[h][w] = i
            color_count[i] -= 1

            if h % 2 == 0:
                w += 1
                if w == W:
                    h += 1
                    w = W - 1
            else:
                w -= 1
                if w == -1:
                    h += 1
                    w = 0

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

if __name__ == "__main__":
    main()

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 = [[0]*w for _ in range(h)]

i, j = 0, 0
for k, l in enumerate(a):
    for _ in range(l):
        c[i][j] = str(k + 1)
        j += 1
        if j == w:
            j = 0
            i += 1

for i in range(h):
    if i % 2 == 1:
        c[i] = list(reversed(c[i]))

for i in c:
    print(" ".join(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)
cnt = 0
for idx,a in enumerate(A):
    for i in range(a):
        ans[cnt+i] = idx+1
    cnt += a
#print(ans)
# ans　をWずつ折り返して表示
for h in range(H):
    if h % 2 == 0:
        print(*ans[h*W:(h+1)*W])
    else:
        temp = ans[h*W:(h+1)*W]
        print(*temp[::-1])

        #print(*ans[h*W:(h+1)*W:-1])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
count=0

h,w=map(int,input().split())
ans=[[0]*w for i in range(h)]
n=int(input())
A=list(map(int,input().split()))
now=0
for i in range(h):
    for j in range(w):
        a=j
        if i%2==1:
            j=w-j-1
        if not A[now]:
            now+=1
        A[now]-=1
        ans[i][j]=now+1
        j=a
        # print(now)
for a in ans:
    print(*a)
# print(A)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
A=list(map(int,input().split()))
MAP=[[0 for i in range(w)] for j in range(h)]
s,num=0,1
for i in range(h):
    if i%2==0:add=[k for k in range(w)]
    else:add=[k for k in reversed(range(w))]
    for j in add:
        MAP[i][j]=num
        A[s] -=1
        if A[s]==0:
            s +=1
            num +=1

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))

l=[]
for i in range(N):
    l+=(([i+1]*A[i]))

#print(*l)

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

Submission status: Accepted

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

cur_W = 0 # 今いるx座標
cur_H = 0 # 今いるy座標
# 左上から右へ、次は下の段を左へ塗っていく
answer = [[0] * W for _ in range(H)]
sgn = 1 # 最初は右向き
for i in range(1, len(A)+1):
    for j in range(A[i-1]):
        answer[cur_H][cur_W] = i
        cur_W += sgn
        if not 0 <= cur_W <= W-1:
            cur_W -= sgn
            cur_H += 1
            sgn *= -1

for x in answer:
    print(*x)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
c=list(map(int,input().split()))
d=[]
for i in range(1,n+1):
  for j in range(c[i-1]):
    d.append(i)
ansl=[[0]*w for i in range(h)]
for i in range(h):
  if i%2==0:
    for j in range(w):
      ansl[i][j]=str(d[i*w+j])
  else:
    for j in range(w):
      ansl[i][w-j-1]=str(d[i*w+j])
for j in ansl:
  print(" ".join(j))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
s=[]
for i in range(1,n+1):
  for j in range(a[i-1]):
    s.append(str(i))
b=[]
for i in range(h):
  if i%2==0:
    b.append(s[:w])
  else:
    t=s[:w]
    b.append(t[::-1])
  s=s[w:]
for i in range(h):
  print(*b[i])

Submission status: Accepted

Solution code:
import sys

#+++++
		
def main():
	h, w = map(int, input().split())
	n = int(input())
	anl=list(map(int, input().split()))
	ans=[]
	cm=0
	for color, ni in enumerate(anl, start=1):
		v=ni
		while v > 0:
			cl = w - len(ans)
			if cl > v:
				ans = ans + [color] * v
				v=0
			else:
				ans = ans + [color] * cl
				v-=cl
				ans = ans
				if cm % 2 == 1:
					ans=ans[::-1]
				pp=' '.join([str(v) for v in ans])
				print(pp)
				cm+=1
				ans=[]
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
c = 0
l = [ [0]*w for _ in range(h) ]
for i in range(n):
  for j in range(a[i]):
    l[c//w][c%w] = i+1
    c += 1

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