description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def get_ans(s, m, arr): ans = [(-1) for _ in range(m)] for i in range(m): if arr[i] == 0: ans[i] = 0 q = 1 for _ in range(m): upd = [] for curr in range(m): if ans[curr] == -1: b = 0 for comp in range(m): if ans[comp] > ans[curr]: b += abs(comp - curr) if b == arr[curr]: upd.append(curr) for i in upd: ans[i] = q q += 1 t = set(s) t = sorted(t) t.reverse() cter = [(-1) for _ in range(len(t))] for i in range(len(t)): q = 0 for c in s: if t[i] == c: q += 1 cter[i] = q cter2 = [] for i in range(len(t)): q = 0 for j in ans: if i == j: q += 1 cter2.append(q) ret = "" c = 0 while c < len(t): if cter[c] < cter2[c]: del cter[c] del t[c] else: c += 1 for i in ans: ret = ret + t[i] return ret t = int(input()) anss = [] for _ in range(t): s = input() m = int(input()) arr = input().split() arr = list(map(int, arr)) anss.append(get_ans(s, m, arr)) for i in anss: print(i)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + "\n") def wi(n): sys.stdout.write(str(n) + "\n") def wia(a): sys.stdout.write(" ".join([str(x) for x in a]) + "\n") def solve(s, m, b): n = len(s) ss = sorted([si for si in s], reverse=True) t = [""] * m ci = 0 not_taken = [i for i in range(m)] for _ in range(m): cnt = 0 tt = set() for j in range(m): if t[j] == "" and b[j] == 0: tt.add(j) not_taken.remove(j) cnt += 1 if cnt == 0: break while True: k = 0 while k < cnt and ci + k < n and ss[ci + k] == ss[ci]: k += 1 if k == cnt: break else: ci += k for j in tt: t[j] = ss[ci] k = 0 while ci + k < n and ss[ci + k] == ss[ci]: k += 1 ci += k for j in not_taken: for k in tt: b[j] -= abs(j - k) return "".join(t) def main(): for _ in range(ri()): s = rs() m = ri() b = ria() ws(solve(s, m, b)) main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR FOR VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) for i in range(t): s = input() b = [] d = dict() for j in s: if j in d.keys(): d[j] += 1 else: b.append(j) d[j] = 1 b.sort(reverse=True) n = int(input()) a = list(map(int, input().split())) c = ["a"] * n m = 1 f = dict() p = 0 while m <= n: j = 0 k = [] while j < n: if a[j] == 0 and j not in f.keys(): f[j] = 1 k.append(j) j += 1 while p < len(b): if d[b[p]] < len(k): p += 1 else: for i in k: c[i] = b[p] p += 1 break j = 0 while j < n: if j not in f.keys(): for i in k: a[j] += -abs(i - j) j += 1 m += len(k) print("".join(c))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
T = int(input()) for i in range(0, T): S = str(input()) m = int(input()) B = list(map(int, input().split())) vis = [] ans_int = [] S_int = [] for j in range(0, m): vis.append(0) ans_int.append(0) for j in range(0, len(S)): S_int.append(ord(S[j])) S_int.sort() S_int.reverse() tar = 0 indi = 0 while tar < m: mark = [] for j in range(0, m): if B[j] == 0 and vis[j] == 0: mark.append(j) vis[j] = 1 leng = len(mark) for j in range(0, m): for k in range(0, leng): if vis[j] == 0: B[j] -= abs(j - mark[k]) y = S_int[indi] while True: x = S_int[indi] y = x flag = 1 for k in range(1, leng): if S_int[indi + k] != x: flag = 0 break if flag == 0: indi += 1 else: break for j in range(0, leng): ans_int[mark[j]] = S_int[indi] indi += 1 while indi < len(S) and S_int[indi] == y: indi += 1 tar += leng ans = [] for k in range(0, m): ans.append(chr(ans_int[k])) X = "" ans = X.join(ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
import sys inp = [x for x in sys.stdin.read().split()] ii = 0 ttt = int(inp[ii]) ii += 1 for _ in range(ttt): s, m = inp[ii], int(inp[ii + 1]) ii += 2 b = [int(x) for x in inp[ii : ii + m]] ii += m cnt = [0] * 26 for c in s: cnt[ord(c) - ord("a")] += 1 res = [-1] * m for c in range(25, -1, -1): cur_ind = [] for i in range(m): if res[i] == -1: cur_sum = 0 for j in range(m): if res[j] != -1: cur_sum += abs(i - j) if cur_sum == b[i]: cur_ind.append(i) if len(cur_ind) <= cnt[c]: for i in cur_ind: res[i] = c print("".join(chr(c + ord("a")) for c in res))
IMPORT ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) for _ in range(t): s = input() m = int(input()) b = list(map(int, input().split())) slist = [] for i in range(len(s)): slist.append(s[i]) slist.sort() slist.reverse() slist2 = [] slist2.append([slist[0], 1]) for i in range(1, len(s)): if slist2[-1][0] == slist[i]: slist2[-1][1] += 1 else: slist2.append([slist[i], 1]) pointer = 0 done = [0] * m ans = [0] * m while sum(done) < m: zeros = [] for i in range(m): if b[i] == 0 and done[i] == 0: zeros.append(i) num = len(zeros) while slist2[pointer][1] < num: pointer += 1 for i in zeros: done[i] = 1 ans[i] = slist2[pointer][0] for j in range(m): b[j] -= abs(i - j) pointer += 1 print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) for _ in range(t): di = {} for i in input(): if i in di: di[i] += 1 else: di[i] = 1 li = list(di.items()) li.sort(reverse=True) m = int(input()) b = list(map(int, input().split())) now = [0] * m ans = [None] * m for c, cnt in li: tmp = 0 for i in range(m): if ans[i] is None and b[i] == now[i]: tmp += 1 if tmp > cnt: continue nxt = now[:] for i in range(m): if b[i] != now[i] or ans[i] is not None: continue ans[i] = c for j in range(m): nxt[j] += abs(i - j) now = nxt print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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 LIST NONE VAR FOR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def get_encoded(arr, s, m): vocab = {} for letter in s: if letter in vocab: vocab[letter] += 1 else: vocab[letter] = 1 res = [""] * m stop_arr = [-1] * m while arr != stop_arr: imin = [] for i, elem in enumerate(arr): if elem == 0: imin.append(i) letter_count = 0 while len(imin) > letter_count: max_key = max(vocab.keys()) letter_count = vocab[max_key] vocab.pop(max_key) for i in imin: res[i] = max_key for i, elem in enumerate(arr): if i in imin: arr[i] = -1 elif elem > -1: for big_letter in imin: arr[i] -= abs(i - big_letter) return "".join(res) t = int(input()) for _ in range(t): s = input() m = int(input()) arr = list(map(int, input().split())) res = get_encoded(arr, s, m) print(res)
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
q = int(input()) for q in range(0, q): s = input() m = int(input()) b = [int(x) for x in input().split()] count = [(0) for i in range(0, 26)] for ch in s: count[ord(ch) - ord("a")] += 1 k = 25 ans = ["a" for i in range(0, m)] okay = -1 while k >= 0: c = [] for i in range(0, m): if b[i] == 0: c.append(i) b[i] = okay d = len(c) while count[k] < d: k -= 1 for i in range(0, m): if b[i] > 0: for j in c: b[i] -= abs(i - j) elif b[i] == okay: ans[i] = chr(ord("a") + k) k -= 1 okay -= 1 for all in ans: print(all, end="") print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for _ in range(int(input())): s = list(input().strip()) m = int(input()) ans = [None] * m B = list(map(int, input().split())) s.sort(reverse=True) scnt = [] cnt = 1 for i in range(1, len(s)): if s[i] == s[i - 1]: cnt += 1 else: scnt.append(cnt) cnt = 1 scnt.append(cnt) vis = [True] * m si = j = 0 curr = [bi for bi in range(m) if B[bi] == 0] while len(curr): while scnt[si] < len(curr): j += scnt[si] si += 1 for bi in curr: vis[bi] = False ans[bi] = s[j] for bii in range(m): B[bii] -= abs(bi - bii) j += scnt[si] si += 1 curr = [bi for bi in range(m) if B[bi] == 0 and vis[bi]] print("".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def solve(s, m, b): f = [0] * 26 for c in s: f[ord(c) - ord("a")] += 1 sol = [-1] * m def _solve(xx): for x in range(xx, -1, -1): indices = [] for i in range(m): if sol[i] == -1: hm = 0 for j in range(m): if sol[j] != -1: hm += abs(i - j) if hm == b[i]: indices.append(i) if f[x] >= len(indices): for i in indices: sol[i] = x f[x] -= 1 if all(c != -1 for c in sol) or _solve(x - 1): return True for i in indices: sol[i] = -1 f[x] += 1 return False _solve(25) return "".join(chr(ord("a") + x) for x in sol) t = int(input()) for _ in range(t): s = input() m = int(input()) b = list(map(int, input().split())) print(solve(s, m, b))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
class AlphaHz: def __init__(self, s, m, b): self.s = s self.m = m self.b = b code = map(lambda x: ord(x) - ord("a"), s) self.count = [0] * 27 for c in code: self.count[c] += 1 self.mcode = 26 self.t = ["\x00"] * m def solve(self): while True: zeros = [] for i in range(m): if b[i] == 0: zeros.append(i) b[i] = -1 if self.count[self.mcode] < len(zeros): for i in range(self.mcode - 1, -1, -1): if self.count[i] >= len(zeros): self.mcode = i break for zero in zeros: self.t[zero] = chr(ord("a") + self.mcode) for i in range(m): for zero in zeros: b[i] -= abs(i - zero) if not len(zeros): break self.mcode -= 1 return "".join(self.t) t = int(input()) for i in range(t): s = input() m = int(input()) b = list(map(int, input().split())) obj = AlphaHz(s, m, b) t = obj.solve() print(t)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FUNC_DEF WHILE NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for _ in range(int(input())): s = input() m = int(input()) b = list(map(int, input().split())) alph = [0] * 26 n = len(s) ans = [""] * m for i in range(n): alph[ord(s[i]) - 97] += 1 ind = 25 while b.count(0) != 0: zero = b.count(0) while True: if alph[ind] >= zero: alph[ind] = 0 break ind -= 1 cur = [] for i in range(m): if b[i] == 0: ans[i] = chr(97 + ind) cur.append(i) b[i] = -1 for i in range(m): if b[i] != -1: for j in cur: b[i] -= abs(i - j) print("".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def find_zeros(ss): re = [] for sss in range(len(ss)): if ss[sss] == 0: re.append(sss) return re for q in range(int(input())): s = input() m = int(input()) d = dict() cur_value = m b = list(map(int, input().split())) times = [] while len(d) != m: zeros = find_zeros(b) for z in zeros: b[z] = -1 d[z] = cur_value times = [len(zeros)] + times cur_value -= 1 for i in range(m): if b[i] != -1: b[i] -= sum([abs(z - i) for z in zeros]) sd = sorted(s) + ["A"] s_list = [] cur_c = sd[0] counter = 0 ab_list = [] for i in sd: if i == cur_c: counter += 1 else: ab_list.append(cur_c) cur_c = i s_list.append(counter) counter = 1 start = 100 l = len(times) abc_list = [] for i in times: while s_list[0] < i: s_list = s_list[1:] ab_list = ab_list[1:] abc_list.append(ab_list[0]) s_list = s_list[1:] ab_list = ab_list[1:] ans = ["0"] * m for key in range(m): ans[key] = abc_list[d[key] - m + l - 1] print("".join(ans))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST STRING ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def prog(): for _ in range(int(input())): s = list(input().strip()) m = int(input()) b = list(map(int, input().split())) s.sort(reverse=True) most = [] prev = "asdf" for val in s: if val != prev: prev = val most.append([1, val]) else: most[-1][0] += 1 idx = 0 output = [0] * m placed = 0 while placed != m: zeros = [] for i in range(m): if b[i] == 0: zeros.append(i) placed += len(zeros) for i in zeros: b[i] = -1 while most[idx][0] < len(zeros): idx += 1 for i in zeros: output[i] = most[idx][1] for j in range(m): if b[j] != -1: b[j] -= abs(i - j) idx += 1 print("".join(output)) prog()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) for _ in range(t): string = [s for s in input()] dic = {} arr = [] for i in string: dic[i] = dic.get(i, 0) + 1 for i, j in dic.items(): arr.append((i, j)) arr.sort(key=lambda x: x[0]) n = int(input()) a = [int(c) for c in input().split()] ans = [None for i in range(n)] while True: zeroindex = [] zc = 0 for i in range(n): if a[i] == 0: zeroindex.append(i) zc += 1 if zc == 0: break while True: pair = arr.pop() if pair[1] >= zc: break for i in zeroindex: ans[i] = pair[0] a[i] = float("inf") for j in range(1, n - i): a[i + j] = a[i + j] - j for j in range(i + 1): a[i - j] = a[i - j] - j print("".join([str(x) for x in ans]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def main(): q = int(input()) for _ in range(q): s = input() m = int(input()) b = [int(x) for x in input().split()] solve(s, m, b) def solve(s, m, b): letters = sorted(set(s), reverse=True) t = ["X"] * m for l in letters: if s.count(l) < b.count(0): continue zero = [i for i in range(m) if b[i] == 0] positive = [j for j in range(m) if b[j] > 0] for i in zero: t[i] = l for j in positive: b[j] -= abs(i - j) b[i] = -1 print("".join(t)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) while t: t -= 1 s = input() m = int(input()) b = list(map(int, input().split())) ans = ["."] * m ss = "".join(list(set(sorted(s)))) idx = len(ss) - 1 while True: idxx = [] f = 0 for i in range(m): if not b[i] and ans[i] == ".": idxx.append(i) f = 1 if not f: break while s.count(ss[idx]) < len(idxx): idx -= 1 for i in idxx: ans[i] = ss[idx] idx -= 1 for i in range(m): if ans[i] == ".": for j in idxx: b[i] -= max(i - j, j - i) ans = "".join(ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR WHILE FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def solve(line, bees): line = sorted(line, reverse=True) line2 = [] quant = [] for i in line: if i not in line2: line2.append(i) quant.append(1) else: quant[len(quant) - 1] += 1 line = line2 del line2 B = len(bees) result_old = [None] * B result_new = [None] * B while filled(result_new) == False: to_edit = [] for i in range(B): if value(result_old, i) == bees[i] and result_old[i] == None: to_edit.append(i) if len(to_edit) <= quant[0]: for i in to_edit: result_new[i] = line[0] result_old = result_new.copy() line.pop(0) quant.pop(0) return "".join(result_old) def filled(result): for i in result: if i == None: return False return True def value(line, p): result = 0 for i in range(len(line)): if line[i] != None: result += abs(i - p) return result N = int(input()) result = [] for i in range(N): line = input() m = int(input()) bees = [int(x) for x in input().split()] result.append(solve(line, bees)) for i in result: print(i)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NONE EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL STRING VAR FUNC_DEF FOR VAR VAR IF VAR NONE RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NONE VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): cnt = [0] * 26 for i in minp(): cnt[ord(i) - ord("a")] += 1 m = mint() b = list(mints()) ans = ["a"] * m j = 25 while True: c = b.count(0) if c == 0: break while j >= 0: if cnt[j] >= c: break j -= 1 p = [] for i in range(m): if b[i] == 0: p.append(i) for x in p: for i in range(m): b[i] -= abs(i - x) ans[x] = chr(ord("a") + j) b[x] += 1000000000 j -= 1 print("".join(ans)) for i in range(mint()): solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def f(): a = input() n = int(input()) b = list(map(int, input().split(" "))) string = ["0"] * len(b) a = list(a) a.sort(reverse=True) temp = [] while 1: for i in range(0, len(b)): flag = 0 s = 0 if string[i] == "0": for j in range(0, len(temp)): if temp[j][0] > a[0]: s += abs(temp[j][1] - i) if s == b[i]: flag = 1 temp.append([a[0], i]) string[i] = a[0] break if flag == 0: string[temp[len(temp) - 1][1]] = "0" temp.pop(len(temp) - 1) continue if len(temp) == len(b): break a.pop(0) print(*string, sep="") for i in range(int(input())): f()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST WHILE NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) ans = [] for _ in range(t): s = list(input()) x = sorted(set(s)) n = int(input()) l = list(map(int, input().split())) z = [0] * n while l != [-1] * n: c = 0 for i in range(n): if l[i] == 0: c += 1 while True: q = x.pop() if c <= s.count(q): break y = [] for i in range(n): if l[i] == 0: z[i] = q y.append(i) l[i] = -1 for i in range(n): if l[i] != -1: su = 0 for j in y: su += abs(i - j) l[i] = l[i] - su ans.append("".join(z)) print("\n".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
import sys q = int(sys.stdin.readline()) ans_arr = [] for i in range(q): s = sys.stdin.readline().strip() m = int(sys.stdin.readline()) b = [int(j) for j in sys.stdin.readline().split()] arr = [] freq = [0] * 26 for g in range(len(s)): arr.append(s[g]) freq[ord(s[g]) - 97] += 1 arr.sort() ans = [] for g in range(m): ans.append("a") ptr = len(arr) - 1 d_idx = {} d_ch = {} for sol in range(m): ind = -2 ctr = 0 for e in range(m): if b[e] == 0 and e not in d_idx: if ind == -2: ind = e ctr += 1 num = ord(arr[ptr]) - 97 while freq[num] < ctr: for g in range(freq[num]): ptr -= 1 num = ord(arr[ptr]) - 97 d_idx[ind] = 1 ans[ind] = arr[ptr] if arr[ptr] not in d_ch: d_ch[arr[ptr]] = 1 else: d_ch[arr[ptr]] += 1 if ctr == 1 and freq[num] - d_ch[arr[ptr]] > 0: for g in range(freq[num] - d_ch[arr[ptr]]): ptr -= 1 ptr -= 1 for g in range(m): if b[g] != 0: b[g] = b[g] - abs(g - ind) answer = "" for h in range(m): answer += ans[h] ans_arr.append(answer) print("\n".join(ans_arr))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def findval(x, arr): ans = 0 for i in arr: tem = x - i if tem < 0: tem = tem * -1 ans = ans + tem return ans def findmin(arr, nrr): res = [] for i in range(len(arr)): if arr[i] == -1: continue nu = findval(i, nrr) if nu == arr[i]: res.append(i) arr[i] = -1 return arr, res T = int(input()) for _ in range(T): s = input().rstrip() dic = {} for i in s: dic[i] = 0 for i in s: dic[i] += 1 s = "" for i in dic: s = s + i s = sorted(s) s.reverse() M = int(input()) arr = list(map(int, input().rstrip().split())) ns = [] ls = [(0) for i in range(M)] while len(ns) != M: arr, res = findmin(arr, ns) ns = ns + res for i in range(len(s)): if dic[s[i]] >= len(res): chara = s[i] s = s[i + 1 :] break for i in res: ls[i] = chara s = "" for i in range(M): s = s + ls[i] print(s)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): S = input().strip() m = int(input()) B = list(map(int, input().split())) LIST = [0] * 26 for s in S: LIST[ord(s) - 97] += 1 ANS = [0] * m ind = 25 while max(B) >= 0: L = [] for i in range(m): if B[i] == 0: L.append(i) B[i] = -1 LEN = len(L) while LIST[ind] < LEN: ind -= 1 for l in L: ANS[l] = ind ind -= 1 for l in L: for i in range(m): B[i] -= abs(i - l) print("".join([chr(a + 97) for a in ANS]))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
from sys import stdin input = stdin.readline def main(): test = int(input()) for _ in range(test): l = list(input().strip()) ss = list(set(l)) ss.sort(reverse=True) m = int(input()) b = [int(i) for i in input().split(" ")] s = ["" for i in range(m)] t = 0 val = [(0) for i in range(m)] while t < len(ss): ll = [] for i in range(m): if b[i] == val[i]: ll.append(i) p = ss[t] while t < len(ss): p = ss[t] c = l.count(p) t += 1 if c >= len(ll): break for i in ll: s[i] = p for i in range(m): if i not in ll and val[i] != -1: for j in range(len(ll)): val[i] += abs(i - ll[j]) else: val[i] = -1 print("".join(s)) main()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
A = ord("a") def main(): s = input() m = int(input()) b = [] for obj in input().split(): b.append(int(obj)) d = [0] * 26 for char in s: d[ord(char) - A] += 1 count_0 = m x = 25 ans = ["-"] * m while count_0 != 0: a = [] for i in range(m): if b[i] == 0: b[i] = -1 a.append(i) L = len(a) while L > d[x]: x -= 1 char = chr(x + A) for i in a: ans[i] = char for i in range(m): if b[i] == -1: continue sum_l = 0 for num in a: sum_l += abs(num - i) b[i] -= sum_l count_0 -= L x -= 1 print(*ans, sep="") q = int(input()) for _ in range(q): main()
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for ii in range(int(input())): s = input() m = int(input()) l = [int(i) for i in input().split()] dic = {} for i in s: if i in dic: dic[i] += 1 else: dic[i] = 1 li = sorted(list(dic.keys())) dic2 = {} for i in range(m): dic2[i] = l[i] j = len(li) - 1 ans = ["" for i in range(m)] while j >= 0: pos = [i for i in list(dic2.keys()) if dic2[i] == 0] if pos == []: break if dic[li[j]] >= len(pos): for i in pos: ans[i] = li[j] del dic2[i] for i in list(dic2.keys()): for p in pos: dic2[i] -= abs(i - p) j -= 1 print("".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR LIST IF VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
import sys read = lambda: sys.stdin.readline() T = int(read()) for t in range(T): s = read() m = int(read()) b = list(map(int, read().split())) s = sorted(s, reverse=True) result = [None for i in range(m)] si = 0 while True: indx = [] num0 = 0 for i in range(m): if result[i] == None and b[i] == 0: indx.append(i) num0 += 1 if num0 == 0: break while s[si:].count(s[si]) < num0: si += s[si:].count(s[si]) for i in indx: result[i] = s[si] for i in range(m): if result[i] == None: for j in indx: b[i] -= abs(i - j) si += s[si:].count(s[si]) print("".join(result))
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
q = int(input()) for _ in range(q): s = list(input()) m = int(input()) b = list(map(int, input().split())) numberB = [-1] * m determined = 0 index = 0 maxIndex = 0 while True: zeros = [] for i in range(m): if numberB[i] == -1 and b[i] == 0: zeros.append(i) for elem in zeros: numberB[elem] = index maxIndex = index index += 1 determined += len(zeros) if determined >= m: break for i in range(m): if numberB[i] != -1: continue for elem in zeros: b[i] -= abs(i - elem) countS = [0] * 26 for elem in s: countS[ord(elem) - ord("a")] += 1 curLetter = 0 ans = [""] * m for i in range(maxIndex, -1, -1): curAdd = [] for j in range(m): if i == numberB[j]: curAdd.append(j) while countS[curLetter] < len(curAdd): curLetter += 1 for elem in curAdd: ans[elem] = chr(curLetter + ord("a")) curLetter += 1 print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
m = 10**9 + 7 t = int(input()) while t: t -= 1 s = input() m = int(input()) b = list(map(int, input().split())) fin = sorted(s) ans = [""] * m while "" in ans: new = [index for index, value in enumerate(b) if value == 0] while fin.count(fin[-1]) < len(new): fin = list(filter(lambda x: x != fin[-1], fin)) for i in new: b[i] = -1 ans[i] = fin[-1] fin = list(filter(lambda x: x != fin[-1], fin)) for i in range(len(b)): if b[i] != -1: for j in new: b[i] -= abs(i - j) print("".join(ans))
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR WHILE STRING VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
MAX = 100000000 t = int(input()) for tt in range(t): s = input() m = int(input()) b = list(map(int, input().split())) ans = ["c"] * m group = list() while True: pos = list() for i in range(m): if b[i] == 0: pos.append(i) if len(pos) == 0: break else: group.append(pos) for i in range(m): if b[i] == 0: b[i] = MAX else: for j in pos: b[i] -= abs(i - j) freq = {} for char in s: if char in freq: freq[char] += 1 else: freq[char] = 1 sort_freq = sorted(freq.items(), key=lambda x: x[0], reverse=True) items = iter(sort_freq) char = next(items) for pos in group: while char[1] < len(pos): try: char = next(items) except StopIteration: break for pp in pos: ans[pp] = char[0] try: char = next(items) except StopIteration: break print("".join(ans))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR WHILE VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
tests = int(input()) for _ in range(tests): s = input() n = int(input()) b = list(map(int, input().split())) g = sorted(b) ans = ["a"] * n d = dict() for i in set(s): d[i] = 0 for i in s: d[i] += 1 tec = 122 r = b.count(0) while r != 0: flag = True while flag: if chr(tec) in d: if d[chr(tec)] >= r: flag = False break tec -= 1 sp = [] for i in range(n): if b[i] == 0: sp.append(i) b[i] = -1 for i in range(n): summ = 0 if b[i] < 0: continue for j in sp: summ += abs(i - j) b[i] -= summ for i in sp: ans[i] = chr(tec) tec -= 1 r = b.count(0) for i in ans: print(i, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
letters = 26 for q in range(int(input())): s = input() m = int(input()) bs = list(map(int, input().split(" "))) vs = [(ord(c) - ord("a")) for c in s] counts = [0] * letters for v in vs: counts[v] += 1 eqs = [None] * m eqCounts = [] eqCur = 0 while True: zinds = [i for i, b in enumerate(bs) if b == 0] for zi in zinds: for i in range(m): bs[i] -= abs(i - zi) bs[zi] -= 1 eqs[zi] = eqCur eqCur += 1 if len(zinds) == 0: break eqCounts += [len(zinds)] mapping = {} let = 0 for eq, count in reversed(list(enumerate(eqCounts))): while counts[let] < count: let += 1 mapping[eq] = chr(ord("a") + let) let += 1 print("".join(mapping[eq] for eq in eqs))
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR LIST FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def solve(s, m, arr, ans): t = [-1] * m letters = [0] * 26 for i in s: letters[ord(i) - ord("a")] += 1 limit = 25 indices = [] count = arr.count(0) while True: index = -1 for i in range(limit, -1, -1): if letters[i] >= count: limit = i - 1 index = i break if not indices: for i in range(m): if arr[i] == 0: t[i] = chr(ord("a") + index) indices.append(i) else: for i in good: t[i] = chr(ord("a") + index) indices.extend(good) good = [] for i in range(m): if t[i] != -1: continue count = 0 for index in indices: count += abs(index - i) if count == arr[i]: good.append(i) if not good: break count = len(good) t1 = "" for i in t: t1 += i ans.append(t1) def main(): q = int(input()) ans = [] for i in range(q): s = input() m = int(input()) arr = list(map(int, input().split())) solve(s, m, arr, ans) for i in ans: print(i) main()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) cnt = 0 while cnt < t: cnt += 1 a = [0] * 26 for i in input(): a[ord(i) - 97] += 1 a.reverse() n = int(input()) res = [" "] * n visited = [0] * n b = [int(i) for i in input().split()] pos = 0 while True: tmp = [] for i, elem in enumerate(b): if elem == 0 and visited[i] == 0: tmp.append(i) visited[i] = 1 tmp_total = len(tmp) if tmp_total == 0: break for elem in range(pos, 26): if a[elem] >= tmp_total: for i in tmp: res[i] = chr(25 - elem + 97) a[elem] = 0 pos = elem break for j in range(n): if visited[j] == 0: for i in tmp: b[j] -= abs(j - i) print("".join(i for i in res))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
letters = "abcdefghijklmnopqrstuvwxyz" T = int(input()) def min_index(bs): min_b = 2000 min_is = [] for i, b in enumerate(bs): if b == None: continue if b == min_b: min_is.append(i) if b < min_b: min_b = b min_is = [i] for i in min_is: bs[i] = None return min_is, min_b def max_letter(letter_dict, many): for l in letters[::-1]: if letter_dict[l] >= many: letter_dict[l] = 0 return l letter_dict[l] = 0 for t in range(T): letter_dict = dict() for l in letters: letter_dict[l] = 0 s = input() for a in s: letter_dict[a] += 1 m = int(input()) ans = list(range(m)) bs = list(int(b) for b in input().split()) while True: min_is, min_b = min_index(bs) if len(min_is) == 0: break a = max_letter(letter_dict, len(min_is)) for i in min_is: ans[i] = a for j, b in enumerate(bs): if b is not None: bs[j] -= abs(j - i) for a in ans: print(a, end="") print()
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NONE IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR FOR VAR VAR ASSIGN VAR VAR NONE RETURN VAR VAR FUNC_DEF FOR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NONE VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for _ in range(int(input())): s = input() m = int(input()) b = [int(x) for x in input().split()] indice_done = [] i = 0 ip = [] if len(indice_done) == 0: temp = [] for i in range(m): if b[i] == 0: indice_done.append(i) temp.append(i) ip.append(temp) while len(indice_done) != m: temp = [] for j in range(m): if j not in indice_done: sm = 0 for k in indice_done: sm += abs(j - k) if sm == b[j]: temp.append(j) indice_done.extend(temp) ip.append(temp) ans = [(-1) for i in range(m)] nl = sorted(list(set(s))) d = {} for i in s: if i not in d.keys(): d[i] = 1 else: d[i] += 1 si = 0 for i in range(-1, -len(ip) - 1, -1): tl = len(ip[i]) for j in range(si, len(nl)): if d[nl[j]] >= tl: for k in ip[i]: ans[k] = nl[j] si = j + 1 break print("".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def update(arr, zeros, replacement): for i in range(len(arr)): if arr[i] == 0: arr[i] = replacement if isinstance(arr[i], int): arr[i] -= sum([abs(i - zero) for zero in zeros]) return arr def find(s, num): r = s[-1] if s.count(r) >= num: while s[-1] == r: s.pop() return s, r else: while s[-1] == r: s.pop() return find(s, num) for _ in range(int(input())): s = ["*"] + sorted(list(input())) n = int(input()) arr = [int(i) for i in input().split()] while n > 0: zeros = [] for i, x in enumerate(arr): if x == 0: zeros.append(i) n -= len(zeros) s, replacement = find(s, len(zeros)) arr = update(arr, zeros, replacement) print("".join(arr))
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
T = int(input()) for _ in range(T): a = [0] * 26 for i in input().strip(): a[ord(i) - ord("a")] += 1 n = int(input()) b = list(map(int, input().split())) s = [-1] * n x = 26 while s.count(-1) > 0: x, y = x - 1, b.count(0) while a[x] < y: x -= 1 for i in range(n): if b[i] == 0: s[i] = x for i in range(n): if s[i] == x: b[i] = -1 for j in range(n): if b[j] > 0: b[j] -= abs(i - j) print("".join(chr(i + ord("a")) for i in s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR 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 NUMBER WHILE FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(int(input(""))) while t > 0: string = input("") n = int(input("")) b = list(map(int, input().split())) l = list(string) l.sort() string_length = len(l) final = [] finished = [] for j in range(string_length): counter = string.count(l[j]) if l[j] not in finished: final.append((l[j], counter)) finished.append(l[j]) final = final[::-1] index = 0 ans = [0] * n while 0 in b: times = b.count(0) minicount = [] for i in range(1, times + 1): for j in range(index, len(final)): if final[j][1] >= times: index = j break inserter = b.index(0) b[inserter] = -1 ans[inserter] = final[index][0] minicount.append(inserter) index += 1 for x in range(len(b)): for y in minicount: b[x] -= abs(x - y) answer = "" print(answer.join(ans)) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) for _ in range(t): s = list(input()) m = int(input()) a = list(map(int, input().split())) s.sort() count = 0 flag = True ans = [0] * m ptr = len(s) - 1 while count < m: temp = [] for i in range(m): if a[i] == 0: temp.append(i) a[i] = -1 count = count + len(temp) c = 1 if c != len(temp): for j in range(ptr - 1, -1, -1): if s[j] == s[j + 1]: c += 1 else: c = 1 if c == len(temp): ptr = j + c - 1 break ch = s[ptr] for i in temp: ans[i] = s[ptr] ptr -= 1 j = 1 while i - j >= 0: if a[i - j] > 0: a[i - j] = a[i - j] - j j += 1 j = 1 while i + j < m: if a[i + j] > 0: a[i + j] -= j j += 1 while ptr >= 0 and s[ptr] == ch: ptr -= 1 for i in ans: print(i, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) for _ in range(t): list1 = list(input()) m = int(input()) list2 = list(map(int, input().strip().split())) map1 = dict() for val in list1: if val in map1: map1[val] += 1 else: map1[val] = 1 ans = [] res = [] b = 0 while 1: temp = [] for i in range(m): index = i required = 0 if i in ans: continue for j in range(len(ans)): required += abs(ans[j] - index) if list2[index] == required: temp.append(index) res.append(temp) for val in temp: ans.append(val) if len(ans) == m: break list3 = sorted(map1) list3.reverse() output = [] for i in range(m): output.append(-1) pointer = 0 for val in list3: if map1[val] >= len(res[pointer]): for indices in res[pointer]: output[indices] = val pointer += 1 if pointer == len(res): break for letter in output: print(letter, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for _ in range(int(input())): alpha = [0] * 26 s = input() for i in s: alpha[ord(i) - 97] += 1 curr = 25 n = int(input()) b = list(map(int, input().split())) ans = [""] * n visited = [False] * n com = 0 while com < n: pos = [] for i in range(n): if b[i] == 0 and not visited[i]: visited[i] = True com += 1 pos.append(i) posl = len(pos) for i in pos: for j in range(n): if b[j] == 0: continue b[j] -= abs(i - j) while posl > alpha[curr]: curr -= 1 for i in pos: ans[i] = chr(97 + curr) curr -= 1 print("".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) for z in range(t): st = list(input()) n = int(input()) arr = list(map(int, input().split())) d = {} for i in range(len(st)): if st[i] not in d: d[st[i]] = 0 d[st[i]] += 1 flag = [(1) for i in range(n)] a = [] for i in range(n): if arr[i] == 0: a.append(i) res, res2 = [], [] while len(a) > 0: ne = [] res.append(len(a)) res2.append(a) for i in a: flag[i] = 0 for i in a: for j in range(0, i): if flag[j] == 1: arr[j] -= abs(i - j) if arr[j] == 0: flag[j] = 0 ne.append(j) for j in range(i + 1, n): if flag[j] == 1: arr[j] -= abs(i - j) if arr[j] == 0: flag[j] = 0 ne.append(j) a = ne j = 0 ans = ["a" for i in range(n)] for i in range(25, -1, -1): c = chr(97 + i) if c in d: if d[c] >= res[0]: for j in res2[0]: ans[j] = c del res[0] del res2[0] if len(res) == 0: break print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR IF VAR VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def counter(l, a): count = 0 for i in range(len(l)): if l[i] == a: count += 1 return count t = int(input()) for _ in range(t): s = list(input()) n = len(s) m = int(input()) b = list(map(int, input().split())) t = ["0"] * m while True: z = [] for i in range(m): if b[i] == 0: z.append(i) maxx = max(s) if counter(s, maxx) >= len(z): for i in z: t[i] = maxx for j in range(m): if j not in z: b[j] -= abs(i - j) b[i] = -1 for i in range(n): if s[i] == maxx: s[i] = "0" else: for i in range(n): if s[i] == maxx: s[i] = "0" if b == [-1] * m or s == ["0"] * n: break for i in range(m): print(t[i], end="") print("")
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR WHILE NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR STRING IF VAR BIN_OP LIST NUMBER VAR VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
T = int(input()) for _ in range(T): s = input().strip() m = int(input()) b = list(map(int, input().strip().split())) letters = [0] * 26 for l in s: letters[ord(l) - 97] += 1 z = [] for i, x in enumerate(b): if x == 0: z.append(i) res = [0] * m letter_counts = [] while len(z) > 0: zc = len(z) letter_counts.append(zc) for zs in z: res[zs] = len(letter_counts) - 1 b[zs] = -1 for i in range(m): index_diff = 0 for zs in z: index_diff += abs(zs - i) b[i] -= index_diff z = [] for i, x in enumerate(b): if x == 0: z.append(i) letter_map = [] i = 0 j = 25 while i < len(letter_counts) and j > -1: if letters[j] >= letter_counts[i]: letter_map.append(chr(j + 97)) i += 1 j -= 1 word = "" for r in res: word += letter_map[r] print(word)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
q = int(input()) for _ in range(q): letters = [0] * 26 s = input() for a in s: letters[ord(a) - ord("a")] += 1 m = int(input()) b = list(map(int, input().split())) t = ["0"] * m deleted = 0 lastletter = 25 while deleted < m: x = b.count(0) while letters[lastletter] < x: lastletter -= 1 zeros = [] for qq in range(len(b)): if b[qq] == 0: t[qq] = chr(ord("a") + lastletter) zeros.append(qq) b[qq] = -1 deleted += x for f in range(len(b)): if b[f] >= 0: for df in zeros: b[f] -= abs(df - f) lastletter -= 1 print("".join(t))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def works(c, i, prob, btgt, m): val = 0 for j in range(m): if prob[j] != " " and c < prob[j]: val += abs(i - j) return val == btgt def solve0(prob, s, slack, b, m, depth): if prob.find(" ") == -1: return prob, True c = s.pop() for i in range(m): if prob[i] != " ": continue if works(c, i, prob, b[i], m): ans, ok = solve0( prob[:i] + c + prob[i + 1 :], s.copy(), slack, b, m, depth + 1 ) if ok: return ans, True if slack > 0: ans, ok = solve0(prob, s, slack - 1, b, m, depth + 1) if ok: return ans, True return "", False def solve(s, m, b): s = sorted(s) slack = len(s) - m ans, correct = solve0(" " * m, s, slack, b, m, 0) return ans cases = int(input().strip()) for _ in range(cases): s = input().strip() m = int(input().strip()) b = list(map(int, input().strip().split())) print(solve(s, m, b))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR STRING NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR RETURN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR RETURN VAR NUMBER RETURN STRING NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
T = int(input()) for _ in range(T): s = input() m = int(input()) l = list(map(int, input().split())) cnt = [0] * 26 for i in range(len(s)): cnt[ord(s[i]) - 97] += 1 ze = [] li = [] for i in range(m): if l[i] == 0: li.append(i) ze.append(li) while 1: li = [] for i in range(m): if l[i] != 0: for j in range(len(ze[-1])): l[i] = l[i] - abs(i - ze[-1][j]) if l[i] == 0: li.append(i) if len(li) == 0: break else: ze.append(li) ans = [0] * len(ze) cnt = cnt[::-1] j = 0 fin = [0] * m for i in range(len(ze)): num = len(ze[i]) while j < 26: if cnt[j] >= num: ans[i] = 26 - j j += 1 break j += 1 for i in range(len(ans)): for j in range(len(ze[i])): fin[ze[i][j]] = chr(ans[i] + 96) print("".join(fin))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
q = int(input()) def helper(inputS, m, inputB): s = inputS.copy() b = inputB.copy() processed = set() result = [(-1) for x in b] while -1 in result: indices = [i for i in range(m) if b[i] == 0 and i not in processed] numZeros = len(indices) while len(set(s[-numZeros:])) != 1: s.pop() usedLetter = s[-1] for idx in indices: result[idx] = s[-1] s.pop() for j in reversed(range(idx)): if b[j] != 0: b[j] -= abs(idx - j) for j in range(idx + 1, m): if b[j] != 0: b[j] -= abs(idx - j) processed.add(idx) curIndex = len(s) - 1 while curIndex >= 0 and s[curIndex] == usedLetter: s.pop() curIndex -= 1 return "".join(result) for _ in range(q): s = list(input()) m = int(input()) b = [int(x) for x in input().split()] s.sort() result = helper(s, m, b) print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR WHILE NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
import sys T = int(sys.stdin.readline()) for _ in range(T): s = list(sys.stdin.readline().strip()) m = int(sys.stdin.readline()) b = list(map(int, sys.stdin.readline().split())) result = ["0"] * m list_count = [0] * 26 for l in s: list_count[ord(l) - 97] += 1 a = 25 while True: list_zero = [] numz = 0 for i in range(m): if b[i] == 0: list_zero.append(i) numz += 1 if not numz: break for a in range(a, -1, -1): if list_count[a] >= numz: list_count[a] = 0 break word = chr(97 + a) for i in range(m): if b[i] > 0: for j in list_zero: b[i] -= abs(i - j) for i in list_zero: result[i] = word b[i] = -1 print("".join(result))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
from sys import gettrace, stdin if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def main(): def solve(): ss = list(input()) + [" "] m = int(input()) bb = [int(a) for a in input().split()] ss.sort(reverse=True) tt = ["a"] * m sp = 0 while True: ilist = [] p = -1 while 0 in bb[p + 1 :]: p = bb.index(0, p + 1) ilist.append(p) bb[p] = -1 req = len(ilist) while True: base = ss[sp] bcount = 0 while ss[sp] == base: bcount += 1 sp += 1 if bcount >= req: break for p in ilist: tt[p] = base for i in range(m): if bb[i] > 0: bb[i] -= abs(i - p) if 0 not in bb: break print("".join(tt)) t = int(inputi()) for _ in range(t): solve() main()
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for tt in range(int(input())): s = [i for i in input()] n = int(input()) a = list(map(int, input().split())) cnt = [0] * 26 for i in s: cnt[ord(i) - 97] += 1 pq = [] for i in range(26): if cnt[i] != 0: pq.append((chr(i + 97), cnt[i])) ans = [""] * n zCount = a.count(0) inds = [] left = n while pq[-1][1] < zCount: pq.pop() for i in range(n): if a[i] == 0: left -= 1 inds.append(i + 1) ans[i] = pq[-1][0] pq.pop() while left: check = [] for i in range(n): if ans[i] != "": continue this = 0 for j in inds: this += abs(j - (i + 1)) if this == a[i]: check.append(i) while pq[-1][1] < len(check): pq.pop() for i in check: ans[i] = pq[-1][0] left -= 1 inds.append(i + 1) pq.pop() print("".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR WHILE VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) for _ in range(t): s = str(input()) m = int(input()) b = list(map(int, input().split())) d = {} for x in s: if x in d.keys(): d[x] += 1 else: d[x] = 1 keys = list(d.keys()) keys.sort(reverse=True) i = 0 l = 0 t = [0] * m while l < m: z = [] for j in range(m): if b[j] == 0: z.append(j) lz = len(z) k = keys[i] while d[k] < lz: i += 1 k = keys[i] for j in range(m): if j in z: t[j] = k b[j] = -10 else: for h in z: b[j] -= abs(h - j) l += lz i += 1 for x in t: print(x, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for _ in range(int(input())): s = input() n = int(input()) arr = list(map(int, input().split())) qu = list(0 for _ in range(30)) for i in s: qu[ord(i) - ord("a")] += 1 ind = 29 ans = list("0" for i in range(len(arr))) pop = 0 while pop != len(arr): k = 0 for i in arr: if i == 0: k += 1 for i in range(ind, -1, -1): if qu[i] >= k: ind = i break kek = list() for i in range(len(arr)): if arr[i] == 0: arr[i] = -1 ans[i] = chr(ind + ord("a")) pop += 1 kek.append(i) for j in range(len(arr)): if arr[j] != 0: for i in kek: arr[j] -= abs(i - j) ind -= 1 print(*ans, sep="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s)]) def invr(): return map(int, input().split()) entries = inp() for i in range(entries): s = insr() z = inp() l = inlt() s.sort() s.reverse() char_count = [] cur_char = s[0] count = 0 for x in s: if x != cur_char: char_count.append([cur_char, count]) count = 1 cur_char = x else: count += 1 char_count.append([cur_char, count]) res = [0] * z pos = 0 ind = [] while 0 in res: zero_pos = [] for i in range(len(res)): if l[i] == 0 and i not in ind: zero_pos.append(i) while char_count[pos][1] < len(zero_pos): pos += 1 for i in range(len(l)): for x in zero_pos: l[i] -= abs(i - x) for x in zero_pos: res[x] = char_count[pos][0] ind.append(x) pos += 1 ans = "" for x in res: ans += x print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def know_new(l): place = [] for i in range(len(l)): if l[i] == 0: place.append(i) for i in range(len(b)): if b[i] != 0 and b[i] != -1: for j in range(len(place)): b[i] -= abs(i - place[j]) for e in range(len(place) - 1, -1, -1): b[place[e]] = -1 save.append(place) return len(place) t = int(input()) for x in range(t): s = input() m = int(input()) b = list(map(int, input().split())) a = [(0) for i in range(26)] for i in range(len(s)): a[ord(s[i : i + 1]) - ord("a")] += 1 arr = [] save = [] while b != [(-1) for zxc in range(len(b))]: arr.append(know_new(b)) j = 25 i = 0 ans = [(0) for el in range(len(b))] while i < len(arr) and j > -1: if a[j] >= arr[i]: for element in save[i]: ans[element] = chr(ord("a") + j) i += 1 j -= 1 print("".join(map(str, ans)))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) for x in range(t): s = input() s = sorted(s) s.reverse() n = int(input()) b = list(map(int, input().split(" "))) ch = ["" for i in range(len(b))] zeros = b.count(0) while zeros != 0: idx = [] for i, j in enumerate(b): if j == 0: idx.append(i) b[i] = -1 while s.count(s[0]) < zeros: s.remove(s[0]) for i in idx: ch[i] = s[0] for j in range(len(b)): b[j] -= abs(i - j) zeros = b.count(0) add_c = s[0] while add_c in s: s.remove(add_c) print("".join([i for i in ch]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for _ in range(int(input())): s = ["#"] + sorted(list(input())) n = int(input()) b = [-1] + [int(i) for i in input().split()] ans = ["#"] * (n + 1) while 0 in b: zero = [] for i in range(1, n + 1): if b[i] == 0: zero.append(i) b[i] = -1 while s.count(s[-1]) < len(zero): p = s[-1] while s[-1] == p: s.pop() p = 0 for i in zero: p = ans[i] = s[-1] while s[-1] == p: s.pop() for i in range(1, n + 1): if ans[i] == "#": for j in zero: b[i] -= abs(j - i) print("".join(ans[1:]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER WHILE NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
q = int(input()) for _ in range(q): s = list(input()) m = int(input()) b = list(map(int, input().split())) n = len(s) alphcnt = [0] * 26 for i in s: alphcnt[122 - ord(i)] += 1 now = [0] * m cnt = 0 u = [] for _ in range(26): end = [] for i in range(m): if b[i] == now[i]: cnt += 1 end.append(i) if not end: break for j in end: for i in range(m): now[i] += abs(i - j) now[j] = 114514 u.append(end) ans = ["?"] * m i = 0 for v in u: l = len(v) while not alphcnt[i] >= l: i += 1 for j in v: ans[j] = chr(122 - i) i += 1 ans = "".join(ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
q = int(input()) results = [] for case in range(q): s = [c for c in input()] s_letters = sorted(list(set(s))) m = int(input()) b = list(map(int, input().split())) t = [""] * m while sum(b) != -m: zeroes = set() for i in range(m): if b[i] == 0: zeroes.add(i) while True: letter = s_letters.pop() if s.count(letter) >= len(zeroes): for i in zeroes: t[i] = letter b[i] = -1 break for n in range(m): for i in zeroes: if b[n] != -1: b[n] -= abs(i - n) print("".join(t))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for _ in range(int(input())): f = [0] * 26 s = input().strip() m = int(input()) b = list(map(int, input().split())) for i in s: f[ord(i) - 97] += 1 n = len(s) ans = ["."] * m c = 122 while 1: z = [] for i in range(0, m): if b[i] == 0: z.append(i) if len(z) == 0: break while f[c - 97] < len(z) and c >= 97: c -= 1 for i in z: ans[i] = chr(c) b[i] = -1 for i in range(0, m): if ans[i] == ".": for j in z: b[i] -= abs(i - j) c -= 1 print("".join(str(a) for a in ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def fill(ans, s, arr, n): if s == []: for i in ans: if i == -1: return "-1" return "".join(ans) i = s[0] for j in range(n): if ans[j] != -1: continue chk = 0 for k in range(n): if k != j and ans[k] != -1 and ans[k] > i: chk += abs(k - j) if chk == arr[j]: ans[j] = i x = fill(ans, s[1:], arr, n) if x == "-1": ans[j] = -1 else: return x return fill(ans, s[1:], arr, n) t = int(input()) for i in range(t): s = list(input()) n = int(input()) arr = list(map(int, input().split())) ans = [-1] * n s = sorted(s, reverse=True) ans = fill(ans, s, arr, n) print(ans)
FUNC_DEF IF VAR LIST FOR VAR VAR IF VAR NUMBER RETURN STRING RETURN FUNC_CALL STRING VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR STRING ASSIGN VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
t = int(input()) for i in range(t): D = list(input()) d = sorted(list(D), reverse=True) l = int(input()) S = list(map(int, input().split())) s = sorted(S) out = ["x" for x in range(l)] counter = 0 curl = "x" while max(S) > -1: ind = [] while S.count(0) > d.count(d[counter]): counter += 1 for j in range(len(S)): if S[j] == 0: out[j] = d[counter] curl = d[counter] S[j] -= 1 ind.append(j) for indd in ind: for j in range(len(S)): S[j] -= abs(j - indd) counter += len(ind) try: while curl == d[counter]: counter += 1 except: pass print("".join(out))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for _ in range(int(input())): S = sorted(input()) rec = {} s = [] for i in S: if rec.get(i, 0) == 0: s.append(i) rec[i] = rec.get(i, 0) + 1 m = int(input()) b = list(map(int, input().split())) ans = ["" for i in range(m)] while True: pos = [] n = 0 for i in range(m): if b[i] == 0 and ans[i] == "": pos.append(i) n += 1 if n == 0: break t = s.pop() while rec[t] < n: t = s.pop() for i in range(m): if b[i] == 0 and ans[i] == "": ans[i] = t else: for j in pos: b[i] -= abs(i - j) print("".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR STRING ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
def noX(ans): for i in ans: if i == "X": return False return True def updateArr(bArr, updatedThisTime): for i in range(0, len(bArr)): if bArr[i] != 0 or i not in updatedThisTime: continue subValue = 1 j = i + 1 while j < len(bArr): if bArr[j] == 0: j += 1 subValue += 1 continue bArr[j] = bArr[j] - subValue j += 1 subValue += 1 subValue = 1 j = i - 1 while j >= 0: if bArr[j] == 0: j = j - 1 subValue += 1 continue bArr[j] = bArr[j] - subValue j = j - 1 subValue += 1 for _ in range(int(input())): s = list(input()) s.sort(reverse=True) di = {} for i in s: di[i] = di.get(i, 0) + 1 n = int(input()) liStr = input().split() bArr = [] for i in liStr: bArr.append(int(i)) ans = [] for i in range(len(bArr)): ans.append("X") start = 0 sNoDup = [] dup = set() for i in s: if i not in dup: dup.add(i) sNoDup.append(i) while not noX(ans): updatedThisTime = set() newAddCtr = 0 for i in range(len(bArr)): if bArr[i] == 0 and ans[i] == "X": newAddCtr += 1 updatedThisTime.add(i) while di[sNoDup[start]] < newAddCtr: start += 1 for i in range(0, len(bArr)): if bArr[i] == 0 and ans[i] == "X": ans[i] = sNoDup[start] updateArr(bArr, updatedThisTime) start += 1 for p in ans: print(p, end="") print()
FUNC_DEF FOR VAR VAR IF VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR STRING ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
for mda in range(int(input())): s = input() d = dict() for x in s: if d.get(x) == None: d[x] = 1 else: d[x] += 1 n = int(input()) a = list(map(int, input().split())) tt = [""] * n used = [False] * len(s) t = 0 while t != n: zero = [] for i in range(n): if a[i] == 0: zero.append(i) a[i] = -1 ch = "" for x in d.keys(): if d[x] >= len(zero): if ch == "": ch = x elif ord(ch) < ord(x): ch = x for x in d.keys(): if ord(x) >= ord(ch): d[x] = 0 for i in range(n): if a[i] != -1: for x in zero: a[i] -= abs(i - x) for x in zero: tt[x] = ch t += len(zero) for x in tt: print(x, end="") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR IF VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)Β β€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position.
nt = int(input()) while nt > 0: nt -= 1 s = input() n = int(input()) a = [] occurance = {} store = set() for x in s: store.add(x) if x in occurance: occurance[x] += 1 else: occurance[x] = 1 store = sorted(store, reverse=True) final = [] position_of_zero = [] num0 = 0 a = list(map(int, input().split())) for i in range(0, n): final.append(0) if a[i] == 0: num0 += 1 position_of_zero.append(i) for i in range(0, len(store)): if occurance[store[i]] >= num0: for q in position_of_zero: final[q] = store[i] store = store[i + 1 :] break changer = [] while True: if len(position_of_zero) == n: break changer.clear() num0 = 0 for i in range(0, n): if type(final[i]) == str: continue else: final[i] = 0 for k in position_of_zero: final[i] += abs(k - i) if final[i] == a[i]: num0 += 1 changer.append(i) for j in range(0, len(store)): if occurance[store[j]] >= num0: for q in changer: final[q] = store[j] store = store[j + 1 :] position_of_zero.extend(changer) break for z in final: print(z, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≀ i_1 < i_2 < … < i_k ≀ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≀ n, m ≀ 300 000) β€” the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≀ a_i < m) β€” the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
N, M = map(int, input().split()) A = [int(a) for a in input().split()] def chk(k): ret = 0 for i in range(N): a, b = A[i], (A[i] + k) % M if a <= b < ret: return 0 if ret <= a <= b or b < ret <= a: ret = a return 1 l = -1 r = M while r - l > 1: m = (l + r) // 2 if chk(m): r = m else: l = m print(r)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≀ i_1 < i_2 < … < i_k ≀ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≀ n, m ≀ 300 000) β€” the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≀ a_i < m) β€” the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
n, m = map(int, input().split()) a = list(map(int, input().split())) l, r = 0, m - 1 while r > l: mid = l + r >> 1 p = 0 f = False for i in a: if i <= p <= i + mid or i <= p + m <= i + mid: continue if i < p: f = True break p = max(p, i) if f: l = mid + 1 else: r = mid print(r)
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 NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≀ i_1 < i_2 < … < i_k ≀ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each chosen integer i_j. The integer m is fixed for all operations and indices. Here x mod y denotes the remainder of the division of x by y. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers n and m (1 ≀ n, m ≀ 300 000) β€” the number of integers in the array and the parameter m. The next line contains n space-separated integers a_1, a_2, …, a_n (0 ≀ a_i < m) β€” the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 0. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Examples Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 Note In the first example, the array is already non-decreasing, so the answer is 0. In the second example, you can choose k=2, i_1 = 2, i_2 = 5, the array becomes [0,0,1,3,3]. It is non-decreasing, so the answer is 1.
def check(M): now = 0 for i in range(n): if (now - mas[i]) % m > M: if mas[i] > now: now = mas[i] else: return False return True n, m = list(map(int, input().split())) l = -1 r = m mas = list(map(int, input().split())) check(3) while l < r - 1: M = (l + r) // 2 if check(M): r = M else: l = M print(r)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
from sys import stdin def main(): n, m = map(int, stdin.readline().split()) s = [0] * (m + 1) d = [0] * (m + 1) c = [0] * (m + 1) check = [-1] * (n + 1) for i in range(1, m + 1): s[i], d[i], c[i] = map(int, stdin.readline().split()) check[d[i]] = i ans = [0] * (n + 1) ok = True done = [False] * (m + 1) for i in range(1, n + 1): if not ok: break if check[i] != -1: if c[check[i]] == 0: ans[i] = m + 1 done[check[i]] = True else: ok = False break else: candi = [] for j in range(1, m + 1): if s[j] <= i and c[j] > 0 and not done[j]: candi.append(j) if not candi: ans[i] = 0 else: candi.sort(key=lambda x: d[x]) select = candi[0] ans[i] = select c[select] -= 1 if ok: print(*ans[1:]) else: print(-1) main()
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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) prep = [] days = [-1] * n release = [] for i in range(m): s_, d_, c_ = map(int, input().split()) release.append(s_) days[d_ - 1] = i prep.append(c_) rel_on_day = {} for i, r in enumerate(release): if r - 1 in rel_on_day: rel_on_day[r - 1].append(i) else: rel_on_day[r - 1] = [i] ans = [] waiting = set() exam_q = [] for d in days: if d != -1: exam_q.append(d) for i in range(n): if i in rel_on_day: waiting = waiting | set(rel_on_day[i]) if days[i] != -1: if prep[days[i]] == 0: ans.append(m + 1) waiting.remove(days[i]) else: print(-1) exit(0) else: chosen = None for ex in exam_q: if prep[ex] > 0 and ex in waiting: chosen = ex break if not chosen is None: prep[ex] -= 1 ans.append(ex + 1) else: ans.append(0) print(" ".join(list(map(str, ans))))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR NONE VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) x = [] ans = [0] * n for i in range(m): s, d, c = map(int, input().split()) x.append([d - 1, s - 1, c, i + 1]) ans[d - 1] = m + 1 x.sort() for d, s, c, i in x: cnt = 0 while cnt < c: if s == d: print(-1) exit() if ans[s] == 0: ans[s] = i cnt += 1 s += 1 print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
I = lambda: list(map(int, input().split())) n, m = I() dates = [(0) for i in range(n + 1)] data = [] for i in range(m): d = I() + [i + 1] if dates[d[1]]: print(-1) return dates[d[1]] = m + 1 data.append(d) data.sort(key=lambda x: x[1]) for s, d, c, idx in data: x = 0 for i in range(s, d): if x == c: break if not dates[i]: dates[i] = idx x += 1 if x != c: print(-1) return print(*dates[1:])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR LIST BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) l = list() out = [0] * n for i in range(m): s1, d1, c1 = map(int, input().split()) l.append((s1, d1, c1, i)) l.sort(key=lambda x: x[1]) can = True for j in l: out[j[1] - 1] = m + 1 for i in l: c = i[2] for t in range(i[0], i[1]): if out[t - 1] == 0 and c > 0: out[t - 1] = i[3] + 1 c -= 1 if c == 0: break if c != 0: print(-1) can = False break if can is True: print(" ".join(map(str, out)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
def solve(): n, m = map(int, input().split()) deed = [0] * n exams = [] for i in range(m): s, d, c = map(int, input().split()) s -= 1 d -= 1 if deed[d]: return [-1] deed[d] = m + 1 exams.append((d, s, c, i + 1)) for d, s, c, idx in sorted(exams): for i in range(s, d): if deed[i] == 0: deed[i] = idx c -= 1 if c == 0: break else: return [-1] return deed print(*solve())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN LIST NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN LIST NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
a = input().split(" ") a = [int(e) for e in a] isdc = [] for i in range(a[1]): l = input().split(" ") l = [int(e) for e in l] isdc.append([i + 1, l[0] - 1, l[1] - 1, l[2]]) l = [0] * a[0] for e in isdc: l[e[2]] = len(isdc) + 1 isdc = sorted(isdc, key=lambda one: one[2]) for exam in isdc: day = exam[1] while day < exam[2]: if exam[3] == 0: break if l[day] == 0: l[day] = exam[0] exam[3] -= 1 day += 1 if exam[3] != 0: print(-1) exit() for e in l: print(e, end=" ")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR NUMBER IF VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) arr = [] for i in range(1, m + 1): s, d, c = map(int, input().split()) arr.append((d, s, c, i)) arr.sort() ans = [0] * (n + 1) for d, s, c, i in arr: j = s ef = False for k in range(c): while j < d and ans[j] != 0: j += 1 if j >= d: ef = True break ans[j] = i if ef: print(-1) break if ans[d] == 0: ans[d] = m + 1 else: print(-1) break else: print(*ans[1:])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) c = [] for i in range(m): c.append([int(x) for x in input().split()]) c[i].append(i + 1) a = [0] * n c.sort(key=lambda x: x[1]) for i in c: a[i[1] - 1] = m + 1 x = i[2] for j in range(i[0] - 1, i[1]): if x == 0: break if a[j] == 0: a[j] = i[3] x -= 1 if x > 0: print(-1) exit() print(*a)
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
class exam: def __init__(self, tickets, ex_day, prep_days, index): self.tick = tickets - 1 self.ex_day = ex_day - 1 self.prep_days = prep_days self.index = index def task(): n, m = [int(x) for x in input().split(" ")] exs = list() for i in range(m): a, b, c = [int(x) for x in input().split(" ")] exs.append(exam(a, b, c, i + 1)) s = sum(x.prep_days for x in exs) if s > n - m: print(-1) return -1 exs = sorted(exs, key=lambda x: -x.tick) exdays = [x.ex_day for x in exs] days = [(m + 1 if i in exdays else 0) for i in range(0, n)] for ex in exs: d = ex.ex_day offs = 1 for i in range(ex.prep_days): cont = True while cont: if d - offs < ex.tick: print(-1) return if days[d - offs] == 0: days[d - offs] = ex.index cont = False else: offs += 1 print(" ".join(str(x) for x in days)) task()
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
import sys n, m = [int(c) for c in input().split()] s = [] d = [] c = [] studying = [] for _ in range(m): x, y, z = [int(w) for w in input().split()] s.append(x) d.append(y) c.append(z) plan = [] for i in range(1, n + 1): if i in s: indices = [k for k, x in enumerate(s) if x == i] for idx in indices: studying.append((d[idx], idx)) studying.sort() if i in d: idx = d.index(i) if (i, idx) in studying: print(-1) sys.exit() else: plan.append(m + 1) else: if not studying: plan.append(0) continue idx = studying[0][1] plan.append(idx + 1) c[idx] -= 1 if c[idx] == 0: studying = studying[1:] for i in plan: print(i, end=" ") print()
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) exams = [] for i in range(m): e = list(map(int, input().split())) e.append(0) e.append(i + 1) exams.append(e) exams.sort() now, have, f, ans, c = 0, [], True, [], 0 for i in range(1, n + 1): while now < m and exams[now][0] == i: have.append(exams[now][1:]) now += 1 have.sort() if c < len(have) and have[c][0] == i: if have[c][1] == have[c][2]: ans.append(m + 1) c += 1 else: f = False break else: j = 0 while j < len(have) and have[j][1] == have[j][2]: j += 1 if j == len(have): ans.append(0) else: ans.append(have[j][3]) have[j][2] += 1 if f: for i in range(n): print(ans[i], end=" ") else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
a, b = map(int, input().split()) m = [0] * a m1 = [] for i in range(b): m1.append(list(map(int, input().split()))) m[m1[-1][1] - 1] = b + 1 m1[-1].append(i + 1) m1.sort(key=lambda x: x[1]) for i in m1: c = i[2] for j in range(i[0] - 1, i[1]): if m[j] == 0: m[j] = i[3] c -= 1 if c == 0: break if j + 1 == len(m): break if c != 0: m1.sort() m = [0] * a for i in m1: m[i[1] - 1] = b + 1 for i in m1: c = i[2] for j in range(i[0] - 1, i[1]): if m[j] == 0: m[j] = i[3] c -= 1 if c == 0: break if j + 1 == len(m): break if c != 0: m = False break break if m == False: print(-1) else: print(*m)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) x = [0] * n t = [] for i in range(m): s, d, c = map(int, input().split()) x[d - 1] = m + 1 t.append((d - 1, s - 1, c, i + 1)) t.sort() for d, s, c, i in t: for k in range(s, d): if not x[k]: x[k] = i c -= 1 if c == 0: break if c > 0: print(-1) break else: print(" ".join(map(str, x)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
import sys n, m = map(int, input().split()) sdc = [tuple(map(int, input().split())) for _ in range(m)] isdc = list(enumerate(sdc)) isdc.sort(key=lambda _: _[1][1]) a = [(0) for _ in range(n + 1)] for i, sdc in isdc: s, d, c = sdc a[d] = m + 1 for i, sdc in isdc: s, d, c = sdc for k in range(s, d): if a[k] == 0 and c > 0: a[k] = i + 1 c -= 1 if c > 0: print(-1) sys.exit(0) print(*a[1:])
IMPORT 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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
aa = input() n, m = [int(s) for s in aa.split(" ")] exams = [] res = [0] * n for i in range(m): aa = input() s, d, c = [int(s) for s in aa.split(" ")] el = {"s": s - 1, "d": d - 1, "c": c} exams.append(el) res[d - 1] = m + 1 for i in range(n - 1, -1, -1): if res[i] > 0: continue exam_num = -1 min_zapas = n + 1 for j in range(m): if exams[j]["s"] <= i and i < exams[j]["d"] and exams[j]["c"] > 0: zapas = i - exams[j]["s"] - exams[j]["c"] if zapas < min_zapas: min_zapas = zapas exam_num = j if zapas < 0: break if exam_num == -1: continue res[i] = exam_num + 1 exams[exam_num]["c"] = exams[exam_num]["c"] - 1 possible = True for i in range(m): if exams[i]["c"] > 0: possible = False break if possible: print(" ".join([str(x) for x in res])) else: print(-1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR DICT STRING STRING STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR VAR STRING VAR VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR STRING VAR VAR STRING IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING BIN_OP VAR VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
def solve(n, m, ss, dd, cc): date = [-1] * (n + 1) for j, d in enumerate(dd): date[d] = j start = [[] for _ in range(n + 1)] for j, d in enumerate(ss): start[d].append(j) preps = [] for i in range(1, n + 1): if start[i]: preps += start[i] u = date[i] if u >= 0: j = u if cc[j] > 0: return -1 date[i] = m + 1 elif not preps: date[i] = 0 else: min_d = min(dd[j] for j in preps) for j in preps: if dd[j] == min_d: break date[i] = j + 1 cc[j] -= 1 if cc[j] == 0: preps.remove(j) return date[1:] def main(): n, m = [int(_) for _ in input().split()] s = [] d = [] c = [] for i in range(m): s_, d_, c_ = [int(_) for _ in input().split()] s.append(s_) d.append(d_) c.append(c_) plan = solve(n, m, s, d, c) if isinstance(plan, list): print(" ".join(map(str, plan))) else: print(-1) main()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) ans = [0] * n f = 0 li = [] for i in range(m): a, b, c = map(int, input().split()) li.append((b, a, c, i + 1)) li.sort() for i in range(m): b, a, c, k = li[i] ans[b - 1] = m + 1 j = a - 1 cnt = 0 while j < b and cnt < c: if ans[j] == 0: ans[j] = k cnt += 1 j += 1 if cnt != c: f = 1 if f: print(-1) else: print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) SDC = [] ans = [0] * n D = {} for i in range(m): s, d, c = map(int, input().split()) SDC.append((s, d, c)) ans[d - 1] = m + 1 D[d - 1] = i C = [0] * m for i in range(n): if ans[i] == 0: e = 10**18 k = -1 for j in range(m): s, d, c = SDC[j] if d <= e and s <= i + 1 and i + 1 <= d and C[j] < c: e = d k = j if k != -1: ans[i] = k + 1 C[k] += 1 else: j = D[i] s, d, c = SDC[j] if C[j] < c: print(-1) exit() print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
from sys import stdin, stdout def rint(): return map(int, stdin.readline().split()) n, m = rint() s = [(0) for i in range(m)] d = [(0) for i in range(m)] c = [(0) for i in range(m)] for i in range(m): s[i], d[i], c[i] = rint() s_in_day = [set() for i in range(n + 1)] for i in range(m): day = s[i] s_in_day[day].add(i) d_in_day = [(-1) for i in range(n + 1)] for i in range(m): day = d[i] d_in_day[day] = i di_sorted = [(0) for i in range(m)] di_sorted.sort(key=lambda x: d[i]) ans = [(0) for i in range(n + 1)] candi_exam = set() for day in range(1, n + 1): for exam in s_in_day[day]: candi_exam.add(exam) if d_in_day[day] != -1: exam = d_in_day[day] if c[exam] != 0: print(-1) exit() ans[day] = m + 1 if exam in candi_exam: candi_exam.remove(exam) continue if len(candi_exam) == 0: ans[day] = 0 continue min_d_day = 101 busy_exam = 0 for exam in candi_exam: if d[exam] < min_d_day: busy_exam = exam min_d_day = d[exam] ans[day] = busy_exam + 1 c[busy_exam] -= 1 if c[busy_exam] == 0: candi_exam.remove(busy_exam) for i in range(m): if c[i] != 0: print(-1) exit() print(*ans[1:])
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) array = [0] * (n + 1) lst = [] for x in range(m): s, d, c = map(int, input().split()) lst.append((d, s, c, x + 1)) lst.sort() i = 0 flag = True for d, s, c, p in lst: x = s while c > 0 and x < d: if array[x] == 0: array[x] = p c -= 1 x += 1 if c != 0: flag = False break else: array[d] = m + 1 if flag: print(*array[1:]) else: print(-1)
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 FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
[n, m] = [int(x) for x in input().split()] I = 0 S = 1 D = 2 C = 3 exses = [[0, 0, 0, 0]] pops = [0] * (n + 1) i = 1 while i <= m: [s, d, c] = [int(x) for x in input().split()] exses.append([i, s, d, c]) pops[d] = i i += 1 i = 1 result = [0] * (n + 1) fail = False while i <= n: if pops[i] != 0 and exses[pops[i]][C] == 0: result[i] = m + 1 elif pops[i] != 0 and exses[pops[i]][C] > 0: fail = True break else: ex = None min = n + 1 for e in exses: if e[D] > i and e[S] <= i and e[D] < min and e[C] > 0: min = e[D] ex = e if ex == None: result[i] = 0 else: result[i] = ex[I] ex[C] -= 1 i += 1 if fail: print(-1) else: for x in result[1:]: print(x, end=" ")
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) s = [0] * (m + 1) d = [0] * (m + 1) c = [0] * (m + 1) ans = [0] * (n + 1) for i in range(1, m + 1): s[i], d[i], c[i] = map(int, input().split()) ans[d[i]] = m + 1 for i in range(1, n + 1): if ans[i] != 0: continue mn, pos = 1000, 0 for j in range(1, m + 1): if c[j] > 0 and s[j] <= i and i < d[j] and d[j] < mn: mn = d[j] pos = j if mn < 1000: ans[i] = pos c[pos] -= 1 for i in range(1, m + 1): if c[i] > 0: print(-1) exit(0) print(*ans[1:])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
def ke(i): return b[i] n, m = map(int, input().split()) a = [0] * m b = [0] * m c = [0] * m e = [] ans = [0] * n for i in range(m): a[i], b[i], c[i] = map(int, input().split()) ans[b[i] - 1] = m + 1 e.append(i) e.sort(key=ke) for i in range(m): k = 0 for j in range(a[e[i]] - 1, b[e[i]] - 1): if ans[j] == 0: ans[j] = e[i] + 1 k += 1 if k == c[e[i]]: break if k != c[e[i]]: print(-1) exit(0) for i in ans: print(i, end=" ")
FUNC_DEF RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
R = lambda: map(int, input().split()) n, m = R() a = [] for i in range(m): s, d, c = R() a.append([d, s, c, i + 1]) a.sort() r = [0] * (n + 1) for i in range(m): r[a[i][0]] = m + 1 for i in range(m): for j in range(a[i][1], a[i][0]): if a[i][2] == 0: break elif r[j] == 0: r[j] = a[i][3] a[i][2] -= 1 if a[i][2]: print(-1) exit() print(*r[1:])
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
class exam1: def __init__(self, ind, ee): self.ind = ind self.ee = ee class exam: def __init__(self, qq, ee, pp, ind): self.qq = qq self.ee = ee self.pp = pp self.ind = ind yoar = list(map(int, input().split())) n = yoar[0] m = yoar[1] ans = [] examar = [] paperar = [] paperdayar = [] for i in range(n): ans.append(0) paperdayar.append([]) for i in range(m): yoar = list(map(int, input().split())) qq = yoar[0] - 1 ee = yoar[1] - 1 pp = yoar[2] examar.append(exam(qq, ee, pp, i)) ans[ee] = examar[-1] paperdayar[qq].append(exam1(i, ee)) flag = 0 for i in range(n): if paperdayar[i] != []: for j in paperdayar[i]: paperar.append(j) paperar.sort(key=lambda exam1: exam1.ee) if type(ans[i]) == exam: if examar[ans[i].ind].pp > 0: print(-1) flag = 1 break elif paperar == []: ans[i] = 0 else: ans[i] = paperar[0].ind + 1 examar[paperar[0].ind].pp -= 1 if examar[paperar[0].ind].pp == 0: del paperar[0] if flag == 0: for i in range(n): if type(ans[i]) == exam: print(m + 1, end=" ") else: print(ans[i], end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) L = [] for i in range(m): a, b, c = map(int, input().split()) L.append([b, a, c, i]) L.sort() M = [(-1) for i in range(n + 1)] for i in range(m): M[L[i][0]] = m + 1 for i in range(1, n + 1): if M[i] == -1: f = False for j in range(m): if L[j][1] <= i and L[j][2] > 0 and i < L[j][0]: M[i] = L[j][3] + 1 L[j][2] -= 1 f = True break if not f: M[i] = 0 f = True for j in range(m): if L[j][2] > 0: f = False if f: print(*M[1:]) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$. There are three values about each exam: $s_i$ β€” the day, when questions for the $i$-th exam will be published, $d_i$ β€” the day of the $i$-th exam ($s_i < d_i$), $c_i$ β€” number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$. It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days. Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. -----Input----- The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ β€” the number of days and the number of exams. Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ β€” the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. -----Output----- If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it). Assume that the exams are numbered in order of appearing in the input, starting from $1$. If there are multiple schedules, print any of them. -----Examples----- Input 5 2 1 3 1 1 5 1 Output 1 2 3 0 3 Input 3 2 1 3 1 1 2 1 Output -1 Input 10 3 4 7 2 1 10 3 8 9 1 Output 2 2 2 1 1 0 4 3 4 4 -----Note----- In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams. In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
n, m = map(int, input().split()) exams = [] examday = set() for i in range(m): s, d, c = map(int, input().split()) exams.append((i, s, d, c)) examday.add(d) prep = [0] * m s_exams = exams[:] s_exams.sort(key=lambda x: x[2]) ans = [] for day in range(1, n + 1): if day in examday: ans.append(m + 1) continue for i, s, d, c in s_exams: if day < s or day > d: continue if prep[i] == c: continue prep[i] += 1 ans.append(i + 1) break else: ans.append(0) for i in range(m): if prep[i] < exams[i][3]: print(-1) exit(0) print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR