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:
def main():
    h,w = map(int,input().split())
    n=int(input())
    a = list(map(int,input().split()))
    ans=[[0 for _ in range(w)] for _ in range(h)]
    x=y=key=0
    for i in range(n):
        for _ in range(a[i]):
            ans[x][y]=str(i+1)
            if key==0:
                y+=1
            else:
                y-=1
            if key==0 and y==w:
                x+=1
                y-=1
                key=1
            elif key==1 and y==-1:
                x+=1
                y+=1
                key=0
    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())
l = []
a = list(map(int,input().split()))
for i in range(n):
  for j in range(a[i]):
    l.append(i+1)
for i in range(h):
  if i % 2 == 0:
    print(' '.join(map(str,l[i*w:(i+1)*w])))
  else:
    b = l[i*w:(i+1)*w]
    b.reverse()
    print(' '.join(map(str,b)))

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)]
now=0
nokori=a[0]
for i in range(h):
    if i%2==0:
        for j in range(w):
            ans[i][j]=now+1
            nokori-=1
            if nokori==0:
                now+=1
                if now<n:
                    nokori=a[now]
    else:
        for j in range(w-1,-1,-1):
            ans[i][j]=now+1
            nokori-=1
            if nokori==0:
                now+=1
                if now<n:
                    nokori=a[now]

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

Submission status: Accepted

Solution code:
from collections import deque

def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    d = deque()
    for i in range(N):
        for a in range(A[i]):
            d.append(i+1)
    C = [[0] * W for _ in range(H)]

    for h in range(H):
        if h % 2 == 0:
            for w in range(W):
                C[h][w] = d.popleft()
        else:
            for w in range(W-1,-1,-1):
                C[h][w] = d.popleft()
    
    for h in range(H):
        print(*C[h])


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = [[0 for x in range(w)] for y in range(h)]
index = 0
now = a_array[index]
reverse = True

for i in range(h):
    if reverse:
        w_range = [x for x in range(w)]
        reverse = False
    else:
        w_range = [x for x in reversed(range(w))]
        reverse = True
    for j in w_range:
        if now == 0:
            index += 1
            now = a_array[index]
        ans[i][j] = index+1
        now -= 1

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

Submission status: Accepted

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

breakA = []
for i, v in enumerate(A):
  for _ in range(v):
    breakA.append(i + 1)

# breakAをWでchunck
# 奇数行目だけ[::-1]
import itertools
def splitparN(iterable, N):
    for i, item in itertools.groupby(enumerate(iterable), lambda x: x[0] // N):
        yield (x[1] for x in item)

for i, x in enumerate(splitparN(breakA, W)):
  if i % 2 == 0:
    print(*list(x))
  else:
    print(*list(x)[::-1])

Submission status: Accepted

Solution code:
# ARC080D - Grid Coloring (ABC069D)
def main():
    """
    move left to right in odd rows and vice versa in even rows
    1 s--->
    2 <----
    3 --->t
    """
    H, W, N, *A = map(int, open(0).read().split())
    grid = sum([[color] * cnt for color, cnt in enumerate(A, 1)], [])
    for i, g in enumerate(zip(*[iter(grid)] * W), 1):
        if i & 1:
            print(*g)
        else:
            print(*g[::-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()))

M = [[0 for _ in range(W)] for _ in range(H)]
i = 0
j = 0
dj = 1
k = 0
l = 0
while(l<H*W):
  if A[k] == 0:
    k += 1
  M[i][j] = k+1
  A[k] -= 1
  j += dj
  if j > W-1:
    j = W-1
    dj = -1
    i += 1
  elif j < 0:
    j = 0
    dj = 1
    i += 1
  l += 1
for m in M:
  print(*m)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
h, w = [int(i) for i in sys.stdin.readline().split()]
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
cumsum = [j for j in accumulate(a_ls)]
start = [0,0]
flg = True
_map = [[0 for i in range(w)] for j in range(h)]
color = 1
cnt = 0
while True:
    if cnt in cumsum:
        color += 1
    y, x = start
    _map[y][x] = color
    if flg:
        if x + 1 >= w:
            if y + 1 >= h:
                break
            flg = False
            y += 1
        else:
            x += 1
    else:
        if x - 1 < 0:
            if y + 1 >= h:
                break
            y += 1
            flg = True
        else:
            x -= 1
    _next = [y, x]
    start = _next
    cnt += 1

for y in range(h):
    print(" ".join([str(j) for j in _map[y]]))

Submission status: Accepted

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

H, W = list(map(int, input().split()))
N = int(input().split()[0])
a_list = list(map(int, input().split()))

c_list = []

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

for i in range(H):
    w_list = c_list[W * i : W * (i + 1)]
    if i % 2 == 1:
        w_list = reversed(w_list)
    print(" ".join([str(w) for w in w_list]))

Submission status: Accepted

Solution code:
from sys import stdin
H, W = [int(_) for _ in stdin.readline().rstrip().split()]
N = int(stdin.readline().rstrip())
A = [int(a) for a in stdin.readline().rstrip().split()]

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

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

index = 0
for h in range(H):
  for w in range(W):
    ans[h][w] = flattened[index]
    index += 1

for h in range(H):
  if h % 2 == 0:
    print(*ans[h], sep=" ")
  else:
    print(*ans[h][::-1], sep=" ")

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    dx = [1, -1]
    mode = 0
    ans = [[0]*w for _ in range(h)]
    x = 0
    y = 0
    i = 0
    while(0 <= y < h):
        while(0 <= x < w):
            if(a[i] > 0):
                ans[y][x] = i+1
                x += dx[mode]
                a[i] -= 1
            else:
                i += 1
        y += 1
        mode = 1-mode
        x += dx[mode]
    for i in ans:
        print(*i)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

h,w = inpl()
n = inp()
a = inpl()
q = deque(a)
tmp = q.popleft()
cnt = 1
res = [[0 for i in range(w)] for j in range(h)]
for y in range(h):
    for x in range(w):
        if y%2:
            x = w - x - 1
        if tmp == 0:
            tmp = q.popleft()
            cnt += 1
        res[y][x] = cnt
        tmp -= 1
for y in range(h):
    for x in range(w):
        print(res[y][x])

Submission status: Accepted

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

ind = 0
counter = 0
ans = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
    if i%2 == 0:
        for j in range(w):
            ans[i][j] = ind+1
            counter += 1
            if counter == a[ind]:
                ind += 1
                counter = 0
    else:
        for j in range(w):
            ans[i][w-j-1] = ind+1
            counter += 1
            if counter == a[ind]:
                ind += 1
                counter = 0
for i in range(h):
    for j in range(w):
        print(ans[i][j], end=' ')
    print()

Submission status: Accepted

Solution code:
H,W= list(map(int, input().split()))
N= int(input())
a= list(map(int, input().split()))
li=[[0]*W for i in range(H)]
color=0
x=0;y=0
r=0;vec=0
for i in a:
    color+=1
    for _ in range(i):
        li[x][y]=color
        if vec:
            if r==1:
                y+=1
            else:
                y-=1
        else:
            if r==0:
                x+=1
            else:
                x-=1
        try :
            if li[x][y]!=0:
                if vec:
                    if r==1:
                        y-=1
                    else:
                        y+=1
                else:
                    if r==0:
                        x-=1
                    else:
                        x+=1
                r+=1
                r%=4
                vec^=1
                if vec:
                    if r==1:
                        y+=1
                    else:
                        y-=1
                else:
                    if r==0:
                        x+=1
                    else:
                        x-=1
        except:
            if vec:
                if r==1:
                    y-=1
                else:
                    y+=1
            else:
                if r==0:
                    x-=1
                else:
                    x+=1
            r+=1
            r%=4
            vec^=1
            if vec:
                if r==1:
                    y+=1
                else:
                    y-=1
            else:
                if r==0:
                    x+=1
                else:
                    x-=1
for i in li:
    for j in i:
        print(j,end=" ")
    print("")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
boje = []
for i, x in enumerate(a):
    for j in range(x):
        boje.append(i + 1)
m = [[0 for j in range(w)] for i in range(h)]
cnt = 0
for i in range(h):
    if i % 2 == 0:
        for j in range(w):
            m[i][j] = boje[cnt]
            cnt += 1
    else:
        for j in range(w - 1, -1, -1):
            m[i][j] = boje[cnt]
            cnt += 1
    print(' '.join(map(str, m[i])))

Submission status: Accepted

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

n = int(input())

arr = list(map(int,input().split()))
ans = [[0 for i in range(w)] for j in range(h)]

right = True
temp = 0
ind = 1
for i in range(h):
    for j in range(w):
        if right:
            ans[i][j] = ind
            temp += 1
        else:
            ans[i][-j-1] = ind
            temp += 1
        if temp>=arr[ind-1]:
            ind += 1
            temp = 0
    right = not right

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

Submission status: Accepted

Solution code:
h,w  = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
res = []
for _ in range(h):
    res.append([])
line = 0

def insert(res,i,l):
    if l %2 == 0:
        res[l].append(str(i+1))
    else:
        res[l].insert(0,str(i+1))

for i in range(0,n):
    ammount=a[i]
    while len(res[line]) < w and ammount > 0:
        insert(res,i,line)
        ammount -= 1
    if len(res[line]) == w:
        line += 1
    while ammount >= w:
        res[line] = [str(i+1) for _ in range(w)]
        ammount -= w
        line += 1
    if ammount > 0:
        res[line] = [str(i+1) for _ in range(ammount)]

for i in range(0,h):
    print(' '.join(res[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()))
    ans = [[0]*W for i in range(H)]
    x = 0
    y = 0
    flag = 0
    for i in range(N):
        color = i+1
        num = A[i]
        for j in range(num):
            ans[y][x] = color
            if flag==0:
                x+=1
                if x==W:
                    x = W-1
                    flag = 1
                    y += 1
            else:
                x-=1
                if x==-1:
                    x = 0
                    flag=0
                    y += 1
    for i in range(H):
        print(*ans[i])
    
main()

Submission status: Accepted

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

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

a=0
ii=0
for i in range(H):
  for j in range(W):
    if a==0:
      a=A[ii]
      ii+=1
    G[i][j]=str(ii)
    a-=1

for i in range(H):
  if i%2==0:
    print(" ".join(G[i]))
  else:
    print(" ".join(G[i][::-1]))

Submission status: Accepted

Solution code:
import sys
import heapq
import bisect

mod = 10**9+7
dd = ((-1,0),(1,0),(0,-1),(0,1))

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def S(): return(sys.stdin.readline()[:-1])
def IR(n): return([I() for _ in range(n)])

def GCD(a,b):
    while b!=0:
        a,b = b,a%b
    return a

def LCM(a,b):
    return a * b // GCD(a,b)

def main():
    H,W = LI()
    N = I()
    A = LI()
    HW = [[0] * W for _ in range(H)]
    
    a_flag = [1,A[0]]
    for h in range(H):
        if h%2 == 0:
            range_W = range(W)
        else:
            range_W = range(W-1,-1,-1)
        for w in range_W:
            HW[h][w] = a_flag[0]
            if a_flag[1] == 1 and a_flag[0] < len(A):
                a_flag = [a_flag[0]+1,A[a_flag[0]]]
            else:
                a_flag[1] -= 1

    for x in HW:
        print(" ".join(map(str,x)))



            

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
import sys
sys.setrecursionlimit(1000000)

def solve():
  h,w = (int(i) for i in input().split())
  n = int(input())
  a = list(int(i) for i in input().split())
  query = []
  flag = True
  for i in range(n):
    times = a[i]
    number = i+1
    for j in range(times):
      query.append(number)
  ans = []
  retu = []
  while query:
    nextnumber = query.pop(0)
    if len(retu) < w:
      retu.append(nextnumber)
    else:
      ans.append(retu)
      retu = [nextnumber]
  ans.append(retu)
  for i in range(h):
    if i%2 == 1:
      ans[i] = ans[i][::-1]
  for j in range(h):
    print(*ans[j])
solve()

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)]
ai = 0

for i in range(h):
    f, e, s = 0, w, 1
    if i % 2: f, e, s = w-1, -1, -1 
    for j in range(f, e, s):
        ai += a[ai] <= 0
        a[ai] -= 1
        c[i][j] = ai+1

for l in c: 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 i in range(H)]
count = 0
reverse = False
for i in range(H):
    if reverse:
        for j in reversed(range(W)):
            if a[count] == 0:
                count += 1
            grid[i][j] = count+1
            a[count] -= 1
        reverse = False
    else:
        for j in range(W):
            if a[count] == 0:
                count += 1
            grid[i][j] = count+1
            a[count] -= 1

        reverse = True


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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
from heapq import *
sys.setrecursionlimit(200000)
input = sys.stdin.readline

H, W = map(int, input().split())
N, = map(int, input().split())
L = [int(v) for v in input().split()]
# L = [int(input()) for _ in range(Q)]
# S = input().strip()

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

def bfs(i, j, remain, now):
    if now > N:
        return

    if remain == 0:
        bfs(i, j, L[now], now + 1)
        return

    if m[i][j] == 0:
        m[i][j] = now
    else:
        return

    if i + 1 < H and m[i + 1][j] == 0:
        bfs(i + 1, j, remain - 1, now)

    if i - 1 >= 0 and m[i - 1][j] == 0:
        bfs(i - 1, j, remain - 1, now)

    if j + 1 < W and m[i][j + 1] == 0:
        bfs(i, j + 1, remain - 1, now)

    if j - 1 >= 0 and m[i][j - 1] == 0:
        bfs(i, j - 1, remain - 1, now)        

