description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) while t: s, k = map(int, input().split()) a = list(map(int, input().split())) color = [0] * s d = {} for i in range(s): if a[i] not in d: d[a[i]] = [] d[a[i]].append(i) leftover = [] for i in d: if len(d[i]) >= k: for j in range(k): color[d[i][j]] = j + 1 else: leftover.extend(d[i]) m = len(leftover) leftover = leftover[: m - m % k] m = len(leftover) c = 1 for i in range(m): if c > k: c = 1 color[leftover[i]] = c c = c + 1 print(" ".join(list(map(str, color)))) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
from sys import stdin def sortt(aa): return arr[aa] for _ in range(int(stdin.readline())): n, k = map(int, stdin.readline().split()) arr = list(map(int, stdin.readline().split())) d = [[] for i in range(n + 1)] ans = [(0) for i in range(n)] el = [] s = 0 j = 0 for i in arr: if len(d[i]) < k: d[i].append(j) el.append(j) s += 1 j += 1 a = s // k t = a * k el.sort(key=sortt) cl = 1 for i in range(t): ans[el[i]] = cl cl += 1 if cl == k + 1: cl = 1 print(*ans)
FUNC_DEF RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def Solve(): n, k = map(int, input().split()) a = list(map(int, input().split())) dic = {} for i in a: if dic.get(i) == None: dic[i] = 1 else: dic[i] += 1 lst = list(dic.values()) b = [(0) for i in a] dic2 = {} s = 0 for i in range(len(a)): if dic[a[i]] >= k: if dic2.get(a[i]) == None: b[i] = k dic2[a[i]] = k - 1 else: b[i] = dic2[a[i]] if dic2[a[i]] > 0: dic2[a[i]] -= 1 else: s += 1 dic3 = {} for i in range(len(a)): if dic[a[i]] < k: if dic3.get(a[i]) == None: dic3[a[i]] = [i] else: dic3[a[i]].append(i) lst = list(dic3.values()) p = k r = 1 for i in range(len(lst)): for j in range(len(lst[i])): if r * k <= s: b[lst[i][j]] = p p -= 1 if p == 0: p = k r += 1 else: b[lst[i][j]] = 0 for i in b: print(i, end=" ") print() q = int(input()) while q > 0: Solve() q -= 1
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for i in range(t): n, k = map(int, input().split()) w = [] dic = {} l = list(map(int, input().split())) for i in range(n): el = l[i] w.append([el, i, 0]) if el in dic: dic[el] += 1 else: dic[el] = 1 w = sorted(w) j = 0 while j < n: for i in range(k): if dic[w[j][0]] > k: j += dic[w[j][0]] - k dic[w[j][0]] = k w[j][2] = i + 1 j += 1 if j >= n: break j = n - 1 while w[j][2] != k: w[j][2] = 0 j -= 1 w.sort(key=lambda x: x[1]) for el in w: print(el[2], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
MULTIPLE_CASE = True def solve(): n, k = [int(i) for i in input().split(" ")] data = [[] for _ in range(n)] a = [(int(i) - 1) for i in input().split(" ")] colors = [0] * n for pos in range(n): data[a[pos]].append(pos) not_enough = 0 for pos in data: if len(pos) >= k: for j in range(k): colors[pos[j]] = j + 1 elif 0 < len(pos) < k: not_enough += len(pos) not_enough -= not_enough % k for pos in data: if 0 < len(pos) < k: for i in pos: if not_enough > 0: colors[i] = not_enough % k + 1 not_enough -= 1 else: break for i in colors: print("{} ".format(i), end="") print() T = 1 if MULTIPLE_CASE: T = int(input()) while T > 0: T -= 1 solve()
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR VAR IF NUMBER FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) while t: n, k = map(int, input().split()) a = list(map(int, input().split())) d = {} ans = [0] * n ex = [] for i in range(n): if a[i] in d: d[a[i]].append(i) else: d[a[i]] = [] d[a[i]].append(i) for i in d: if len(d[i]) >= k: for x in range(1, k + 1): ans[d[i][x - 1]] = x else: ex += d[i] if len(ex) >= k: for x in range(1, k + 1): ans[ex[x - 1]] = x ex = ex[k:] for i in ans: print(i, end=" ") print() t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
T = int(input()) while T > 0: T -= 1 n, k = map(int, input().split()) a = list(map(int, input().split())) indices = {} p = [0] * n sb = 0 for i in range(n): if a[i] not in indices: indices[a[i]] = [] if len(indices[a[i]]) == k: continue indices[a[i]].append(i) sb += 1 sb -= sb % k for idx in indices: for i in indices[idx]: p[i] = sb % k + 1 sb -= 1 if sb == 0: break if sb == 0: break print(" ".join(map(str, p)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
mini = 100000 tests = int(input()) mod = 10**9 + 7 while tests: tests -= 1 arr = list(map(int, input().split())) n, k = arr[0], arr[1] arr = list(map(int, input().split())) d = {} distinct = 0 stri = "" ct = 1 loc = -1 for i in arr: loc += 1 if i in d: if len(d[i]) < k: d[i].append(loc) distinct += 1 else: arr[loc] = 0 else: d[i] = [loc] distinct += 1 ct = 1 stri = "" can_be_colored = distinct - int(distinct % k) for i in d: for j in d[i]: if can_be_colored > 0: can_be_colored -= 1 col = ct % k + 1 arr[j] = col ct += 1 else: arr[j] = 0 ans = "" for i in arr: ans += str(i) + " " print(ans)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR FOR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
i = int(input("")) li = [0] * i for a in range(i): count = {} l2 = input("") l2 = l2.split(" ") length = int(l2[0]) color = int(l2[1]) stri = input("") lis = stri.split(" ") for i in range(len(lis)): lis[i] = int(lis[i]) for j in range(len(lis)): if lis[j] in count: count[lis[j]].append(j) else: count[lis[j]] = [j] finallis = [0] * length sum = 0 for key in count: if len(count[key]) >= color: sum += len(count[key]) ini = 1 for i in count[key]: if ini <= color: finallis[i] = ini ini += 1 else: finallis[i] = 0 num = (length - sum) // color unpainted = (length - sum) % color ini = 1 round = 0 unpain = 0 for key in count: if len(count[key]) < color: for i in count[key]: if round < num: if ini < color: finallis[i] = ini ini += 1 else: finallis[i] = color ini = 1 round = round + 1 elif unpain < unpainted: finallis[i] = 0 unpain += 1 li[a] = finallis for i in range(len(li)): for j in li[i]: print(j, end=" ") if i < len(li) - 1: print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) c = [[] for i in range(n + 1)] a = list(map(int, input().split())) z = 0 for i in range(n): if len(c[a[i]]) < k: c[a[i]].append(i) z += 1 b = z // k d = b * k r = [0] * n j = 0 for v in c: for w in v: r[w] = j % k + 1 j += 1 if j == d: print(" ".join(map(str, r))) return print(" ".join(map(str, r))) for i in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys N = int(200000.0 + 5) sys.setrecursionlimit(N) def charming(): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(zip(a, range(n))) b.sort(key=lambda x: x[0]) cnt = 0 color = [0] * n c = 1 stk = list() for i in range(n): if i == 0 or b[i - 1][0] != b[i][0]: cnt = 1 else: cnt += 1 if cnt > k: continue else: color[b[i][1]] = (c - 1) % k + 1 c += 1 stk.append(i) c = (c - 1) % k + 1 siz = len(stk) for i in range(siz - (c - 1), siz): color[b[stk[i]][1]] = 0 for i in color: print(i, end=" ") print() for t in range(int(input())): charming()
IMPORT ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
R = lambda: map(int, input().split()) (t,) = R() while t: t -= 1 n, k = R() r = [0] * n a = [] p = 0 for x, i in sorted(zip(R(), range(n))): n *= x == p a += [i][: n < k] p = x n += 1 for i in a[len(a) % k :]: n = n % k + 1 r[i] = n print(*r)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def colors(N, K, arr): arr.sort() count = 1 curr = 1 req = [arr[0]] for i in range(1, N): if arr[i][0] == arr[i - 1][0]: curr += 1 if curr <= K: count += 1 req.append(arr[i]) else: curr = 1 count += 1 req.append(arr[i]) del arr color_count = [0] * (K + 1) color = [0] * N for i in range(len(req)): if (i + 1) % K == 0 and color_count[K] + 1 <= count // K: color[req[i][1]] = K color_count[K] += 1 elif color_count[(i + 1) % K] + 1 <= count // K: color[req[i][1]] = (i + 1) % K color_count[(i + 1) % K] += 1 return color t = int(input()) for _ in range(t): n, k = map(int, input().split()) arr = [[int(x), i] for i, x in enumerate(input().split())] print(*colors(n, k, arr))
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
R = lambda: map(int, input().split()) (t,) = R() while t: t -= 1 n, k = R() r = [0] * n a = [[] for _ in r] i = 0 for x in R(): a[x - 1] += (i,) i += 1 for j in (a := [i for x in a for i in x[:k]])[: len(a) // k * k]: i = i % k + 1 r[j] = i print(*r)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for i in range(t): n, k = list(map(int, input().strip().split())) a = list(map(int, input().strip().split())) freqarray = [(0) for i in range(n + 1)] ans = [(0) for i in range(n)] for x in a: freqarray[x] += 1 c = n for x in freqarray: if x > k: c = c - (x - k) c = (c - c % k) // k r = [[] for i in range(int(n + 1))] for i in range(n): r[a[i]].append(i) for x in r: colour = 1 l = len(x) if l >= k: for i in range(l): ans[x[i]] = colour if colour < k and colour > 0: colour += 1 elif colour == k: colour = 0 colour = 1 for x in r: l = len(x) if l > 0 and l < k: for i in range(l): ans[x[i]] = colour if colour < k: colour += 1 elif colour == k: colour = 1 colours = [(0) for i in range(k)] for x in ans: if x > 0 and colours[x - 1] < c: print(x, end=" ") colours[x - 1] += 1 else: print(0, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
for t in range(int(input())): n, k = map(int, input().split()) A = [int(i) for i in input().split()] c_count = n // k * k B, d, d1 = A[:], dict(), dict() for i in B: d[i] = d[i] + 1 if i in d else 1 for i in range(n): if B[i] in d1: d1[B[i]].append(i) else: d1[B[i]] = [i] B.sort(key=lambda x: (d[x], x), reverse=True) X, i, j, lst = [B[0]], 0, n, B[0] while j > 0: if B[i] != lst: X.append(B[i]) lst = B[i] i += 1 j -= 1 C = [] a = [int(i) for i in range(k, 0, -1)] for i in range(len(X)): if d[X[i]] > k: C += a rem = d[X[i]] - k while rem > 0: C.append(0) rem -= 1 else: cal = n - len(C) a1 = cal // k for i in range(a1): C += a rem2 = n - len(C) while rem2 > 0: C.append(0) rem2 -= 1 break b, k = 0, 0 for i in X: Y = d1[i] for j in Y: B[j] = C[k] k += 1 print(*B)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR LIST VAR NUMBER NUMBER VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys from sys import maxsize def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) def get_list_string(): return list(map(str, sys.stdin.readline().strip().split())) def get_string(): return sys.stdin.readline().strip() def get_int(): return int(sys.stdin.readline().strip()) def get_print_int(x): sys.stdout.write(str(x) + "\n") def get_print(x): sys.stdout.write(x + "\n") def get_print_int_same(x): sys.stdout.write(str(x) + " ") def get_print_same(x): sys.stdout.write(x + " ") def solve(): for _ in range(get_int()): n, k = get_ints() arr = get_list() d = dict() maxx = 0 for i in range(n): if arr[i] not in d: d[arr[i]] = [i] maxx += 1 elif len(d[arr[i]]) <= k - 1: d[arr[i]].append(i) maxx += 1 ans = [0] * n start = 1 mul = maxx // k upper = mul * k for key, v in d.items(): if upper == 0: break for ind in v: if upper == 0: break ans[ind] = start start += 1 upper -= 1 if start > k: start = 1 get_print(" ".join(map(str, ans))) solve()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def kth(ref_lis, c_list): c = 1 for i in c_list: ref_lis[i] = c c += 1 for test in range(int(input())): n, k = map(int, input().split()) st = list(input().split()) dic = {} for i in range(n): if dic.get(st[i], -1) == -1: dic[st[i]] = [i] elif len(dic[st[i]]) < k: dic[st[i]].append(i) ans = [0] * n nls = [] for itm in dic.values(): if len(itm) == k: kth(ans, itm) else: nls.extend(itm) p = len(nls) // k * k c = 1 for i in range(p): if i % k == 0: ans[nls[i]] = k else: ans[nls[i]] = i % k print(*ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) store = [(0) for i in range(n + 1)] stack = [[] for i in range(n + 1)] counted = [(0) for i in range(n + 1)] for i in range(n): if counted[arr[i]] < k: counted[arr[i]] += 1 stack[arr[i]].append(i) queue = [] for i in range(n + 1): for j in stack[i]: queue.append(j) l = len(queue) r = l // k * k i = 0 ans = [(0) for i in range(n)] for i in range(0, r, k): count = 0 for j in range(i, i + k): ans[queue[j]] = count + 1 count += 1 print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) while t > 0: t -= 1 a = input().split() n = int(a[0]) k = int(a[1]) a = input().split() a = [int(x) for x in a] li = [] ans = [] le = 0 for x in range(n): li.append([]) ans.append(0) for i in range(n): va = li[a[i] - 1] va.append(i) if len(va) <= k: le += 1 ki = k le = le // k * k for i in range(n): va = li[i] if len(va) >= k: for ii in range(k): ans[va[ii]] = ii + 1 li[i] = [] le -= k rk = 1 for i in range(n): va = li[i] if len(va) > 0: for xx in va: if le > 0: ans[xx] = rk rk = rk % k + 1 le -= 1 ans = [str(x) for x in ans] ans = " ".join(ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) arr = list(map(int, input().split())) di = {} for i in range(len(arr)): if arr[i] not in di: di[arr[i]] = [i] elif len(di[arr[i]]) < k: di[arr[i]].append(i) s = [] for ar in di.values(): for ind in ar: s.append(ind) r = len(s) // k * k ans = [0] * n for i in range(r): ans[s[i]] = i % k + 1 print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) sc = dict() for i in range(n): if a[i] in sc.keys(): sc[a[i]][0] += 1 sc[a[i]][1].append(i) else: sc[a[i]] = [1, [i]] ans = [0] * n it_min = 0 cnt_min = 0 for key in sc.keys(): if sc[key][0] < k: cnt_min += sc[key][0] cnt_min -= cnt_min % k for key in sc.keys(): if sc[key][0] >= k: it = 1 for i in range(k): ans[sc[key][1][i]] = it it += 1 elif it_min < cnt_min: for i in sc[key][1]: ans[i] = it_min % k + 1 it_min += 1 if it_min == cnt_min: break print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR LIST NUMBER LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR FOR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
from sys import stdin input = stdin.readline for _ in range(int(input())): n, k = list(map(int, input().split())) a = list(map(int, input().split())) b = [[]] b.clear() for i in range(len(a)): b.append([a[i], i]) b.sort() c = [(-1) for i in range(len(a))] mp = {} baki = n for i in range(n): if b[i][0] in mp.keys(): mp[b[i][0]] += 1 else: mp[b[i][0]] = 1 if mp[b[i][0]] > k: c[b[i][1]] = 0 baki -= 1 koyta = baki // k ct = 1 for i in range(n): if c[b[i][1]] != -1: continue c[b[i][1]] = ct ct += 1 if ct > k: ct = 1 koyta -= 1 if koyta <= 0: break for i in c: if i == -1: print(0) else: print(i)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys input = sys.stdin.readline t = int(input()) while t > 0: n, k = map(int, input().split()) a = list(map(int, input().split())) d = {} for i in range(n): if a[i] not in d: d[a[i]] = [] d[a[i]].append(i) ans = [0] * n temp = [] for x in d: for y in range(min(k, len(d[x]))): temp.append(d[x][y]) if len(temp) == k: for z in range(k): ans[temp[z]] = z + 1 temp = [] print(*ans) t -= 1
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) while t: t -= 1 n, k = map(int, input().split()) a = list(map(int, input().split())) di, li, pq = {}, [], [0] * (n + 1) for i in range(n): if di.get(a[i]): di[a[i]].append(i + 1) else: di[a[i]] = [i + 1] for i in list(di.values()): if len(i) <= k: li.extend(i) else: r = 1 for j in range(len(i)): pq[i[j]] = r r += 1 if r > k: break gi, o = len(li) // k, 0 if o < len(li) and gi > 0: while o < len(li) and gi > 0: q, val, y = o, k, 1 if val > 0: while q < len(li) and val > 0: pq[li[q]] = y y += 1 val -= 1 q += 1 o = q gi -= 1 print(*pq[1:])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR DICT LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys pl = 1 sys.setrecursionlimit(10**5) if pl: input = sys.stdin.readline else: sys.stdin = open("input.txt", "r") sys.stdout = open("outpt.txt", "w") def li(): return [int(xxx) for xxx in input().split()] def fi(): return int(input()) def si(): return list(input().rstrip()) def mi(): return map(int, input().split()) def ff(): sys.stdout.flush() def google(tc, *ans): print("Case #" + str(tc) + ":", *ans) t = fi() f = t def check(a): c = 0 for i in range(n): if a[i] != b[i]: c += 1 return c while t > 0: t -= 1 n, k = mi() a = li() d = {} f = [] for i in range(n): if a[i] not in d: d[a[i]] = [] d[a[i]].append(i) ans = [0] * n for i in d: if len(d[i]) >= k: for j in range(k): ans[d[i][j]] = j + 1 else: f += d[i] p = len(f) // k * k j = 1 for i in range(p): ans[f[i]] = j j += 1 if j > k: j = 1 print(*ans)
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = list(map(int, input().split())) a = [(0) for i in range(n + 1)] b = [(0) for i in range(n + 1)] e = [[] for i in range(n + 1)] c = 0 for i in range(n): a[s[i]] += 1 for i in range(1, n + 1): if a[i] >= k: b[i] = k else: b[i] += a[i] c += a[i] d = c % k for i in range(1, n + 1): if d == 0: break if b[i] != k: if b[i] > d: b[i] -= d break else: d -= b[i] b[i] = 0 x = 1 for i in range(1, n + 1): for j in range(b[i]): e[i].append(x) if x == k: x = 1 else: x += 1 for i in range(n): if b[s[i]] == 0: print(0, end=" ") else: print(e[s[i]][b[s[i]] - 1], end=" ") b[s[i]] -= 1 print()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
R = lambda: map(int, input().split()) (t,) = R() while t: t -= 1 n, k = R() d = {} r = [0] * n i = j = 0 for x in R(): l = d.setdefault(x, []) l += ([], [i])[len(l) < k] i += 1 for i in (a := [i for x in d for i in d[x]])[: len(a) // k * k]: j = j % k + 1 r[i] = j print(*r)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR LIST LIST VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys def process(n, k, a): counts = {} indices = {} for i in range(len(a)): if a[i] in counts: counts[a[i]] += 1 else: counts[a[i]] = 1 if a[i] in indices: indices[a[i]].append(i) else: indices[a[i]] = [i] counter = 0 to_sort = [] for i in counts: counter += min(counts[i], k) to_sort.append((i, min(counts[i], k))) to_sort.sort(key=lambda x: x[1], reverse=True) to_return = [0] * n paint_number = 0 paint_max = int(counter / k) * k for i, j in to_sort: painted = 0 while painted < j: if paint_number == paint_max: return to_return to_return[indices[i][painted]] = paint_number % k + 1 paint_number += 1 painted += 1 return to_return t = int(input()) for i in range(t): n, k = [int(x) for x in input().split()] a = [int(x) for x in input().split()] sys.stdout.write(" ".join(map(str, process(n, k, a))) + "\n")
IMPORT FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR STRING
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys input = lambda: sys.stdin.readline().rstrip() T = int(input()) for t in range(T): N, K = map(int, input().split()) arr = sorted([(v, i) for i, v in enumerate(map(int, input().split()))]) lasts = [-1] * len(arr) res = [0] * len(arr) d = dict() for v, i in arr: if v not in d: d[v] = 0 else: d[v] += 1 arr_overK = [] arr_underK = [] for v, i in arr: if d[v] >= K: arr_overK.append((v, i)) else: arr_underK.append((v, i)) last = -1 k = 0 for v, i in arr_overK: if v != last: k = 0 if k >= K: pass else: res[i] = k + 1 k += 1 last = v k = 0 for i in range(len(arr_underK) - len(arr_underK) % K): v, i = arr_underK[i] res[i] = k + 1 k += 1 k %= K print(*res)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) ar = list(map(int, input().split())) dic = {} li = [] count = 0 for i in range(n): if ar[i] in dic: if dic[ar[i]] < k: dic[ar[i]] += 1 li.append(i) count += 1 else: dic[ar[i]] = 1 count += 1 li.append(i) el = count // k ext = len(li) - el * k li = li[ext:] li.sort(key=lambda x: ar[x]) ans = [0] * n for i in range(len(li)): ans[li[i]] = i % k + 1 print(*ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for _ in range(t): n, k = list(map(int, input().rstrip().split())) arr = list(map(int, input().rstrip().split())) d = {} d2 = {} for i in range(n): if arr[i] in d.keys(): d[arr[i]] += 1 else: d[arr[i]] = 1 final = [-1] * n for i in range(n): if d[arr[i]] >= k: if arr[i] in d2.keys(): final[i] = d2[arr[i]] + 1 d2[arr[i]] += 1 else: final[i] = 1 d2[arr[i]] = 1 d = {} c = 0 for i in range(n): if final[i] == -1: c += 1 if arr[i] in d.keys(): d[arr[i]].append(i) else: d[arr[i]] = [i] e = 0 curr = 0 for i in d.keys(): if e == k * (c // k): break for j in d[i]: final[j] = curr + 1 curr += 1 curr = curr % k e += 1 if e == k * (c // k): break for i in range(n): if final[i] > k or final[i] == -1: final[i] = 0 print(*final)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def u(): return map(int, input().split()) for _ in range(int(input())): n, k = u() l = list(u()) lnew = [[l[i], i, 0] for i in range(n)] lnew = sorted(lnew, key=lambda x: x[0]) arr = [0] * (k + 1) cw = 1 m = dict() for el in lnew: if el[0] not in m: m[el[0]] = 0 if m[el[0]] >= k: pass else: el[2] = cw arr[cw] += 1 cw += 1 m[el[0]] += 1 if cw == k + 1: cw = 1 mark = 0 for i in range(1, k): if arr[i] != arr[i + 1]: mark = i break s = set() for i in range(1, mark + 1): s.add(i) for el in lnew: if el[2] in s: s.remove(el[2]) el[2] = 0 lnew = sorted(lnew, key=lambda x: x[1]) ans = list() for el in lnew: ans.append(el[2]) print(*ans)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
for i in range(int(input())): c, b = map(int, input().split()) a = list(map(int, input().split())) ind = [[] for i in range(c + 1)] p = {} num = 0 for i in range(c): if len(ind[a[i]]) < b: ind[a[i]].append(i) num += 1 m = int(num / b) s = m * b i = 0 f = False j = 0 p = ["0" for i in range(c)] for i in ind: if f == True: break for q in i: p[q] = str(j % b + 1) j += 1 if j == s: print(" ".join(p)) f = True break if f == False: print(" ".join(p))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for t1 in range(t): n, k = input().split(" ") n, k = int(n), int(k) s = input().split(" ") s = [int(x) for x in s] m = dict() for i in range(len(s)): if s[i] in m.keys(): m[s[i]] += 1 else: m[s[i]] = 1 d = dict() e = dict() count = 0 for ele in m.keys(): if m[ele] >= k: d[ele] = 1 else: e[ele] = 1 count += m[ele] flag = count // k s1 = [-1] * n for i in range(len(s)): if s[i] in d.keys(): if d[s[i]] > k: s1[i] = 0 else: d[s[i]] += 1 s1[i] = d[s[i]] - 1 else: pass e1 = dict() for i in range(len(s)): if s[i] in e.keys(): if s[i] in e1.keys(): e1[s[i]].append(i) else: e1[s[i]] = [i] counter = 1 f = 1 for i in e1.keys(): for j in e1[i]: if f <= flag: s1[j] = counter counter += 1 if counter == k + 1: counter = 1 f += 1 else: s1[j] = 0 for ele in s1: print(ele, end=" ") print("\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR IF VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
for i in range(int(input())): n, k = map(int, input().split()) x = list(map(int, input().split())) a, z = {}, 0 for i in range(n): if x[i] in a: if len(a[x[i]]) < k: a[x[i]].append(i) else: a[x[i]] = [i] b = [] for c in a.values(): for i in c: b.append(i) ans, kol = [0] * n, len(b) // k * k for i in range(kol): ans[b[i]] = i % k + 1 print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR DICT NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def func(n, k, a): m = [(0) for i in range(n + 1)] sm = [[] for i in range(n + 1)] res = [(0) for i in range(n)] for i in range(n): m[a[i]] += 1 sm[a[i]].append(i) extra = 0 c = 0 res = [(0) for i in range(n)] for i in range(n + 1): if m[i] >= k: extra += m[i] - k c += 1 for j in range(k): res[sm[i][j]] = j + 1 vvv = (n - c * k - extra) // k c = 0 x = 1 for i in range(n + 1): if m[i] < k: if c == vvv * k: return res for j in range(len(sm[i])): res[sm[i][j]] = x c += 1 x += 1 if x == k + 1: x = 1 if c == vvv * k: return res return res for q in range(int(input())): n, k = list(map(int, input().split())) a = list(map(int, input().split())) print(*func(n, k, a))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def solve(n, k, a): indices = [[] for i in range(n + 1)] colours = [0] * n cur_col = 0 max_painted = 0 for i in range(n): if len(indices[a[i]]) < k: indices[a[i]] += [i] max_painted += 1 max_painted = max_painted - max_painted % k for arr in indices: for x in arr: colours[x] = cur_col % k + 1 cur_col += 1 if cur_col >= max_painted: print(*colours) return print(*colours) for t in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) solve(n, k, a)
FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for i in range(t): n, k = map(int, input().split()) nums = list(map(int, input().split())) d = {} for i in nums: d[i] = d.get(i, 0) + 1 d = {x: k for x, y in d.items() if y >= k} lst = [i for i in nums if i not in d] lst.sort() dp = {i: [] for i in lst} temp = 0 for i in lst: dp[i].append(temp % k + 1) temp += 1 ans = nums.copy() for i in range(len(nums)): if nums[i] not in d: ans[i] = dp[nums[i]].pop() elif d[nums[i]] > 0: ans[i] = d[nums[i]] d[nums[i]] -= 1 else: ans[i] = 0 if temp % k != 0: s = {i for i in range(1, temp % k + 1)} for i in range(len(ans)): if ans[i] in s: s.remove(ans[i]) ans[i] = 0 print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
for _ in range(int(input())): n, k = map(int, input().split()) cnt = [0] * (n + 1) a = [] for i, x in enumerate(map(int, input().split())): if cnt[x] < k: a.append((x, i)) cnt[x] += 1 a.sort() ans = [0] * n color = 0 ra = len(a) - len(a) % k for v, i in a[:ra]: color = color % k + 1 ans[i] = color print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for ii in range(t): n, k = map(int, input().split()) s = list(map(int, input().split())) freq = {} for i in s: if i in freq: freq[i] += 1 else: freq[i] = 1 for i in freq: if freq[i] > k: freq[i] = k reversemap = {} colors = [0] * n for i in freq: val = freq[i] if val in reversemap: reversemap[val].append(i) else: reversemap[val] = [i] sm = 0 for i in reversemap: if i != k: sm += i * len(reversemap[i]) sm = sm - sm % k groups = sm // k index_map = {} cnt = 0 for i in s: if i in index_map: index_map[i].append(cnt) else: index_map[i] = [cnt] cnt += 1 if k in reversemap: for pos in reversemap[k]: cnt = 1 for tag in index_map[pos][:k]: colors[tag] = cnt cnt += 1 cnt = 1 total = groups * k done = False if total > 0: for i in reversemap: if done: break if i < k: for val in reversemap[i]: if done: break for j in index_map[val]: colors[j] = cnt cnt += 1 total -= 1 if cnt == k + 1: cnt = 1 if total == 0: done = True break print(*colors)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER IF VAR VAR FOR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR IF VAR IF VAR VAR FOR VAR VAR VAR IF VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) index = {} for i in range(n): if arr[i] in index: index[arr[i]].append(i) else: index[arr[i]] = [i] brr = list(index.values()) group, temp = [], [] for i in range(len(brr)): if len(brr[i]) < k: temp += brr[i] if len(temp) >= k: group.append(temp[:k]) temp = temp[k:] else: group.append(brr[i]) ans = [0] * n for num in group: for i in range(k): ans[num[i]] = i + 1 for num in ans: print(num, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
T = int(input()) for t in range(T): n, k = map(int, input().split()) A = [int(x) for x in input().split()] M = {} for i, a in enumerate(A): if a not in M: M[a] = [] if len(M[a]) < k: M[a].append(i) m = 0 m = sum([len(M[e]) for e in M]) m -= m % k color = 0 sol = [str(0)] * n for e in M: for i in M[e]: color += 1 sol[i] = str(color) color %= k m -= 1 if m == 0: break if m == 0: break print(" ".join(sol))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR NUMBER VAR FOR VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
from sys import stdin, stdout final_ans = "" for tests in range(int(stdin.readline())): n, k = map(int, stdin.readline().split()) ls = list(map(int, stdin.readline().split())) ind_ls = [[] for i in range(n + 1)] cnt_ls = [0] * (n + 1) for i in range(n): ind_ls[ls[i]].append(i) cnt_ls[ls[i]] += 1 arr = [] for i in range(n + 1): if cnt_ls[i] > 0: arr.append([cnt_ls[i], i]) arr.sort() ans = [0] * n cnt = 1 n1 = len(arr) left_num = n for i in range(n1 - 1, -1, -1): if arr[i][0] >= k: for j in range(k): ans[ind_ls[arr[i][1]][j]] = j + 1 left_num -= arr[i][0] else: for j in range(arr[i][0]): if left_num >= k - cnt + 1: ans[ind_ls[arr[i][1]][j]] = cnt cnt += 1 left_num -= 1 if cnt == k + 1: cnt -= k else: break for i in ans: final_ans += str(i) + " " final_ans += "\n" stdout.write(final_ans)
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
R = lambda: map(int, input().split()) p = print t = 1 (t,) = R() alpali = list(map(chr, range(97, 123))) def solve(): n, k = R() li = list(enumerate(list(R()))) li.sort(key=lambda x: x[1]) di = {} lia = [0] * len(li) cnt = 1 for i in range(len(li)): if di.get(li[i][1], 0) < k: lia[li[i][0]] = cnt di[li[i][1]] = di.get(li[i][1], 0) + 1 cnt += 1 if cnt == k + 1: cnt = 1 if i == len(li) - 1: j = i while j >= 0 and lia[li[j][0]] != k: lia[li[j][0]] = 0 j -= 1 for a in lia: p(a, end=" ") p() while t: t -= 1 solve()
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def magika(nums, cores): dic = {} temp = [] for i in range(len(nums)): if nums[i] in dic: dic[nums[i]].append(i) else: dic[nums[i]] = [i] ans = [0] * len(nums) for k in dic.keys(): if len(dic[k]) >= cores: for j in range(cores): ans[dic[k][j]] = j + 1 elif len(dic[k]) >= 1: for x in range(len(dic[k])): temp.append(dic[k][x]) count = len(temp) // cores p = 1 maxi = 0 for y in range(len(temp)): if maxi == count: break ans[temp[y]] = p if p == cores: maxi += 1 p += 1 if p == cores + 1: p = 1 return ans n = int(input()) r = [0] * n for i in range(n): elementos, cores = map(int, input().split()) nums = list(map(int, input().split())) r[i] = magika(nums, cores) for c in r: for j in range(len(c)): if j == len(c) - 1: print(c[j]) else: print(c[j], end=" ")
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys I = lambda x=" ": map(int, sys.stdin.readline().split(x)) pr = lambda *x, sep=" ", end="\n": sys.stdout.write(f"{sep.join(map(str, x))}{end}") def solve(): n, k = I() a = [*I()] d = dict() for i, x in enumerate(a): if x in d: if len(d[x]) < k: d[x].append(i) else: d[x] = [i] m = sum(map(lambda x: len(x[1]), d.items())) m -= m % k ans, c = [0] * n, 0 for e in d: for i in d[e]: ans[i] = c + 1 c = (c + 1) % k m -= 1 if not m: return ans def main(): (t,) = I() for _ in range(t): pr(*solve()) main()
IMPORT ASSIGN VAR STRING FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR STRING STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR NUMBER FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def solve(): n, k = map(int, input().split()) cnt = [0] * (n + 1) a = [] for i, x in enumerate(map(int, input().split())): if cnt[x] < k: a.append((x, i)) cnt[x] += 1 a.sort() cc = 0 color = [0] * n m = len(a) - len(a) % k for x, i in a[:m]: color[i] = cc + 1 cc = (cc + 1) % k print(*color) t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) d = {} for i in range(n): if l[i] in d: d[l[i]] += 1 else: d[l[i]] = 1 if d[l[i]] > k: d[l[i]] = k dc = {i: (0) for i in range(k)} ans = {} p = 0 for i in d: ans[i] = [] for j in range(d[i]): ans[i].append(p) dc[p] += 1 p += 1 if p == k: p = 0 m = min(list(dc.values())) res = [(-1) for i in range(n)] for i in range(n): if l[i] in ans and len(ans[l[i]]) > 0: temp = ans[l[i]].pop() if dc[temp] > m: dc[temp] -= 1 else: res[i] = temp res = [(i + 1) for i in res] print(*res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) final_ans = [0] * n d = {} for i in range(n): if d.get(a[i]) is None: d[a[i]] = [1, i] else: d[a[i]][0] += 1 d[a[i]].append(i) h = [] for key, v in d.items(): if v[0] >= k: for i in range(1, k + 1): final_ans[v[i]] = i else: for i in range(1, v[0] + 1): h.append(v[i]) count = 0 l = len(h) // k * k for i in range(l): final_ans[h[i]] = count % k + 1 count = count + 1 print(*final_ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR LIST NUMBER VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def wonderfulColoring(n, colors, array): d = {} ans = {} for x in array: if x in d: d[x] += 1 else: d[x] = 1 ans[x] = [] for x in d: if d[x] > colors: d[x] = colors s = 0 for x in d: s += d[x] r = s % colors for x in d: if d[x] > r: d[x] -= r r = 0 break else: r -= d[x] d[x] = 0 if r == 0: break c = 1 for x in d: i = 0 while i < d[x]: i += 1 ans[x].append(c) c += 1 c = c % k if c == 0: c = colors res = [] for x in range(n): if not ans[array[x]]: res.append(0) else: p = ans[array[x]].pop() res.append(p) return res t = int(input()) for _ in range(t): n, k = map(int, input().split()) l = list(map(int, input().split())) ans = wonderfulColoring(n, k, l) for x in ans: print(x, end=" ") print()
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def solve(): n, k = map(int, input().split()) cnt = [] for _ in range(n): cnt.append([]) ind = 0 for i in input().split(): i = int(i) cnt[i - 1].append(ind) ind += 1 now = 1 new_a = [0] * n cnt_colors = [0] * k for i in range(n): for x in range(len(cnt[i])): if x == k: break cur = cnt[i][x] new_a[cur] = now cnt_colors[now - 1] += 1 now += 1 if now > k: now = 1 need = min(cnt_colors) for i in range(n): if new_a[i] > 0: if cnt_colors[new_a[i] - 1] > need: cnt_colors[new_a[i] - 1] -= 1 new_a[i] = 0 print(*new_a) t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
for nt in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) b = [[] for i in range(n)] count = 0 for i in range(n): if len(b[a[i] - 1]) < k: b[a[i] - 1].append(i) count += 1 total = count - count % k ans = [0] * n curr = 1 count = 0 flag = False for i in b: for j in i: ans[j] = curr curr += 1 if curr > k: curr = 1 count += 1 if count == total: flag = True break if flag: break print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
tt = int(input()) for _ in range(tt): n, k = map(int, input().split()) m = {} seq = list(map(int, input().split())) for c in seq: if c in m: m[c] += 1 else: m[c] = 1 p = 0 base = {} shift = {} for c in m: if m[c] >= k: base[c] = 0 shift[c] = 0 else: shift[c] = 0 base[c] = p p += m[c] p_limit = p - p % k for c in seq: if shift[c] < k and (m[c] >= k or shift[c] + base[c] < p_limit): print((shift[c] + base[c]) % k + 1, end=" ") shift[c] += 1 else: print(0, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for _ in range(t): n, k = list(map(int, input().split())) ls = list(map(int, input().split())) memo = {} for i in range(n): try: memo[ls[i]].append(i) except KeyError: memo[ls[i]] = [i] count = 0 for key, value in memo.items(): if len(value) > k: count += k memo[key] = value[:k] else: count += len(value) count = count // k colors = [count for i in range(k)] c = 0 ls = [(0) for i in range(n)] positions = list(memo.values()) positions.sort(key=len, reverse=True) for lst in positions: for position in lst: if colors[c]: ls[position] = c + 1 colors[c] -= 1 c += 1 if c == k: c = 0 else: break for color in ls: print(color, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
NUM = int(input()) for T in range(0, NUM): n, k = map(int, input().split()) vallist = list(map(int, input().split())) answer = [0] * n countnum = [[] for i in range(0, n)] for i in range(0, n): countnum[vallist[i] - 1].append(i) ans = 0 for i in range(0, n): ans += min(k, len(countnum[i])) ans //= k current = 0 for i in countnum: for j in range(0, min(k, len(i))): if ans: answer[i[j]] = current + 1 current = (current + 1) % k ans -= current == 0 for i in answer: print(i, end=" ") print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for test in range(t): n, k = map(int, input().split()) l = list(map(int, input().split())) l2 = {} l3 = [] ans = [] d = {} l4 = {} c = 0 for i in range(n): if l[i] not in d.keys(): d[l[i]] = 1 else: d[l[i]] += 1 for key in d: if d[key] >= k: l2[key] = k else: l4[key] = [] c += d[key] l3.extend([key] * d[key]) if c < k: for i in range(n): if l[i] in l2: if l2[l[i]] == 0: print(0, end=" ") else: print(l2[l[i]], end=" ") l2[l[i]] -= 1 else: print(0, end=" ") print() continue else: ind = 1 l3.sort() for j in range(c - c % k): l4[l3[-1]].append(ind) l3.pop() ind += 1 if ind > k: ind = 1 i = 0 for i in range(n): if l[i] in l2: if l2[l[i]] == 0: print(0, end=" ") else: print(l2[l[i]], end=" ") l2[l[i]] -= 1 elif len(l4[l[i]]) == 0: print(0, end=" ") else: print(l4[l[i]].pop(), end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys def read(): line = sys.stdin.readline().rstrip() if " " in line: return map(int, line.split()) return line def solver(n, k, s): count = {} remain = [] colors = range(1, k + 1) for i, j in enumerate(s): lst = count.setdefault(j, []) lst.append(i) if len(lst) == k: for a in range(1, k + 1): ans[lst[a - 1]] = a for value in count.values(): if len(value) < k: remain.extend(value) for i in range(len(remain) // k): for j in range(1, k + 1): ans[remain[i * k + j - 1]] = j T = int(read()) for t in range(T): N, K = read() S = read() ans = [0] * N solver(N, K, S) print(" ".join(map(str, ans))) sys.exit()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF STRING VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for u in range(t): n, k = map(int, input().split()) b = [int(p) for p in input().split()] a = [0] * n m = {} x = 0 for i in range(n): if m.get(b[i]): if len(m[b[i]]) < k: m[b[i]].append(i) x += 1 else: m[b[i]] = [i] x += 1 x -= x % k c = 0 for i in m: for j in m[i]: c += 1 a[j] = c c %= k x -= 1 if x == 0: break if x == 0: break print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys input = lambda: sys.stdin.readline() int_arr = lambda: list(map(int, input().split())) str_arr = lambda: list(map(str, input().split())) get_str = lambda: map(str, input().split()) get_int = lambda: map(int, input().split()) get_flo = lambda: map(float, input().split()) mod = 1000000007 def solve(n, k, a): ans = [0] * n d = {} for i in range(n): if a[i] not in d: d[a[i]] = [] d[a[i]].append(i) tmp = [] for c in d.keys(): for i in range(min(len(d[c]), k)): tmp.append(d[c][i]) if len(tmp) == k: for j in range(k): ans[tmp[j]] = j + 1 tmp = [] print(*ans) for _ in range(int(input())): n, k = get_int() arr = int_arr() solve(n, k, arr)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) d = [[] for z in range(n + 1)] ans = [(0) for z in range(n)] for z in range(n): d[a[z]].append(z) c = 0 temp = [] for z in range(1, n + 1): num = 1 for y in d[z]: temp.append(y) num += 1 c += 1 ans[y] = c c = c % k l = len(temp) if k == l: temp.clear() if num > k: break l = len(temp) for z in range(l): ans[temp[z]] = 0 print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
from sys import stdin, stdout input = stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = [int(x) for x in input().split()] d = {} c = -1 for i in a: c += 1 if i not in d: d[i] = [] if len(d[i]) < k: d[i].append(c) ans = [(0) for i in range(n)] vals = sorted(d.keys(), key=lambda x: len(d[x]), reverse=True) kk = 0 make_zero = [] for i in vals: for j in d[i]: kk += 1 if kk == k + 1: make_zero = [] kk = 1 ans[j] = kk make_zero.append(j) assert kk == len(make_zero) if kk != k: for i in make_zero: ans[i] = 0 print(*ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) arr = list(map(int, input().split())) idx = [[] for i in range(n + 1)] col = [(0) for i in range(n)] for i in range(n): idx[arr[i]].append(i) less_k_idx = [] for id in idx: if len(id) >= k: for i in range(k): col[id[i]] = i + 1 elif len(id) > 0: less_k_idx += id z = len(less_k_idx) // k * k for i in range(z): col[less_k_idx[i]] = i % k + 1 print(*col)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
_ = int(input()) for __ in range(_): n, k = map(int, input().split()) arr = list(map(int, input().split())) out = [0] * n d = {} d_bigger = {} d_pos = {} curr_col = 1 for x in range(0, n): if arr[x] not in d: d[arr[x]] = 1 d_pos[arr[x]] = [x] else: d[arr[x]] += 1 d_pos[arr[x]].append(x) tot_num = 0 for x in d: if d[x] < k: tot_num += d[x] m = tot_num // k tot_num = m * k for x in d_pos: if d[x] >= k: pos = d_pos[x] for y in range(k): out[pos[y]] = y + 1 else: pos = d_pos[x] for y in pos: if tot_num: out[y] = curr_col curr_col += 1 if curr_col > k: curr_col = 1 tot_num -= 1 else: out[y] = 0 print(" ".join([str(x) for x in out]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) s = list(input().split()) count = {} ans = [0] * n tempb = 0 for i in s: if i not in count: count[i] = 0 count[i] += 1 temp = {} for i, j in count.items(): if j >= k: temp[i] = 1 else: tempb += j cur = 0 for i in sorted([(j, i) for i, j in enumerate(s)]): if i[0] in temp: if temp[i[0]] <= k: ans[i[1]] = temp[i[0]] temp[i[0]] += 1 elif tempb // k * k > cur: ans[i[1]] = cur % k + 1 cur += 1 print(*ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
for _ in range(int(input())): n, k = list(map(int, input().split())) arr = list(map(int, input().split())) mapper = {} for i in range(n): if arr[i] in mapper: mapper[arr[i]].append(i) else: mapper[arr[i]] = [i] ans = [(0) for i in range(n)] rem = [] for i in mapper: l = len(mapper[i]) if l >= k: for j in range(k): ans[mapper[i][j]] = j + 1 else: rem += mapper[i] l = len(rem) f = l // k for j in range(f * k): ans[rem[j]] = j % k + 1 print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys cin = int(sys.stdin.readline().strip()) for testcase in range(cin): cin1 = list(map(int, sys.stdin.readline().strip().split())) cin2 = list(map(int, sys.stdin.readline().strip().split())) n, k = cin2, cin1[-1] d = {} for i in range(len(n)): if n[i] not in d: d[n[i]] = [i] else: d[n[i]].append(i) ans = [(0) for i in range(len(n))] tt = [] for i in d: if len(d[i]) >= k: for j in range(k): ans[d[i][j]] = j + 1 else: for j in d[i]: tt.append(j) for j in range(len(tt) // k * k): ans[tt[j]] = j % k + 1 for i in range(len(ans)): ans[i] = str(ans[i]) print(" ".join(ans))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) cnt = [[] for i in range(n)] num = 0 a = list(map(int, input().split())) for i in range(len(a)): if len(cnt[a[i] - 1]) < k: cnt[a[i] - 1].append(i) num += 1 group = num // k group_end = k * group colors = [(0) for i in range(n)] d = 0 n = 0 for i in cnt: if n == group_end: break for j in i: colors[j] = d + 1 d = (d + 1) % k n += 1 if n == group_end: break print(*colors)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
import sys from itertools import chain input = lambda: sys.stdin.readline().rstrip("\r\n") t = int(input()) for i in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) el_idx = {} for i, ai in enumerate(a): el_ai = el_idx.setdefault(ai, []) if len(el_ai) < k: el_ai.append(i) cnt = {el: len(el_idx[el]) for el in el_idx.keys()} pos_el = sum(cnt.values()) pos_col = pos_el // k res = ["0" for _ in range(n)] col_iter = chain(*[range(1, k + 1) for _ in range(pos_col)]) idx_iter = chain(*el_idx.values()) for idx, col in zip(idx_iter, col_iter): res[idx] = str(col) print(" ".join(res))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
def find_colors(arr, k): answer = [(0) for i in range(arr.__len__())] ocr = dict() for i in range(arr.__len__()): if arr[i] in ocr: ocr[arr[i]].append(i) else: ocr[arr[i]] = [i] ocrk = dict() ocr_ = dict() s = 0 for i in ocr: if ocr[i].__len__() >= k: ocrk[i] = ocr[i] else: ocr_[i] = ocr[i] s += ocr_[i].__len__() for i in ocrk: paint = k for j in range(k, 0, -1): index = ocrk[i][k - j] answer[index] = j index_arr = [] for i in ocr_: index_arr += ocr_[i] for i in range(s - s % k): index = index_arr[i] answer[index] = k - i % k return answer def main(): answers = [] n = int(input()) for i in range(n): l, k = map(int, input().split()) arr = input().split() answers.append(find_colors(arr, k)) for i in answers: print(*i) main()
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. They want to paint it using pieces of chalk of $k$ colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of $k$ colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of $k$ colors the number of elements painted in the color β€” all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$ and $k=3$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10000$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) β€” the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where $c_i=0$, if $i$-th element isn't painted; $c_i>0$, if $i$-th element is painted in the $c_i$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. -----Examples----- Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 -----Note----- In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color β€” $2$, the green β€” $3$.
T = int(input()) t = 0 while t < T: N, K = map(int, input().split()) L = list(map(int, input().split())) C = [(0) for n in range(N)] P = [(0) for n in range(N + 1)] S = [] for n in range(N): P[L[n]] += 1 if P[L[n]] <= K: S.append([L[n], n]) bianchi = 0 for n in range(N): if P[n] > K: bianchi = bianchi + P[n] - K M = N - bianchi M = len(S) M = M // K * K H = S[:M] S.sort() a0 = S[0][0] p = S[0][1] C[p] = 1 k = min(2, K) col = 1 tot = 1 for n in range(1, M): a = S[n][0] p = S[n][1] if a == a0: if tot < K: C[p] = k col += 1 tot = tot + 1 k = k + 1 if k == K + 1: k = 1 else: tot = 1 C[p] = k col += 1 k = k + 1 if k == K + 1: k = 1 a0 = a t += 1 print(*C)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [0] * n for i in range(n): c[i] = abs(a[i] - b[i]) c = sorted(c, reverse=True) k = k1 + k2 while k > 0: c[0] -= 1 if c[0] < 0: c[0] = 1 if n != 1 and c[0] < c[1]: c = sorted(c, reverse=True) k -= 1 d = [(i * i) for i in c] print(sum(d))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) d = [abs(a[i] - b[i]) for i in range(n)] d = sorted(d)[::-1] k = k1 + k2 while k > 0: if d[0] == 0: break if k > 0: d[0] -= 1 k -= 1 d = sorted(d)[::-1] print(sum([(i**2) for i in d]) + k % 2)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, m, k = [int(i) for i in input().split()] k = k + m a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] c = [abs(a[i] - b[i]) for i in range(n)] while k != 0: c.sort() c[-1] = abs(c[-1] - 1) k -= 1 t = 0 for i in range(n): t += c[i] * c[i] print(t)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = [int(x) for x in input().split()[:3]] ks1 = [int(x) for x in input().split()[:n]] ks2 = [int(x) for x in input().split()[:n]] err = [abs(ks1[i] - ks2[i]) for i in range(n)] num = k1 + k2 rest1 = False if num % 2 != sum(err) % 2: rest1 = True def kill(): global num maxx = max(err) if maxx == 0 or num == 0: return False for i in range(len(err)): if err[i] == maxx: err[i] -= 1 num -= 1 if num == 0: return False return True while kill(): pass summ = sum([(x**2) for x in err]) if summ == 0 and rest1: print(1) else: print(summ)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER WHILE FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
def main(): n, k1, k2 = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = [abs(a[i] - b[i]) for i in range(n)] s = sum(c) if s >= k1 + k2: op = k1 + k2 while op > 0: c = sorted(c) c[-1] -= 1 op -= 1 ans = sum([(x**2) for x in c]) else: after = k1 + k2 - s if after % 2 == 0: ans = 0 else: ans = 1 print(ans) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k, l = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] lst = [abs(x - y) for x, y in zip(a, b)] for _ in range(k + l): lst.sort() lst[-1] = abs(lst[-1] - 1) print(sum(elem * elem for elem in lst))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
import sys input = lambda: sys.stdin.readline().strip("\r\n") n, k1, k2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [abs(a[i] - b[i]) for i in range(n)] k = k1 + k2 while k > 0: c.sort() c[-1] = abs(c[-1] - 1) k -= 1 print(sum(i**2 for i in c))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
inp = lambda: map(int, input().split()) n, k1, k2 = inp() a = [abs(x - y) for x, y in zip(list(inp()), list(inp()))] for i in range(k1 + k2): id = a.index(max(a)) a[id] = abs(a[id] - 1) print(sum(i**2 for i in a))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = map(int, input().split(" ")) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) l = [] k = k1 + k2 s = 0 c = 0 ans = 0 for i in range(0, n): s += abs(a[i] - b[i]) l.append(abs(a[i] - b[i])) if s <= k: if (k - s) % 2 == 0: ans = 0 else: ans = 1 else: l.sort() l.reverse() l += [0] i = 0 while c < k: if l[i] == l[i + 1]: i += 1 for j in range(0, i + 1): if c < k and l[j] != l[i + 1]: l[j] -= 1 c += 1 for i in range(0, n): ans += l[i] * l[i] print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
nrazmer, k1operationsA, k2operationsB = map(int, input().split()) myListA = list(map(int, input().split())) myListB = list(map(int, input().split())) diffList = [] for i in range(0, nrazmer): a = myListA[i] b = myListB[i] diff = abs(a - b) diffList.append(diff) totalOps = k1operationsA + k2operationsB diffList.sort() diffList.reverse() while totalOps > 0 and diffList[0] > 0: diffList[0] -= 1 diffList.sort() diffList.reverse() totalOps -= 1 if diffList[0] == 0 and totalOps > 0: if totalOps % 2 == 1: diffList[0] = 1 else: diffList[0] = 0 errorSum = 0 for j in range(0, len(diffList)): errorSum += diffList[j] * diffList[j] print(errorSum)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) difference = [] k = k1 + k2 for i in range(n): difference.append(abs(a[i] - b[i])) while k != 0: if max(difference) > 0: difference[difference.index(max(difference))] -= 1 k -= 1 else: difference[difference.index(max(difference))] += 1 k -= 1 for i in range(n): difference[i] **= 2 print(sum(difference))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = list(map(int, input().split())) ai = list(map(int, input().split())) bi = list(map(int, input().split())) ci = [0] * n k = k1 + k2 for i in range(n): ci[i] = abs(ai[i] - bi[i]) ci.sort() ci2 = [0] * n ci3 = [0] * n ci2[0] = ci[0] ci3[0] = 1 j = 0 for i in range(1, n): if ci[i] == ci[i - 1]: ci3[j] += 1 else: j += 1 ci2[j] = ci[i] ci3[j] = 1 temp2 = -1 temp3 = 0 for i in range(j, 0, -1): if k >= ci3[i] * (ci2[i] - ci2[i - 1]): k -= ci3[i] * (ci2[i] - ci2[i - 1]) ci3[i - 1] += ci3[i] ci3[i] = 0 else: temp = k // ci3[i] temp2 = k % ci3[i] temp3 = i ci2[i] -= temp break if temp2 != -1: ans = 0 for i in range(temp3): ans += ci3[i] * ci2[i] ** 2 print(ans + (ci3[temp3] - temp2) * ci2[temp3] ** 2 + temp2 * (ci2[temp3] - 1) ** 2) elif k >= ci3[0] * ci2[0]: k -= ci3[0] * ci2[0] print(k % 2) else: temp = k // ci3[0] temp2 = k % ci3[0] print((ci3[0] - temp2) * (ci2[0] - temp) ** 2 + temp2 * (ci2[0] - temp - 1) ** 2)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
str1 = input() str2 = input() str3 = input() a1 = str1.split() a2 = str2.split() a3 = str3.split() fr = [(int(a3[i]) - int(a2[i])) for i in range(int(a1[0]))] fr1 = [(i * i) for i in fr] def dd(n1): pp = n1 ** (1 / 2) pp = pp - 1 return pp**2 for i in range(int(a1[1]) + int(a1[2])): ap = fr1.index(max(fr1)) fr1[ap] = dd(fr1[ap]) print(int(sum(fr1)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) arr = [] for i in range(n): arr.append(abs(a[i] - b[i])) arr = sorted(arr, reverse=True) for i in range(k1 + k2): arr[0] = abs(arr[0] - 1) j = 0 while j < n - 1 and arr[j] < arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] j += 1 ans = 0 for i in range(n): ans = ans + arr[i] * arr[i] print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = map(int, input().split()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] da = [] for i in range(len(A)): da.append(abs(A[i] - B[i])) da.sort(reverse=1) op = k1 + k2 while op: if all(x == 0 for x in da): if op % 2 == 0: print(0) exit() else: print(1) exit() da[da.index(max(da))] -= 1 op -= 1 print(sum(x**2 for x in da))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) e = [] for ai, bi in zip(a, b): e.append(pow(ai - bi, 2)) for i in range(k1): pos = e.index(max(e)) if a[pos] <= b[pos]: a[pos] += 1 else: a[pos] -= 1 e[pos] = pow(a[pos] - b[pos], 2) for i in range(k2): pos = e.index(max(e)) if b[pos] <= a[pos]: b[pos] += 1 else: b[pos] -= 1 e[pos] = pow(a[pos] - b[pos], 2) print(sum(e))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] c = sorted([abs(a[i] - b[i]) for i in range(n)])[::-1] k = k1 + k2 cur = 0 for i in range(k): if c[0] == 0: break cur += 1 c[0] -= 1 j = 0 while j + 1 < n and c[j] < c[j + 1]: c[j], c[j + 1] = c[j + 1], c[j] j += 1 k -= cur if k > 0: print(k % 2) else: ans = 0 for i in c: ans += i**2 print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k_1, k_2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) differences = sorted( [abs(a_i - b_i) for a_i, b_i in zip(a, b) if a_i != b_i], reverse=True ) k = k_1 + k_2 while k > 0 and differences: differences[0] -= 1 k -= 1 if differences[0] == 0: del differences[0] for i in range(len(differences) - 1): if differences[i] <= differences[i + 1]: differences[i], differences[i + 1] = differences[i + 1], differences[i] if k == 0: print(sum([(s**2) for s in differences])) else: print(k % 2)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] d = [abs(a[i] - b[i]) for i in range(n)] for i in range(k1): ind = d.index(max(d)) d[ind] = abs(d[ind] - 1) for i in range(k2): ind = d.index(max(d)) d[ind] = abs(d[ind] - 1) s = 0 for i in range(n): s += d[i] ** 2 print(s)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) d = [abs(a[i] - b[i]) for i in range(n)] k = k1 + k2 for _ in range(k): mx = max(d) id = d.index(mx) if mx == 0: d[id] += 1 else: d[id] -= 1 print(sum([(x**2) for x in d]))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
[n, k1, k2] = [int(x) for x in input().split()] k = k1 + k2 a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] error = [abs(x[0] - x[1]) for x in zip(a, b)] error = sorted(error, reverse=True) if k >= sum(error): print((k - sum(error)) % 2) exit() index = 0 while k != 0: error[index] -= 1 if index + 1 == n: index = 0 elif error[index] + 1 == error[index + 1]: index += 1 else: index = 0 k -= 1 print(sum([(x * x) for x in error]))
ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) r = {} for i in range(n): r[i] = (a[i] - b[i]) ** 2 for i in range(k1): q = max(r, key=r.get) a[q] = a[q] - 1 if a[q] > b[q] else a[q] + 1 r[q] = (a[q] - b[q]) ** 2 for i in range(k2): q = max(r, key=r.get) b[q] = b[q] - 1 if b[q] > a[q] else b[q] + 1 r[q] = (a[q] - b[q]) ** 2 r = 0 for i in range(n): r += (a[i] - b[i]) ** 2 print(r)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = list(map(int, input().split(" "))) A = list(map(int, input().split(" "))) B = list(map(int, input().split(" "))) cola_prioridad = [] i = 0 while i < n: cola_prioridad.append(((A[i] - B[i]) * (A[i] - B[i]), i)) i += 1 cola_prioridad.sort() for x in range(0, k1): maxE, i = cola_prioridad[n - 1] if A[i] > B[i]: A[i] -= 1 else: A[i] += 1 cola_prioridad[n - 1] = (A[i] - B[i]) * (A[i] - B[i]), i cola_prioridad.sort() for x in range(0, k2): maxE, i = cola_prioridad[n - 1] if B[i] > A[i]: B[i] -= 1 else: B[i] += 1 cola_prioridad[n - 1] = (A[i] - B[i]) * (A[i] - B[i]), i cola_prioridad.sort() sumaFinal = 0 for x in cola_prioridad: sumaFinal += x[0] print(sumaFinal)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) diff = [abs(a[i] - b[i]) for i in range(n)] moves = k1 + k2 diff.sort() while moves > 0 and diff != [0] * n: diff[-1] -= 1 moves -= 1 diff.sort() if moves: if moves % 2 == 0: print(0) else: print(1) else: ans = 0 for i in range(n): ans += diff[i] ** 2 print(ans) print()
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR BIN_OP LIST NUMBER VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
def decrease_max(C, D): mx = -1, -1 for i, e in enumerate(abs(c - d) for c, d in zip(C, D)): if e > mx[1]: mx = i, e i = mx[0] if C[i] > D[i]: C[i] -= 1 else: C[i] += 1 def solution(k1, k2, A, B): for _ in range(k1): decrease_max(A, B) for _ in range(k2): decrease_max(B, A) return sum((a - b) ** 2 for a, b in zip(A, B)) n, k1, k2 = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) print(solution(k1, k2, A, B), end="")
FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR STRING
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = map(int, input().split()) A = map(int, input().split()) B = map(int, input().split()) C = [(x - y) for x, y in zip(B, A)] k = k1 + k2 for i in range(k): if min(C) >= 0: C[C.index(max(C))] -= 1 elif abs(max(C)) <= abs(min(C)): C[C.index(min(C))] += 1 else: C[C.index(max(C))] -= 1 ans = 0 for i in C: ans += i**2 print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] left = 0 for it in range(k1 + k2): if a == b: left = k1 + k2 - it break j, vl = -1, -1 for i in range(n): if abs(a[i] - b[i]) > vl: vl = abs(a[i] - b[i]) j = i if a[j] > b[j]: a[j] -= 1 elif a[j] < b[j]: a[j] += 1 sm = 0 for i in range(n): sm += (a[i] - b[i]) ** 2 sm += left % 2 print(sm)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) def just_relax(): global gruz mam = 0 ind = -1 for i in range(n): if abs(A[i] - B[i]) > mam: mam = abs(A[i] - B[i]) ind = i if ind != -1: if A[ind] < B[ind]: A[ind] += 1 else: A[ind] -= 1 else: gruz = 1 gruz = 0 ans = 0 for iq in range(k1 + k2): just_relax() if gruz: if (k1 + k2 - iq) % 2 == 0: print(0) else: print(1) exit(0) for i in range(n): ans += abs(A[i] - B[i]) ** 2 print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed. -----Input----- The first line contains three space-separated integers n (1 ≀ n ≀ 10^3), k_1 and k_2 (0 ≀ k_1 + k_2 ≀ 10^3, k_1 and k_2 are non-negative) β€” size of arrays and number of operations to perform on A and B respectively. Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≀ a_{i} ≀ 10^6) β€” array A. Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≀ b_{i} ≀ 10^6)β€” array B. -----Output----- Output a single integer β€” the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B. -----Examples----- Input 2 0 0 1 2 2 3 Output 2 Input 2 1 0 1 2 2 2 Output 0 Input 2 5 7 3 4 14 4 Output 1 -----Note----- In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1.
n, k1, k2 = input().split() a1 = input().split() a2 = input().split() res = [] for i in range(len(a1)): res.append(int(a1[i]) - int(a2[i])) res = [abs(i) for i in res] k = int(k1) + int(k2) k = int(k) n = int(n) if sum(res) < int(k): if (k - sum(res)) % 2: print("1") else: print("0") elif sum(res) == k: print("0") else: while k: res.sort() res[-1] = res[-1] - 1 k -= 1 result = 0 for i in res: result += i**2 print(result)
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING WHILE VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR