# 2 ≤ k ≤ n ≤ 500 000 n, k = map(int, input().split()) # 1 ≤ a ≤ 10^9 a = list(map(int, input().split())) # print(n, k) # print(*a, sep=" ") # pw = [{"v":-1, "i":-1}] * n # for i in reversed(range(n - 1)): # if pw[i + 1]["v"] == -1: # vv = a[i + 1] # ii = i + 1 # else: # vv = pw[i + 1]["v"] # ii = pw[i + 1]["i"] # # print("a[i]", a[i], "vv", vv) # if a[i] < vv: # pw[i] = {"v": vv, "i": ii} # pw1 = list(map(lambda v: v["i"], pw)) # print("prawy większy:\n", pw1) tab = [] if k == 2: # print('k==2') wynik = False min_l = [-1] * n min = a[0] for i in range(n): min_l[i] = min if min > a[i]: min = a[i] # print("min na lewo:\n", min_l) max_p = [-1] * n max = a[n - 1] for i in reversed(range(n - 1)): max_p[i] = max if max < a[i]: max = a[i] # print("max na prawo:\n", max_p) for i in range(n - 1): if min_l[i] >= max_p[i]: tab.append(i + 1) # print("dzielimy na i:", poz) wynik = True break else: # k > 2: pmr = [{"v":-1, "i":-1}] * n for i in reversed(range(n - 1)): if pmr[i + 1]["v"] == -1: vv = a[i + 1] ii = i + 1 else: vv = pmr[i + 1]["v"] ii = pmr[i + 1]["i"] # print("a[i]", a[i], "vv", vv) if a[i] >= vv: pmr[i] = {"v": vv, "i": ii} pmr1 = list(map(lambda v: v["i"], pmr)) # print("prawy mniejszy równy:\n", pmr1) poz = -1 wynik = True moze_sie_da = False # print('k>2') for i in range(n - 1): if pmr1[i] != -1: # jest na prawo mniejszy lub równy moze_sie_da = True poz = pmr1[i] break if moze_sie_da: if k == 3: wynik = False # print("poz:", poz) min1 = a[0] max1 = a[n - 1] if min1 >= max1: wynik = True tab.append(1) tab.append(n - 1) else: # for i in range(1, n - 1): # if min1 >= a[i] or max1 <= a[i]: # wynik = True # break y = 1 for i in range(1, n - 1): if min1 >= a[i]: wynik = True # print("min1 i", i) y = i break tab.append(y) y = y + 1 for i in reversed(range(1, n - 1)): # print("a[i]", a[i]) if max1 <= a[i]: wynik = True # print("max1 i", i, max1, a[i]) y = i break tab.append(y) else: wynik = True tab.append(poz) tab.append(poz + 1) j = 0 while len(tab) < k: j += 1 if j != poz and j != poz + 1: tab.append(j) else: wynik = False # print(*tab, sep=" ") # print('-----') if wynik: print("TAK") tab.sort() print(*tab, sep=" ") else: print("NIE")
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | # 2 ≤ k ≤ n ≤ 500 000 n, k = map(int, input().split()) # 1 ≤ a ≤ 10^9 a = list(map(int, input().split())) # print(n, k) # print(*a, sep=" ") # pw = [{"v":-1, "i":-1}] * n # for i in reversed(range(n - 1)): # if pw[i + 1]["v"] == -1: # vv = a[i + 1] # ii = i + 1 # else: # vv = pw[i + 1]["v"] # ii = pw[i + 1]["i"] # # print("a[i]", a[i], "vv", vv) # if a[i] < vv: # pw[i] = {"v": vv, "i": ii} # pw1 = list(map(lambda v: v["i"], pw)) # print("prawy większy:\n", pw1) tab = [] if k == 2: # print('k==2') wynik = False min_l = [-1] * n min = a[0] for i in range(n): min_l[i] = min if min > a[i]: min = a[i] # print("min na lewo:\n", min_l) max_p = [-1] * n max = a[n - 1] for i in reversed(range(n - 1)): max_p[i] = max if max < a[i]: max = a[i] # print("max na prawo:\n", max_p) for i in range(n - 1): if min_l[i] >= max_p[i]: tab.append(i + 1) # print("dzielimy na i:", poz) wynik = True break else: # k > 2: pmr = [{"v":-1, "i":-1}] * n for i in reversed(range(n - 1)): if pmr[i + 1]["v"] == -1: vv = a[i + 1] ii = i + 1 else: vv = pmr[i + 1]["v"] ii = pmr[i + 1]["i"] # print("a[i]", a[i], "vv", vv) if a[i] >= vv: pmr[i] = {"v": vv, "i": ii} pmr1 = list(map(lambda v: v["i"], pmr)) # print("prawy mniejszy równy:\n", pmr1) poz = -1 wynik = True moze_sie_da = False # print('k>2') for i in range(n - 1): if pmr1[i] != -1: # jest na prawo mniejszy lub równy moze_sie_da = True poz = pmr1[i] break if moze_sie_da: if k == 3: wynik = False # print("poz:", poz) min1 = a[0] max1 = a[n - 1] if min1 >= max1: wynik = True tab.append(1) tab.append(n - 1) else: # for i in range(1, n - 1): # if min1 >= a[i] or max1 <= a[i]: # wynik = True # break y = 1 for i in range(1, n - 1): if min1 >= a[i]: wynik = True # print("min1 i", i) y = i break tab.append(y) y = y + 1 for i in reversed(range(1, n - 1)): # print("a[i]", a[i]) if max1 <= a[i]: wynik = True # print("max1 i", i, max1, a[i]) y = i break tab.append(y) else: wynik = True tab.append(poz) tab.append(poz + 1) j = 0 while len(tab) < k: j += 1 if j != poz and j != poz + 1: tab.append(j) else: wynik = False # print(*tab, sep=" ") # print('-----') if wynik: print("TAK") tab.sort() print(*tab, sep=" ") else: print("NIE") |