bfs(0, 0, L[0], 1)

for v in m:
    print(*v)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
a=list(map(int,input().split()))
a_all=[]
c=[[""]*W for i in range(H)]

for i in range(N):
    b=[i+1]*a[i]
    a_all.extend(b)

i=0
for j in range(H):
    if j%2==0:
        for k in range(W):
            c[j][k]=str(a_all[i])
            i+=1
    else:
        for k in range(W-1,-1,-1):
            c[j][k]=str(a_all[i])
            i+=1

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
ans=[]
i=0
rest=a[i]
for j in range(h):
    ans_j=[]
    for k in range(w):
        if(rest==0):
            i+=1
            rest=a[i]

        if(j%2==0):
            ans_j.append(i+1)
            rest-=1
        else:
            ans_j.insert(0, i+1)
            rest-=1
    ans.append(ans_j)

for i in range(len(ans)):
    for j in range(len(ans[i])):
        print(ans[i][j],end=" ")
    print()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    H,W = map(int,input().split())
    N = int(input())
    a = list(map(int,input().split()))
    cl = []
    for i in range(N):
        cl += [(i+1) for _ in range(a[i])]

    ans = [[] for _ in range(H)]
    for i in range(H):
        ans[i] = cl[(W*i):(W*(i+1))]

    for i in range(H):
        if i%2 == 1:
            ans[i].reverse()
        print(*ans[i])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# 2019/09/09

from collections import deque

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

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

res=deque(res)

for i in range(h):
    tmp=[]
    for j in range(w):
        tmp.append(res.popleft())
    if i%2:print(*tmp[::-1])
    else:print(*tmp)

Submission status: Accepted

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

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

h = -1
ans = []
for i, b in enumerate(B):
    if i % W == 0:
        h += 1
        C = []
    C.append(b)
    if i % W == W-1:
        if h%2 == 0:
            ans.append(C)
        else:
            ans.append(C[::-1])

for C in ans:
    print(' '.join([str(c) for c in C]))

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
n = int(input())
a = list(map(int, input().split()))
b = [[0 for i in range(w)] for j in range(h)]
top = 0
bot = h-1
left = 0
right = w-1
x = 0
y = 0
flag = 0

for i in range(n):
  cnt = a[i]
  while cnt>0:
    b[y][x] = i+1
    cnt -= 1
    if flag==0 and x==left and y==bot:
      x += 1
      left += 1
      flag = (flag+1)%4
    elif flag==1 and x==right and y==bot:
      y -= 1
      bot -= 1
      flag = (flag+1)%4
    elif flag==2 and x==right and y==top:
      x -= 1
      right -= 1
      flag = (flag+1)%4
    elif flag==3 and x==left and y==top:
      y += 1
      top += 1
      flag = (flag+1)%4
    elif flag == 0:
      y += 1
    elif flag == 1:
      x += 1
    elif flag == 2:
      y -= 1
    elif flag == 3:
      x -= 1
for row in b:
  print(*row)

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
*A, = map(int, input().split())
grid = [[-1]*W for _ in range(H)]
order = []
for i in range(H):
    if i % 2 == 0:
        for j in range(W): order.append((i, j))
    else:
        for j in reversed(range(W)): order.append((i, j))
idx = 0
for i, a in enumerate(A):
    color = i + 1
    for x, y in order[idx: idx+a]: grid[x][y] = color
    idx += a
for g in grid:
    print(*g)

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 = np.zeros((H*W), dtype=np.int)

idx = 0
for i in range(1, N+1):
    ans[idx:idx+1+a[i-1]] = i
    idx += a[i-1]

ans = np.reshape(ans, (H, W))

x = [1, -1]

for i in range(H):
    print(' '.join(map(str, list(ans[i])[::x[i%2]])))

Submission status: Accepted

Solution code:
#import pysnooper
#import os,re,sys,operator,math,heapq,string
#from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy

setrecursionlimit(10**6)
input=stdin.readline

h,w=map(int,input().split())
n=int(input().rstrip())
a=[int(i) for i in input().split()]
nh,nw=0,0
ans=[[0]*w for i in range(h)]
now=True
for i,j in enumerate(a,1):
    for _ in range(j):
        if nw<0:
            nh+=1
            nw+=1
            now=not now
        elif nw>=w:
            nh+=1
            nw-=1
            now=not now
        ans[nh][nw]=i
        if now:
            nw+=1
        else:
            nw-=1
for i in ans:
    print(*i)

Submission status: Accepted

Solution code:
H,W,N,*A=map(int,open(0).read().split())
for i,r in enumerate(zip(*[iter(sum([[i]*a for i,a in enumerate(A,1)],[]))]*W)):print(*r[::1-2*(i%2)])

Submission status: Accepted

Solution code:
H,W,N,*A=map(int,open(0).read().split())
C=[]
for i,a in enumerate(A,1):C+=[i]*a
for i,r in enumerate(zip(*[iter(C)]*W)):print(*r[::1-2*(i%2)])

Submission status: Accepted

Solution code:
H,W,N,*A=map(int,open(0).read().split())
C=[]
for i,a in enumerate(A,1):C+=[i]*a
for i,r in enumerate(zip(*[iter(C)]*W)):print(*r[::-1]if i%2 else r)

Submission status: Accepted

Solution code:
H,W,N,*A=map(int,open(0).read().split())
C=[]
for i,a in enumerate(A,1):
	C+=[i]*a
for i,r in enumerate(zip(*[iter(C)]*W)):
	print(*r[::-1]if i%2 else r)

Submission status: Accepted

Solution code:
H,W,N,*A=map(int, open(0).read().split())
C = []
for i,a in enumerate(A,1):
	C+=[i]*a
for i,r in enumerate(zip(*[iter(C)]*W)):
	print(*r[::-1]if i%2 else r)

Submission status: Accepted

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

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

for i, r in enumerate(zip(*[iter(C)] * W)):
    if i % 2 == 0:
        print(*r)
    else:
        print(*reversed(r))

Submission status: Accepted

Solution code:
# ARC080D - Grid Coloring (ABC069D)
import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    N = int(input())
    A = tuple(map(int, input().split()))
    F = []  # flatten grid
    for color, cnt in enumerate(A, 1):
        F += [color] * cnt
    for p, i in enumerate(range(1, H + 1), 1):
        if p & 1:  # odd rows (1-idx)
            print(*F[(i - 1) * W : i * W])
        else:
            print(*F[(i - 1) * W : i * W][::-1])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC080D - Grid Coloring (ABC069D)
def main():
    H, W = tuple(map(int, input().split()))
    N = int(input())
    A = tuple(map(int, input().split()))
    F = []  # flatten grid
    for color, cnt in enumerate(A, 1):
        F += [color] * cnt
    par = 1  # parity
    for i in range(1, H + 1):
        if par:
            print(*F[(i - 1) * W : i * W])
        else:
            print(*F[(i - 1) * W : i * W][::-1])
        par ^= 1


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC080D - Grid Coloring (ABC069D)
def main():
    H, W = tuple(map(int, input().split()))
    N = int(input())
    A = tuple(map(int, input().split()))
    G = []  # grid
    for color, cnt in enumerate(A, 1):
        G += [color] * cnt
    par = 0  # parity
    for i in range(1, H + 1):
        if not par:
            print(*G[(i - 1) * W : i * W])
        else:
            print(*G[(i - 1) * W : i * W][::-1])
        par ^= 1


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import copy
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
l = []
l1 = []
key = 0
p = 0
q = 0
for i in range(n):
    count = a[i]
    for j in range(a[i]):
        l1.append(i+1)
        if q == w-1:
            if key % 2 == 0:
                l.append(l1)
            else:
                l1.reverse()
                l.append(l1)
            l1 = []
            q = 0
            key = key + 1
        else:
            q = q + 1
