import math def buildMovesForColor(current, gummiesForColor, m): all = {} all_keys = 0 min1 = min(current.values()) for g in gummiesForColor: if (all_keys == m) : s = max(all.values()) - g[1] if (s < min1): break for c in current.items(): if c[1] >= s: continue g_ = [(c[0] + g[0]) % m, c[1] + g[1]] if all[g_[0]] > g_[1]: all[g_[0]] = g_[1] else: for c in current.items(): g_ = [(c[0] + g[0]) % m, c[1] + g[1]] if g_[0] not in all: all[g_[0]] = g_[1] all_keys += 1 elif all[g_[0]] > g_[1]: all[g_[0]] = g_[1] return all def buildMoves(gummies, k, m): cp = {0: 0} for i in range(1, k+1): cp = buildMovesForColor(cp, gummies[i], m) if len(cp) == 0: return [] return list(cp.items()) [n, k, m] = list(map(int, input().split())) def sort(arr): arr.sort(key=lambda x: x[1]) return arr gummies = {} for i in range(1, k + 1): gummies[i] = [] for i in range(n): data = list(map(int, input().split())) gummies[data[0]].append(data[1:]) gummies = {k: sort(v) for k, v in gummies.items()} moves = buildMoves(gummies, k, m) if len(moves) == 0: print(0) for i in range(m - 1): print(-1) exit() if len(moves) == m: mm = min(moves, key=lambda x: x[1])[1] ma = max(moves, key=lambda x: x[1])[1] if ma < 2 * mm: print(0) moves = sorted(moves, key=lambda x: x[0]) for i in range(1, m): print(moves[i][1]) exit() moves.sort(key=lambda x: x[1]) best = [-1] * (m) for mo in moves: best[mo[0]] = mo[1] nodes = [*moves] key_func = lambda x: x[1] loop = 0 worst = max(best) while len(nodes) > loop: current = nodes[loop] mod = current[0] # if current[1] > worst: # break if current[1] > best[mod]: loop += 1 continue for mo in moves: new = [(current[0] + mo[0]) % m, current[1] + mo[1]] if new[1] < best[new[0]] or best[new[0]] == -1: tmp = best[new[0]] best[new[0]] = new[1] if tmp == worst: worst = max(best) # bisect.insort_left(nodes, new, key=key_func) nodes.append(new) elif new[1] >= worst: break loop += 1 # print(loop, len(nodes)) best[0] = 0 for b in best: print(b)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | import math def buildMovesForColor(current, gummiesForColor, m): all = {} all_keys = 0 min1 = min(current.values()) for g in gummiesForColor: if (all_keys == m) : s = max(all.values()) - g[1] if (s < min1): break for c in current.items(): if c[1] >= s: continue g_ = [(c[0] + g[0]) % m, c[1] + g[1]] if all[g_[0]] > g_[1]: all[g_[0]] = g_[1] else: for c in current.items(): g_ = [(c[0] + g[0]) % m, c[1] + g[1]] if g_[0] not in all: all[g_[0]] = g_[1] all_keys += 1 elif all[g_[0]] > g_[1]: all[g_[0]] = g_[1] return all def buildMoves(gummies, k, m): cp = {0: 0} for i in range(1, k+1): cp = buildMovesForColor(cp, gummies[i], m) if len(cp) == 0: return [] return list(cp.items()) [n, k, m] = list(map(int, input().split())) def sort(arr): arr.sort(key=lambda x: x[1]) return arr gummies = {} for i in range(1, k + 1): gummies[i] = [] for i in range(n): data = list(map(int, input().split())) gummies[data[0]].append(data[1:]) gummies = {k: sort(v) for k, v in gummies.items()} moves = buildMoves(gummies, k, m) if len(moves) == 0: print(0) for i in range(m - 1): print(-1) exit() if len(moves) == m: mm = min(moves, key=lambda x: x[1])[1] ma = max(moves, key=lambda x: x[1])[1] if ma < 2 * mm: print(0) moves = sorted(moves, key=lambda x: x[0]) for i in range(1, m): print(moves[i][1]) exit() moves.sort(key=lambda x: x[1]) best = [-1] * (m) for mo in moves: best[mo[0]] = mo[1] nodes = [*moves] key_func = lambda x: x[1] loop = 0 worst = max(best) while len(nodes) > loop: current = nodes[loop] mod = current[0] # if current[1] > worst: # break if current[1] > best[mod]: loop += 1 continue for mo in moves: new = [(current[0] + mo[0]) % m, current[1] + mo[1]] if new[1] < best[new[0]] or best[new[0]] == -1: tmp = best[new[0]] best[new[0]] = new[1] if tmp == worst: worst = max(best) # bisect.insort_left(nodes, new, key=key_func) nodes.append(new) elif new[1] >= worst: break loop += 1 # print(loop, len(nodes)) best[0] = 0 for b in best: print(b) |