for i in l:
    print(*i)

Submission status: Accepted

Solution code:
def main():
    h,w = map(int,input().split())
    n = int(input())
    ls = [int(x) for x in input().split()]
    ans = [[0 for _ in range(w)] for _ in range(h)]
    i,j = 0,0
    now = 1
    while len(ls)!=0:
        cnt = ls.pop(0)
        while cnt!=0:
            if j==w:
                i+=1
                j= 0
            ans[i][j] = now
            cnt-=1
            j +=1
        now +=1
    
    for i in range(h):
        if i%2==0:
            for j in range(w):
                if j == w-1:
                    print(ans[i][j])
                else:
                    print(ans[i][j],end=" ")
        else:
            for j in reversed(range(w)):
                if j == 0:
                    print(ans[i][j])
                else:
                    print(ans[i][j],end=" ")

if __name__ == '__main__':
    main()

Submission status: Accepted

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

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,m = LI()
    a = LIR(n)
    ans = 0
    for i in range(2):
        for j in range(2):
            for k in range(2):
                a.sort(key = lambda x:(-1)**i*x[0]+(-1)**j*x[1]+(-1)**k*x[2])
                p = [0,0,0]
                for l in range(m):
                    p[0] += a[l][0]
                    p[1] += a[l][1]
                    p[2] += a[l][2]
                p = abs(p[0])+abs(p[1])+abs(p[2])
                if p > ans:
                    ans = p
    print(ans)
    return

#B
def B():
    def root(x):
        if x == par[x]:
            return x
        par[x] = root(par[x])
        return par[x]

    def unite(x,y,su):
        x = root(x)
        y = root(y)
        su += comb_2(s[x])+comb_2(s[y])
        if rank[x] < rank[y]:
            s[y] += s[x]
            su -= comb_2(s[y])
            par[x] = y
        else:
            s[x] += s[y]
            su -= comb_2(s[x])
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1
        return su

    def comb_2(a):
        return a*(a-1)//2

    n,m = LI()
    v = LIR(m)
    par = [i for i in range(n)]
    rank = [0]*n
    s = [1]*n
    su = comb_2(n)
    ans = [su]
    for a,b in v[:0:-1]:
        a -= 1
        b -= 1
        if root(a) != root(b):
            su = unite(a,b,su)
        ans.append(su)
    for i in ans[::-1]:
        print(i)
    return

#C
def C():
    def root(x):
        if par[x] == x:
            return par[x]
        r = root(par[x])
        d[x] += d[par[x]]
        par[x] = r
        return par[x]

    def weight(x):
        root(x)
        return d[x]

    def unite(x,y,w):
        w += weight(x)
        w -= weight(y)
        x = root(x)
        y = root(y)
        if rank[x] < rank[y]:
            par[x] = y
            d[x] = -w
        else:
            par[y] = x
            d[y] = w
            if rank[x] == rank[y]:
                rank[x] += 1
    n,m = LI()
    par = [i for i in range(n)]
    rank = [0]*n
    d = [0]*n
    for i in range(m):
        l,r,w = LI()
        l -= 1
        r -= 1
        if root(l) != root(r):
            unite(l,r,w)
        else:
            if weight(r)-weight(l) != w:
                print("No")
                return
    print("Yes")
    return

#D
def D():
    h,w = LI()
    n = I()
    a = LI()
    ans = [0]*h*w
    p = 0
    for i in range(n):
        for j in range(a[i]):
            ans[p+j] = i+1
        p += a[i]
    for i in range(h):
        s = ans[i*w:(i+1)*w]
        if i%2:
            print(*s[::-1])
        else:
            print(*s)
    return

#E
def E():

    return

#F
def F():

    return

#G
def G():

    return

#H
def H():

    return

#I
def I_():

    return

#Solve
if __name__ == "__main__":
    D()

Submission status: Accepted

Solution code:
def split_list(l, n):

    for dx in range(0, len(l), n):
        yield l[dx:dx + n]

H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
list1 = [0] * H * W

count = 0
for i in range(N):
    for j in range(a[i]):
        list1[count + j] = i + 1
    count += a[i]

ans_list = list(split_list(list1, W))

for i in range(H):
    list2 = list(ans_list[i])
    if i % 2 == 0:
        print(" ".join(map(str, list2)))
    else:
        list2.reverse()
        print(" ".join(map(str, list2)))