description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
from sys import stdin input = stdin.readline for xoxo in range(1): n = int(input()) s = input() a = [] b = [] c = [0] * 26 d = [] for i in range(26): a.append([]) b.append([]) d.append([]) for i in range(n): c[ord(s[i]) - 97] += 1 for i in range(n): kk = 0 for j in range(ord(s[i]) - 97): kk += c[j] a[ord(s[i]) - 97].append(i + kk) d[ord(s[i]) - 97].append(i) c[ord(s[i]) - 97] -= 1 s2 = sorted(s) s1 = "" for i in range(1, n + 1): s1 += s2[i] for i in range(n): b[ord(s1[i]) - 97].append(i) ans = [-1] * n flag = 0 for i in range(26): if flag == 1: break for j in range(len(a[i])): if flag == 1: break if b[i][j] >= a[i][j]: continue else: if ans[d[i][j]] != 0: ans[d[i][j]] = 1 else: flag = 1 break p = ans[d[i][j]] for k in range(d[i][j] - 1, d[i][j] - 1 - (a[i][j] - b[i][j]), -1): if i >= ord(s[k]) - 97: continue else: if ans[k] == -1: ans[k] = (p + 1) % 2 if ans[k] == p: flag = 1 break if flag == 1: print("NO") else: print("YES") for i in range(n): if ans[i] == -1: ans[i] = 0 print(ans[i], end="")
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
class Node: idx = None letter = None color = False def __init__(self, idx, letter): assert idx >= 0 and letter self.dependencies = [] self.dependents = [] self.idx = idx self.letter = letter def __repr__(self): return f"Node<{self.idx, self.letter}>" def add_dependency(self, node): assert type(node) is Node self.dependencies.append(node) node.dependents.append(self) input() string = input() nodes = [Node(*x) for x in enumerate(string)] def check_dep_depth(node, max_depth=1): if max_depth < 0: return False for n in node.dependencies: n.color = not node.color if not check_dep_depth(n, max_depth - 1): return False return True for idx, n1 in enumerate(nodes): if idx == len(nodes) - 1: continue for n2 in nodes[idx + 1 :]: if n2.letter < n1.letter: n1.add_dependency(n2) for n in nodes: if not check_dep_depth(n, 1): print("NO") exit() print("YES") print("".join(str(int(n.color)) for n in nodes))
CLASS_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER FUNC_DEF VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN STRING VAR VAR STRING FUNC_DEF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() last, last1 = "a", "a" ans = "" flag = 1 for i in s: if i < last and i < last1: flag = 0 break elif i >= last: last = i ans += "0" else: last1 = i ans += "1" if flag: print("YES") print(ans) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR STRING STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() used = [0] * n col = [-1] * n a = [(c, i) for i, c in enumerate(s)] a.sort() for _, i in a: tmp = -1 for j in range(i): if used[j] or col[j] == -1: continue if tmp == -1: tmp = col[j] if tmp != col[j]: print("NO") exit() if tmp == -1 and col[i] == -1: tmp = 0 elif tmp == -1 and col[i] != -1: tmp = 1 ^ col[i] elif tmp != -1 and col[i] == -1: pass elif tmp == col[i]: print("NO") exit() col[i] = 1 ^ tmp for j in range(i): if used[j]: continue col[j] = tmp used[i] = 1 ans = "".join(map(str, col)) b = [c for c in s] for i in range(1, n): while i and b[i - 1] > b[i]: if col[i - 1] == col[i]: print("NO") exit() b[i - 1], b[i] = b[i], b[i - 1] col[i - 1], col[i] = col[i], col[i - 1] i -= 1 print("YES") print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
import sys f_in = sys.stdin f_out = sys.stdout num = int(f_in.readline()) strings = f_in.readline() grp = [[], [], []] warning = 0 for i in range(0, num): if grp[0] == [] or grp[0][-1] <= strings[i]: grp[0].append(strings[i]) grp[2].append("0") elif grp[1] == [] or grp[1][-1] <= strings[i]: grp[1].append(strings[i]) grp[2].append("1") else: warning = 1 ans = "".join(grp[2]) if warning == 1: f_out.write("NO\n") else: f_out.write("YES\n") f_out.write(ans + "\n") f_in.close() f_out.close()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST LIST LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER LIST VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER STRING IF VAR NUMBER LIST VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
from sys import stdin, stdout input = stdin.readline def im(): return map(int, input().split()) def ii(): return int(input()) def il(): return list(map(int, input().split())) def ins(): return input()[:-1] n = ii() st = ins() lis = [0] * (n + 1) lis[1] = st[0] r = 1 ans = [1] for i in st[1:]: if i >= lis[1]: lis[1] = i ans.append(1) elif i < lis[r]: r += 1 lis[r] = i ans.append(r) else: left = 1 right = r while left < right: mid = (left + right) // 2 if i < lis[mid]: left = mid + 1 else: right = mid ans.append(left) lis[left] = i print(r) print(*ans)
ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR 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 RETURN FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) l = list(input()) k = [(0) for i in range(n)] z = [(0) for i in range(n)] x = sorted(l) r = "" for t in x: r += t for t in range(1, n): if l[t] < l[t - 1]: k[t] = k[t - 1] ^ 1 z[t] = k[t] i = t while i > 0 and l[i] < l[i - 1]: if k[i] == k[i - 1]: break l[i], l[i - 1] = l[i - 1], l[i] k[i], k[i - 1] = k[i - 1], k[i] i -= 1 s = "" for t in l: s += t if s == r: print("YES") print(*z, sep="") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
from sys import stdin, stdout t = 1 for _ in range(t): n = int(input()) s = input() ans = [str(0)] last = s[0] color = 0 for i in s[1:]: if i >= last: last = i ans.append(str(color)) else: ans.append(str(color ^ 1)) val1 = [] val2 = [] for i in range(n): if int(ans[i]): val1.append(s[i]) else: val2.append(s[i]) if val1 == sorted(val1) and val2 == sorted(val2): print("YES") print("".join(ans)) else: print("NO")
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = str(input()) ls = [] b_end = False s_out = "" for x in range(0, len(s)): tmp_tab = [s[x], -1] ls.append(tmp_tab) ls[0][1] = 0 for x in range(1, len(ls)): tmp_val = ls[x] tmp_lit = ls[x][0] tmp_num = 0 tmp_war = [] for y in range(x - 1, -1, -1): if ls[y][0] > tmp_lit: tmp_war.append(ls[y][1]) if not tmp_war: ls[x][1] = 0 elif 0 not in tmp_war: ls[x][1] = 0 elif 1 not in tmp_war: ls[x][1] = 1 else: b_end = True break if b_end == True: print("NO") else: for x in range(0, len(ls)): s_out += str(ls[x][1]) print("YES") print(s_out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER NUMBER IF NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() col = [-1] * n ans = "YES" inversions = False used = [False] * n def solve(i): global ans global inversions used[i] = True for j in range(n): if (ord(s[j]) - ord(s[i])) * (j - i) < 0: if col[j] != -1: if col[i] == col[j]: ans = "NO" else: col[j] = col[i] ^ 1 if not used[j]: solve(j) for i in range(n): if not used[i]: col[i] = 0 solve(i) print(ans) if ans == "YES": print(*col, sep="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() p = [-1] * n dd = [0] * 26 p[0] = 1 dd[ord(s[0]) - 97] = 1 for i in range(1, n): f = 0 mxj = 0 for j in range(26): if dd[j] != 0 and j > ord(s[i]) - 97: f = 1 mxj = max(mxj, dd[j]) p[i] = mxj + 1 dd[ord(s[i]) - 97] = p[i] print(max(p)) for i in p: print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = str(input()) lit = ["Z"] * 26 wyn = "" m = -1 for x in s: for y in range(26): if lit[y] <= x: wyn += str(y + 1) + " " lit[y] = x if y + 1 >= m: m = y + 1 break print(m) print(wyn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) f = input() t = "" a = "a" b = "a" h = 0 for j in f: if ord(a[-1]) <= ord(j): a += j t += "0" elif ord(b[-1]) <= ord(j): b += j t += "1" else: print("NO") h += 1 break if h == 0: print("YES") print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
def hello_nigga(): n = int(input()) s = input() s0 = [] l_s0 = "a" s1 = [] l_s1 = "a" t = 1 for i in range(n): if s[i] >= l_s1: l_s1 = s[i] s1.append(i) elif s[i] >= l_s0: l_s0 = s[i] else: t = 0 print("NO") break if t: ans = ["0"] * n for i in range(len(s1)): ans[s1[i]] = "1" print("YES") print("".join(ans)) if "compiler is alive": if "it has sence" and "the wether is nice": hello_nigga()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF STRING IF STRING STRING EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) a = list(input()) max_color = 0 colors = [(0) for _ in range(n)] color_to_max = [-1] for i in range(n): max_ok_color = -1 for color in range(max_color + 1): if ord(a[i]) >= color_to_max[color]: if max_ok_color == -1 or color_to_max[color] > color_to_max[max_ok_color]: max_ok_color = color if max_ok_color == -1: colors[i] = max_color + 1 max_color += 1 color_to_max.append(ord(a[i])) else: colors[i] = max_ok_color color_to_max[max_ok_color] = ord(a[i]) print(max_color + 1) print(" ".join([str(item + 1) for item in colors]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() a = [] for i in range(n): a.append(ord(s[i])) visit = [(0) for i in range(n)] i, flag = 1, 0 rama = [(0) for i in range(n)] while i < n: if a[i] < a[i - 1]: visit[i] = 1 rama[i] = visit[i] temp = a[i] tempu = visit[i] j = i while j > 0: if temp < a[j - 1]: if tempu != visit[j - 1]: a[j] = a[j - 1] visit[j] = visit[j - 1] else: flag = -1 break else: break j -= 1 if flag == -1: break visit[j] = tempu a[j] = temp i += 1 if flag == -1: print("NO") else: print("YES") for i in rama: print(i, end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() alpha = [0] * 26 col = [] for i in range(0, len(s)): c = 0 for j in range(ord(s[i]) - 97 + 1, 26): c = max(c, alpha[j]) col.append(c + 1) alpha[ord(s[i]) - 97] = c + 1 print(max(col)) print(*col)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
def main(): n = int(input()) s = list(input()) colors = [-1] * n for i in range(len(s)): if colors[i] == -1: colors[i] = 0 for j in range(i + 1, n): if s[j] < s[i]: if colors[j] == colors[i]: print("NO") return if colors[j] == -1: if colors[i] == 0: colors[j] = 1 else: colors[j] = 0 print("YES") for i in colors: print(i, end="") main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) ok = True string = input() seq1 = "a" seq2 = "a" ans = "0" for i in string: if i >= seq1: seq1 = i ans += "0" continue if i >= seq2: seq2 = i ans += "1" else: ok = False if ok == True: print("YES") print(ans[1:]) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR STRING IF VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) c = list(input()) for i in range(n): c[i] = [c[i], -1] i = 0 flag = True while flag and i < n: j = i + 1 while j < n and c[i][1] == -1: if c[j][0] < c[i][0]: if c[j][1] != -1: c[i][1] = (c[j][1] + 1) % 2 j = n + 1 j += 1 if c[i][1] == -1: c[i][1] = 0 for j in range(i + 1, n): if c[j][0] < c[i][0]: if c[j][1] == -1: c[j][1] = (c[i][1] + 1) % 2 elif (c[j][1] + c[i][1]) % 2 != 1: flag = False if not flag: break i += 1 ans = [] for i in c: ans.append(i[1]) if flag: print("YES") print(*ans, sep="") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
def main(): n = int(input()) s = input() fst_lst = [s[0]] snd_lst = [] ans = "1" for i in range(1, len(s)): if fst_lst[-1] <= s[i]: fst_lst.append(s[i]) ans += "1" else: snd_lst.append(s[i]) ans += "0" if sorted(snd_lst) == snd_lst: print("YES") print(ans) else: print("NO") main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
from itertools import tee def line(): return map(int, input().split()) def num(): return int(input()) def pairwise(it): a, b = tee(it) next(b, None) return zip(a, b) n = num() s = input() qq = [] ans = [] for c in s: for j, q in enumerate(qq): if c >= q: qq[j] = c ans.append(j + 1) break else: qq.append(c) ans.append(len(qq)) print(len(qq)) print(" ".join(map(str, ans)))
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NONE RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
def hello_nigga(): n = int(input()) s = input() LAST = {(1): "a"} COLORS = {(1): []} cnum = 1 for i in range(n): nice = 0 for j in range(1, cnum + 1): if s[i] >= LAST[j]: LAST[j] = s[i] COLORS[j].append(i) nice = 1 break if not nice: cnum += 1 LAST[cnum] = s[i] COLORS[cnum] = [i] ans = [0] * n for i in range(1, cnum + 1): for j in range(len(COLORS[i])): ans[COLORS[i][j]] = i print(cnum) print(*ans) if "compiler is alive": if "it has sence" and "the wether is nice": hello_nigga()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER STRING ASSIGN VAR DICT NUMBER LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF STRING IF STRING STRING EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
def r(t=int): return list(map(t, input().split())) def ri(t=int): return t(input()) def cd(it): ret_val = dict() for v in it: ret_val[v] = ret_val.get(v, 0) + 1 return ret_val n = ri() ans = [0] * n alphabet = "abcdefghijklmnopqrstuvwxyz" a = [alphabet.index(ai) for ai in input()] pa = [] for i, ai in enumerate(a): m = 100 for h, p in enumerate(pa): p1 = ai - p if p <= ai and p1 < m: f = h + 1 m = p1 if m == 100: pa.append(ai) f = len(pa) ans[i] = f pa[f - 1] = ai print(len(pa)) print(" ".join(str(i) for i in ans))
FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) a = list(input()) colors = [(0) for _ in range(n)] flag_yes = True last0, last1 = -1, -1 for i in range(n): if ord(a[i]) >= last0: if ord(a[i]) >= last1: if last0 >= last1: last0 = ord(a[i]) colors[i] = 0 else: last1 = ord(a[i]) colors[i] = 1 else: last0 = ord(a[i]) colors[i] = 0 elif ord(a[i]) >= last1: last1 = ord(a[i]) colors[i] = 1 else: flag_yes = False break if flag_yes: print("YES") print("".join([str(item) for item in colors])) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) letters = list(input()) flag = True flagarr = [0] * n binarr = {x: [0, 0] for x in range(26)} for i in range(n): for k in range(i + 1, n): if ord(letters[k]) % 97 < ord(letters[i]) % 97: flagarr[k] = 1 if flagarr[i]: flag = False break if not flag: break for letter in range(n): if not flag: break if flagarr[letter]: binarr[ord(letters[letter]) % 97][1] += 1 else: binarr[ord(letters[letter]) % 97][0] += 1 if not flag: print("NO") else: print("YES") print("".join(str(i) for i in flagarr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
import sys reader = (line.rstrip() for line in sys.stdin) input = reader.__next__ def ceil(tails, L, R, key): while L + 1 < R: m = (L + R) // 2 if key < tails[m]: L = m else: R = m return R def LIS(a, n): tails = [0] * (n + 1) tails[0] = a[0] seq_len = 1 for i in range(1, n): if a[i] > tails[0]: tails[0] = a[i] ans.append(1) elif a[i] < tails[seq_len - 1]: tails[seq_len] = a[i] seq_len += 1 ans.append(seq_len) else: pos = ceil(tails, -1, seq_len - 1, a[i]) tails[pos] = a[i] ans.append(pos + 1) return seq_len n = int(input()) s = input() ans = [1] res = LIS(s, n) print(res) print(*ans)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s2 = input() k = 0 s = [] l = {} for i in range(n): s.append(i) l[i] = -1 for i in range(n): m = "zz" index = -1 for j in range(k, n): if index == -1: index = j m = s2[j] if s2[j] < m: index = j m = s2[j] col = -1 for j in range(k, index): if col == -1: col = l[s[j]] if col == 0: if l[s[j]] == 1: print("NO") exit(0) if col == 1: if l[s[j]] == 0: print("NO") exit(0) if l[s[index]] == -1: if col == -1: l[s[index]] = 1 for j in range(k, index): l[s[j]] = 0 if col == 0: l[s[index]] = 1 for j in range(k, index): l[s[j]] = 0 if col == 1: l[s[index]] = 0 for j in range(k, index): l[s[j]] = 1 else: if l[s[index]] == col: print("NO") exit(0) if col == -1: if l[s[index]] == 0: for j in range(k, index): l[s[j]] = 1 if l[s[index]] == 1: for j in range(k, index): l[s[j]] = 0 s = s[:k] + [s[index]] + s[k:index] + s[index + 1 :] s2 = s2[:k] + s2[index] + s2[k:index] + s2[index + 1 :] k += 1 ss = "" for i in range(n): ss += str(l[i]) print("YES") print(ss)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR LIST VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
def binSearch(a, n): i, j = 0, len(a) - 1 while i != j: if (j - i) % 2 == 1: if a[i + (j - i) // 2] > n: i = i + (j - i) // 2 + 1 else: j = i + (j - i) // 2 elif a[i + (j - i) // 2] > n: i = i + (j - i) // 2 else: j = i + (j - i) // 2 return i n, s = int(input()), input() k = "aa" ans = "" maxColor = 1 for symb in s: i = binSearch(k, symb) ans += str(i + 1) + " " if i + 1 > maxColor: maxColor = i + 1 k += "a" k = k[:i] + symb + k[i + 1 :] print(maxColor) print(ans)
FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() kol = [-1] * n cur = 1 ind = 0 while ind < n: if kol[ind] != -1: ind += 1 else: kol[ind] = cur best = s[ind] for i in range(ind + 1, n): if kol[i] != -1: continue elif s[i] >= best: kol[i] = cur best = s[i] ind += 1 cur += 1 print(max(kol)) for i in range(n): print(kol[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() arr = ["a" for i in range(n)] l = [] for i in s: for j in range(n): if i >= arr[j]: arr[j] = i l.append(j + 1) break print(max(l)) print(*l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
import sys input = sys.stdin.readline class SegmentTree: def __init__(self, a): self.op = lambda a, b: max(a, b) self.e = 0 self.n = len(a) self.lv = (self.n - 1).bit_length() self.size = 2**self.lv self.data = [self.e] * (2 * self.size - 1) self._check = lambda x, acc: acc >= x self._acc = self.e self.initialize(a) def initialize(self, a): for i in range(self.n): self.data[self.size + i - 1] = a[i] for i in range(self.size - 2, -1, -1): self.data[i] = self.op(self.data[i * 2 + 1], self.data[i * 2 + 2]) def update(self, k, x): k += self.size - 1 self.data[k] = x while k > 0: k = (k - 1) // 2 self.data[k] = self.op(self.data[2 * k + 1], self.data[2 * k + 2]) def fold(self, l, r): L = l + self.size R = r + self.size s = self.e while L < R: if R & 1: R -= 1 s = self.op(s, self.data[R - 1]) if L & 1: s = self.op(s, self.data[L - 1]) L += 1 L >>= 1 R >>= 1 return s def _bisect_forward(self, x, start, k): if k >= self.size - 1: self._acc = self.op(self._acc, self.data[k]) if self._check(x, self._acc): return k - (self.size - 1) else: return -1 width = 2 ** (self.lv - (k + 1).bit_length() + 1) mid = (k + 1) * width + width // 2 - self.size if mid <= start: return self._bisect_forward(x, start, 2 * k + 2) tmp_acc = self.op(self._acc, self.data[k]) if start <= mid - width // 2 and not self._check(x, tmp_acc): self._acc = tmp_acc return -1 vl = self._bisect_forward(x, start, 2 * k + 1) if vl != -1: return vl return self._bisect_forward(x, start, 2 * k + 2) def bisect_forward(self, x, start=None): if start: ret = self._bisect_forward(x, start, 0) else: ret = self._bisect_forward(x, 0, 0) self._acc = self.e return ret def _bisect_backward(self, x, start, k): if k >= self.size - 1: self._acc = self.op(self._acc, self.data[k]) if self._check(x, self._acc): return k - (self.size - 1) else: return -1 width = 2 ** (self.lv - (k + 1).bit_length() + 1) mid = (k + 1) * width + width // 2 - self.size if mid >= start: return self._bisect_backward(x, start, 2 * k + 1) tmp_acc = self.op(self._acc, self.data[k]) if start > mid + width // 2 and not self._check(x, tmp_acc): self._acc = tmp_acc return -1 vl = self._bisect_backward(x, start, 2 * k + 2) if vl != -1: return vl return self._bisect_backward(x, start, 2 * k + 1) def bisect_backward(self, x, start=None): if start: ret = self._bisect_backward(x, start, 0) else: ret = self._bisect_backward(x, self.n, 0) self._acc = self.e return ret n = int(input()) s = input().rstrip() array = [0] * n ST = SegmentTree(array) event = [] for i, ch in enumerate(s): event.append((ch, i)) event.sort(reverse=True) ans = [0] * n for ch, index in event: val = ST.fold(0, index) + 1 ans[index] = val ST.update(index, val) print(max(ans)) print(*ans)
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR FUNC_CALL BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF NONE IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR FUNC_CALL BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF NONE IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
import sys class SegmentTree: def __init__(self, n, op, e): self.n = n self.op = op self.e = e self.size = 2 ** (n - 1).bit_length() self.node = [self.e] * (2 * self.size) def built(self, array): for i in range(self.n): self.node[self.size + i] = array[i] for i in range(self.size - 1, 0, -1): self.node[i] = self.op(self.data[i << 1], self.data[(i << 1) + 1]) def update(self, i, val): i += self.size self.node[i] = val while i > 1: i >>= 1 self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1]) def get_val(self, l, r): l, r = l + self.size, r + self.size res = self.e while l < r: if l & 1: res = self.op(self.node[l], res) l += 1 if r & 1: r -= 1 res = self.op(self.node[r], res) l, r = l >> 1, r >> 1 return res input = sys.stdin.readline n = int(input()) s = input() st = SegmentTree(n, max, 0) s = sorted([(s[i], i) for i in range(n)])[::-1] cnt = 0 for char, i in s: min_ = st.get_val(0, i) st.update(i, min_ + 1) cnt = max(cnt, min_ + 1) print(cnt) print(*st.node[st.size : st.size + n])
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
while True: try: n = int(input()) s = input() a = "" b = "" a += s[0] ans = "0" for i in range(1, n): if s[i] >= a[-1]: a += s[i] ans += "0" elif b == "" or s[i] >= b[-1]: b += s[i] ans += "1" else: ans = "N" break if ans == "N": print("NO") else: print("YES") print(ans) except EOFError: break
WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR STRING IF VAR STRING VAR VAR VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
class RangeMinimumQuery: def __init__(self, n, func=min, inf=float("inf")): self.n0 = 2 ** (n - 1).bit_length() self.op = func self.inf = inf self.data = [self.inf] * (2 * self.n0) def query(self, l, r): l += self.n0 r += self.n0 res = self.inf while l < r: if r & 1: r -= 1 res = self.op(res, self.data[r - 1]) if l & 1: res = self.op(res, self.data[l - 1]) l += 1 l >>= 1 r >>= 1 return res def update(self, i, x): i += self.n0 - 1 self.data[i] = x while i: i = ~-i // 2 self.data[i] = self.op(self.data[2 * i + 1], self.data[2 * i + 2]) n = int(input()) s = input() a = [(c, i) for i, c in enumerate(s)] a.sort() RMQ = RangeMinimumQuery(n, max, 0) col = [0] * n for _, i in a: c = RMQ.query(i, n) + 1 col[i] = c RMQ.update(i, c) max_col = RMQ.query(0, n) print(max_col) print(*col)
CLASS_DEF FUNC_DEF VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = list(input()) color = [-1] * n feasible = True for i in range(n): for j in range(0, i): if s[j] > s[i]: if color[i] == -1: color[i] = 1 - color[j] elif color[i] == color[j]: feasible = False if color[i] == -1: color[i] = 0 if feasible: print("YES") print("".join(map(str, color))) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
def main(): n = int(input()) s = input() lst = ["a"] * n ans = [] m = 0 for i in range(n): for j in range(n): if lst[j] <= s[i]: lst[j] = s[i] ans.append(j + 1) m = max(m, j + 1) break print(m) print(*ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() najm = [0] * n najm[n - 1] = s[n - 1] for i in range(n - 1): j = n - i - 2 najm[j] = min(najm[j + 1], s[j + 1]) can_sort = True for i in range(n): for j in range(i + 1, n - 1): if s[i] > s[j] and s[j] > najm[j]: can_sort = False break if not can_sort: print("NO") else: print("YES") kol = [-1] * n kol[0] = 0 ind = 0 while ind < n: if kol[ind] == -1: kol[ind] = 0 for i in range(ind + 1, n): if s[i] < s[ind]: kol[i] = (kol[ind] + 1) % 2 ind += 1 print("".join(map(str, kol)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) a = list(input()) a1 = [i for i in range(n)] new = {i: set() for i in range(n)} flag = False while not flag: flag = True for i in range(n - 1): if a[i] > a[i + 1]: a[i], a[i + 1] = a[i + 1], a[i] new.setdefault(a1[i], set()).add(a1[i + 1]) a1[i], a1[i + 1] = a1[i + 1], a1[i] flag = False i = 0 while i < n and len(new[i]) == 0: i += 1 if i == n: print("YES") print("0" * n) exit(0) set1 = {i} set2 = new[i] for i in range(n): flag = True for x in new: flag = True if x in set1: if not len(new[x] & set1) == 0: flag = False break set2 |= new[x] elif x in set2: if not len(new[x] & set2) == 0: flag = False break set1 |= new[x] else: set1.add(x) set2 |= new[x] if not flag: break if flag: print("YES") new = [0] * n for i in range(n): if i in set1: new[i] = 0 else: new[i] = 1 print("".join(map(str, new))) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
def solution(): n = int(input()) s = list(input()) s1, s2 = ["a"], ["a"] ans = [] for i in range(n): if s[i] >= s1[-1]: s1.append(s[i]) ans.append("0") elif s[i] >= s2[-1]: s2.append(s[i]) ans.append("1") else: print("NO") return print("YES") print("".join(ans)) return solution()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST STRING LIST STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
def ri(): return int(input()) def rl(): return list(map(int, input().split())) def solve(): n = ri() s = input().strip() codes = [-1] * n highest = {i: (0) for i in range(ord("a"), ord("z") + 1)} for p, c in enumerate(s): max_before = 0 for i in range(ord(c) + 1, ord("z") + 1): max_before = max(max_before, highest[i]) codes[p] = max_before + 1 highest[ord(c)] = max_before + 1 print(max(highest.values())) print(*codes) solve()
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 ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input().strip() CONTRADICT = False color = [(-1) for i in range(len(s))] color[-1] = 1 for i in range(n - 1, -1, -1): for j in range(i - 1, -1, -1): if s[i] < s[j]: if color[j] == -1: color[j] = 1 if color[i] == 0 else 0 elif color[i] == -1: color[i] = 1 if color[j] == 0 else 0 elif color[i] == color[j]: CONTRADICT = True for i in range(len(color)): if color[i] == -1: color[i] = 1 if not CONTRADICT: print("YES") for i in color: print(i, end="") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
import sys input = sys.stdin.readline n = int(input()) S = input().strip() ANS = [-1] * n for i in range(n): flag = 0 for j in range(i): if S[j] > S[i]: flag = 1 if ANS[j] != -1: if ANS[j] == ANS[i]: print("NO") sys.exit() ANS[i] = 1 - ANS[j] if flag: ANS[i] = 0 for j in range(i): if S[j] > S[i]: if ANS[j] == ANS[i]: print("NO") sys.exit() ANS[j] = 1 - ANS[i] for i in range(n): if ANS[i] == -1: ANS[i] = 0 print("YES") print("".join(tuple(map(str, ANS))))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR IF VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
import sys input = lambda: sys.stdin.readline().strip() n = int(input()) s = input() adj = [[] for i in range(n)] for i in range(n): for j in range(i + 1, n): if s[i] > s[j]: adj[i].append(j) adj[j].append(i) color = [(0) for i in range(n)] seen = [(False) for i in range(n)] def bfs(s): q = [s] for u in q: for v in adj[u]: if not seen[v]: seen[v] = True color[v] = 1 - color[u] q.append(v) for i in range(n): if not seen[i]: bfs(i) if all(color[u] != color[v] for u in range(n) for v in adj[u]): print("YES") print(*color, sep="") else: print("NO")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) arr = list(input()) for i in reversed(range(1, n)): cc = arr[i] cl = 0 for j in reversed(range(i)): if arr[j] > cc: cc = arr[j] cl += 1 if cl == 2: print("NO") exit(0) elif arr[j] > arr[i]: cc = arr[j] cc = arr[-1] narr = ["1" for _ in range(n)] for i in reversed(range(n)): if arr[i] <= cc: narr[i] = "0" cc = arr[i] print("YES") print("".join(narr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n, s = int(input()), input() x = y = "a" d = "" for c in s: if c >= x: d += "0" x = c elif c >= y: d += "1" y = c else: print("NO") exit() print("YES", d)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() s0 = "" s1 = "" k = "" s0 += s[0] k += "0" cont = True for x in range(1, n): if s1 == "": if s[x] >= s0[len(s0) - 1]: s0 += s[x] k += "0" else: s1 += s[x] k += "1" elif s[x] >= s0[len(s0) - 1]: s0 += s[x] k += "0" elif s[x] >= s1[len(s1) - 1]: s1 += s[x] k += "1" else: cont = False break if cont: print("YES") print(k) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING VAR VAR NUMBER VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR STRING IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR STRING VAR VAR VAR VAR STRING IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR STRING IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
import sys def main(): n = int(input()) s = input() subsequences = [] for i, c in enumerate(s): ok = False for subseq in subsequences: if s[subseq[-1]] <= c: subseq.append(i) ok = True break if not ok: subsequences.append([i]) color = [(-1) for _ in range(n)] for i in range(len(subsequences)): for j in subsequences[i]: color[j] = i + 1 print(len(subsequences)) oneLineArrayPrint(color) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(*args): assert ( len(args) >= 2 ), "makeArr args should be (default value, dimension 1, dimension 2,..." if len(args) == 2: return [args[0] for _ in range(args[1])] else: return [makeArr(args[0], *args[2:]) for _ in range(args[1])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FUNC_CALL VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
import sys def main(): n = int(sys.stdin.readline().split()[0]) s = sys.stdin.readline().split()[0] color = [None] * n color[0] = 0 for i in range(n): if color[i] == None: color[i] = 0 for j in range(i + 1, n): if ord(s[j]) < ord(s[i]): if color[j] == color[i]: print("NO") return if color[j] == None: color[j] = color[i] ^ 1 print("YES") print(*color, sep="") main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() ans = [0] * n for i in range(n - 1): for j in range(i + 1, n): if s[i] > s[j]: if ans[i] == 0: ans[j] = 1 else: ans[j] = 0 flag = True for i in range(n - 1): for j in range(i + 1, n): if s[i] > s[j]: if ans[i] == ans[j]: flag = False break if flag: print("YES") for i in ans: print(i, end="") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) x = input() s = [""] * n for i in range(0, n, 1): s[i] = x[i] ok = True color = [-1] * n ocolor = [-1] * n def swap(i, j): global color, s, ocolor for index in range(j, i, -1): tmpx = s[index] tmpc = color[index] s[index] = s[index - 1] color[index] = color[index - 1] s[index - 1] = tmpx color[index - 1] = tmpc return for i in range(0, n, 1): one = False zero = False alpha = [] index = -1 for j in range(0, i, 1): if s[j] <= s[i] and index != -1 or s[j] > s[i]: c = color[j] if c == -1: alpha.append(j) elif c == 1: one = True else: zero = True if index == -1: index = j if one and zero: ok = False break elif len(alpha) > 0 and not (one or zero): one = True for j in range(0, len(alpha), 1): if one: color[alpha[j]] = 1 ocolor[alpha[j]] = 1 else: color[alpha[j]] = 0 ocolor[alpha[j]] = 0 if one: color[i] = 0 ocolor[i] = 0 elif zero: color[i] = 1 ocolor[i] = 1 else: continue swap(index, i) if ok: print("YES") for i in range(0, n, 1): if ocolor[i] == -1: ocolor[i] = 1 print(ocolor[i], end="") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() r = [] q = 0 f = ["a"] for i in range(n): z = 0 while s[i] < f[z]: z = z + 1 if z == len(f): f.append(s[i]) break f[z] = s[i] r.append(z + 1) print(max(r)) print(*r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n, s, a, b, ans = int(input()), input(), "a", "a", "" check = True for i in s: if i >= a: ans += "0" a = i elif i >= b: ans += "1" b = i else: print("NO") check = False break if check: print("YES") print(ans)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING STRING STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = input() color = [(0) for _ in range(n)] f = 0 for i in range(n): for j in range(i + 1, n): if s[j] < s[i]: color[j] = 1 - color[i] for i in range(n): for j in range(i + 1, n): if color[i] == color[j] and s[i] > s[j]: f = 1 print("NO") break if f == 1: break if f == 0: print("YES") for i in range(n): print(color[i], end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
from sys import stdin, stdout def main(): n = int(stdin.readline()) s = stdin.readline().rstrip() h1 = "A" h2 = "B" res = [] for i, x in enumerate(s): if i == 0: res.append("0") h1 = s[i] continue if s[i] < h2: stdout.write("NO") return if s[i] >= h1: h1 = s[i] res.append("0") continue elif s[i] >= h2 and s[i] < h1: h2 = s[i] res.append("1") continue res.append("1") stdout.write("YES\n" + "".join(res)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
n = int(input()) s = [(ord(i) - ord("a") + 1) for i in input()] clr = ["0"] * n x = s[0] bad = [] flag = 1 for i in range(n): if s[i] >= x: x = s[i] else: clr[i] = "1" bad.append(s[i]) if any([(s[i] < j) for j in bad]): print("NO") flag = 0 break if flag: print("YES") print("".join(clr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
N = int(input()) s = input() a = "a" b = "a" t = "" flag = 0 for i in s: if a < b: if b <= i: b = i t += "1" elif a <= i: a = i t += "0" else: flag = 1 break elif b < a: if a <= i: a = i t += "0" elif b <= i: b = i t += "1" else: flag = 1 break elif a <= i: a = i t += "0" else: flag = 1 break if flag == 1: print("NO") else: print("YES") print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR STRING IF VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR STRING IF VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 200$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise). -----Examples----- Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
a = int(input("")) s = input("") def recurse(s, m): if len(s) == 0: return "" char = min(s) i = s.index(char) if i < m and i != 0: return -1 elif i < m and i == 0: return recurse(s[0:i] + s[i + 1 : len(s)], m - 1) else: add = "" for l in range(i - m): add = add + "1" add = add + "0" xd = recurse(s[0:i] + s[i + 1 : len(s)], i) if xd == -1: return -1 else: return add + xd ans = recurse(s, 0) if ans != -1: print("YES") print(ans) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles. Misha has to buy exactly $k$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel. The next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \le y_i \le x_i \le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free. -----Output----- Print one integer β€” the minimum cost of buying $k$ shovels if Misha buys them optimally. -----Examples----- Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Output 7 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Output 17 Input 5 1 4 2 5 7 4 6 5 4 Output 17 -----Note----- In the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$. In the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$.
import sys input = sys.stdin.readline n, m, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() b = [0] for i in range(k - 1, -1, -1): b.append(a[i]) c = [0] * (k + 1) for i in range(1, k + 1): c[i] += b[i] + c[i - 1] s = [0] * (k + 1) for _ in range(m): x, y = map(int, input().split()) if x <= k: s[x] = max(s[x], y) inf = 1145141919810 dp = [0] * (k + 1) for i in range(1, k + 1): dpi = inf for j in range(1, i + 1): d = i - j + 1 dpi = min(dpi, dp[j - 1] - c[j - 1] + c[i - s[d]]) dp[i] = dpi ans = dp[k] print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN 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 FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles. Misha has to buy exactly $k$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel. The next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \le y_i \le x_i \le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free. -----Output----- Print one integer β€” the minimum cost of buying $k$ shovels if Misha buys them optimally. -----Examples----- Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Output 7 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Output 17 Input 5 1 4 2 5 7 4 6 5 4 Output 17 -----Note----- In the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$. In the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$.
import sys def main(): global visited, adj, sets n, m, k = get_list() li = get_list() li.sort() offers = [[10**10, 0]] for _ in range(m): offers.append(get_list()) offers.sort() best_y = [0] ptr = -1 for i in range(k): best_y.append(best_y[-1]) while offers[ptr + 1][0] <= i + 1: ptr += 1 best_y[-1] = max(best_y[-1], offers[ptr][1]) x, y = 1, i while x <= y: best_y[-1] = max(best_y[-1], best_y[x] + best_y[y]) x += 1 y -= 1 dp = [0] for i in range(1, k + 1): prefix_sm = [0] dp.append(dp[-1] + li[i - 1]) for j in range(i - 1, -1, -1): prefix_sm.append(prefix_sm[-1] + li[j]) x = i - j dp[-1] = min(dp[-1], dp[j] + prefix_sm[x - best_y[x]]) print(dp[k]) input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ out = [] get_int = lambda: int(input()) get_list = lambda: list(map(int, input().split())) main() print(*out, sep="\n")
IMPORT FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST LIST BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST 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 EXPR FUNC_CALL VAR VAR STRING
There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles. Misha has to buy exactly $k$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel. The next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \le y_i \le x_i \le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free. -----Output----- Print one integer β€” the minimum cost of buying $k$ shovels if Misha buys them optimally. -----Examples----- Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Output 7 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Output 17 Input 5 1 4 2 5 7 4 6 5 4 Output 17 -----Note----- In the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$. In the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$.
n, m, k = map(int, input().split()) arr = list(map(int, input().split())) offer = [list(map(int, input().split())) for _ in range(m)] arr = sorted(arr) arr = arr[:k] arr = arr[::-1] acum = [0] for i in range(k): acum.append(acum[-1] + arr[i]) dp = [acum[i] for i in range(k + 1)] for x, y in offer: if x > k: continue for i in range(x, k + 1): dp[i] = min( dp[i], dp[i - x] + (acum[i] - acum[i - x]) - (acum[i] - acum[i - y]) ) tmp = dp[0] for i in range(1, k + 1): tmp = min(tmp, dp[i - 1]) + arr[i - 1] dp[i] = min(dp[i], tmp) for x, y in offer: if x > k: continue for i in range(x, k + 1): dp[i] = min( dp[i], dp[i - x] + (acum[i] - acum[i - x]) - (acum[i] - acum[i - y]) ) tmp = dp[0] for i in range(1, k + 1): tmp = min(tmp, dp[i - 1]) + arr[i - 1] dp[i] = min(dp[i], tmp) print(dp[k])
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles. Misha has to buy exactly $k$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel. The next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \le y_i \le x_i \le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free. -----Output----- Print one integer β€” the minimum cost of buying $k$ shovels if Misha buys them optimally. -----Examples----- Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Output 7 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Output 17 Input 5 1 4 2 5 7 4 6 5 4 Output 17 -----Note----- In the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$. In the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$.
from sys import stdin, stdout n, m, k = tuple(map(lambda x: int(x), stdin.readline().split())) a = stdin.readline().split() for i in range(len(a)): a[i] = int(a[i]) prefix_sum = [] a = sorted(a, key=lambda x: x) for x in a: if prefix_sum: prefix_sum.append(prefix_sum[-1] + x) else: prefix_sum.append(x) offers = {} for i in range(m): x, y = stdin.readline().split() x = int(x) y = int(y) if x not in offers or y > offers[x]: offers[x] = y answer = [] for i in range(k): if i == 0: if 1 in offers and offers[1] > 0: answer.append(0) else: answer.append(a[0]) continue answer.append(400000002) for j in range(i): cursum = answer[j] answer[i] = min( answer[i], answer[j] + prefix_sum[i] - prefix_sum[j + (offers[i - j] if i - j in offers else 0)], ) answer[i] = min( answer[i], ( prefix_sum[i] if i + 1 not in offers else -prefix_sum[offers[i + 1] - 1] + prefix_sum[i] ), ) print(answer[k - 1])
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles. Misha has to buy exactly $k$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel. The next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \le y_i \le x_i \le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free. -----Output----- Print one integer β€” the minimum cost of buying $k$ shovels if Misha buys them optimally. -----Examples----- Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Output 7 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Output 17 Input 5 1 4 2 5 7 4 6 5 4 Output 17 -----Note----- In the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$. In the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$.
n, m, k = map(int, input().split()) ai = list(map(int, input().split())) ar = [0] * k for i in range(m): x, y = list(map(int, input().split())) x -= 1 if x < k: ar[x] = max(ar[x], y) ai.sort() big = 10**9 ar2 = [big] * (k + 1) ar3 = [0] * (k + 1) ar3[0] = 0 for i in range(1, k + 1): ar3[i] = ar3[i - 1] + ai[i - 1] ar2[k] = 0 for i in range(k, 0, -1): for j in range(i): ar2[i - j - 1] = min(ar2[i - j - 1], ar2[i] + ar3[i] - ar3[i - (j + 1 - ar[j])]) print(ar2[0])
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles. Misha has to buy exactly $k$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel. The next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \le y_i \le x_i \le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free. -----Output----- Print one integer β€” the minimum cost of buying $k$ shovels if Misha buys them optimally. -----Examples----- Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Output 7 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Output 17 Input 5 1 4 2 5 7 4 6 5 4 Output 17 -----Note----- In the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$. In the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$.
IN = input rint = lambda: int(IN()) rmint = lambda: list(map(int, IN().split())) rlist = lambda: list(rmint()) n, m, k = rmint() a = rlist() t = [] for i in range(m): x, y = rmint() t.append((x, y)) t.sort() a.sort() pr = [0] for i in a: pr.append(pr[-1] + i) f = 4096 * [0] def s(l, r): if l > r: return 0 else: return pr[r + 1] - pr[l] ans = s(0, k - 1) def upd(x, y): nonlocal ans f[x] = min(f[x], y) ans = min(ans, y + s(x, k - 1)) for i in range(1, k + 1): f[i] = f[i - 1] + a[i - 1] for p in t: x, y = p if i - x < 0: break upd(i, f[i - x] + s(i - x + y, i - 1)) print(ans)
ASSIGN 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER LIST NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles. Misha has to buy exactly $k$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel. The next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \le y_i \le x_i \le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free. -----Output----- Print one integer β€” the minimum cost of buying $k$ shovels if Misha buys them optimally. -----Examples----- Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Output 7 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Output 17 Input 5 1 4 2 5 7 4 6 5 4 Output 17 -----Note----- In the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$. In the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$.
N, M, K = map(int, input().split()) A = sorted([int(a) for a in input().split()])[:K][::-1] B = [0] for a in A: B.append(B[-1] + a) X = [0] * (K + 1) for _ in range(M): x, y = map(int, input().split()) if x <= K: X[x] = max(X[x], y) Y = [0] * (K + 1) for i in range(1, K + 1): mi = 10**100 for j in range(1, i + 1): s = Y[i - j] + B[i - X[j]] - B[i - j] mi = min(mi, s) Y[i] = mi print(Y[K])
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles. Misha has to buy exactly $k$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel. The next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \le y_i \le x_i \le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free. -----Output----- Print one integer β€” the minimum cost of buying $k$ shovels if Misha buys them optimally. -----Examples----- Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Output 7 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Output 17 Input 5 1 4 2 5 7 4 6 5 4 Output 17 -----Note----- In the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$. In the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$.
import sys dp = [] sum = [] INF = sys.maxsize / 2 def sum_range(a, b): if a == 0: return sum[b] return sum[b] - sum[a - 1] def rec(pos, sale, n, k): if k == 0: return 0 if dp[k] != -1: return dp[k] res = INF for x in range(1, k + 1): if sale[x] != -1: res = min( res, rec(pos + x, sale, n, k - x) + sum_range(pos + sale[x], pos + x - 1), ) dp[k] = res return res inp = [int(x) for x in sys.stdin.read().split()] n, m, k = inp[0], inp[1], inp[2] inp_idx = 3 a = [] for _ in range(n): a.append(inp[inp_idx]) inp_idx += 1 sale = [-1] * (k + 1) sale[1] = 0 for _ in range(m): x, y = inp[inp_idx], inp[inp_idx + 1] inp_idx += 2 if x > k: continue sale[x] = max(sale[x], y) a.sort() sum = [0] * n sum[0] = a[0] for i in range(1, n): sum[i] = sum[i - 1] + a[i] dp = [-1] * (k + 1) cost = rec(0, sale, n, k) print(cost)
IMPORT ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles. Misha has to buy exactly $k$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel. The next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \le y_i \le x_i \le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free. -----Output----- Print one integer β€” the minimum cost of buying $k$ shovels if Misha buys them optimally. -----Examples----- Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Output 7 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Output 17 Input 5 1 4 2 5 7 4 6 5 4 Output 17 -----Note----- In the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$. In the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$.
n, m, k = list(map(int, input().split())) a = list(map(int, input().split())) a = sorted(a) a = a[:k] d = [(1, 0)] for i in range(m): x, y = list(map(int, input().split())) if x > k: continue d.append((x, y)) d = sorted(d) s = [0] * (k + 1) s[1] = a[0] for i in range(1, k + 1): s[i] = s[i - 1] + a[i - 1] INF = float("inf") dp = [INF] * (k + 1) dp[0] = 0 for i in range(k + 1): for j in range(len(d)): x, y = d[j] if i + x <= k: dp[i + x] = min(dp[i + x], dp[i] + s[i + x] - s[i + y]) else: break print(dp[k])
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR
There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles. Misha has to buy exactly $k$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel. The next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \le y_i \le x_i \le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free. -----Output----- Print one integer β€” the minimum cost of buying $k$ shovels if Misha buys them optimally. -----Examples----- Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Output 7 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Output 17 Input 5 1 4 2 5 7 4 6 5 4 Output 17 -----Note----- In the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$. In the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$.
n, m, k = (int(i) for i in input().split()) cost = sorted([int(i) for i in input().split()])[:k] + [0] discount = [0] * n for i in range(m): a, b = (int(j) for j in input().split()) discount[a - 1] = max(discount[a - 1], b) S = [0] * (k + 1) for i in range(k): S[i] = cost[i] + S[i - 1] cost[i] += cost[i - 1] for j in range(i + 1): S[i] = min(S[i], S[j - 1] + cost[i] - cost[j - 1 + discount[i - j]]) print(S[k - 1])
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
n = int(input()) while n > 0: a, x = map(int, input().split()) arr = list(map(int, input().split())) i = 0 count = 0 while i < len(arr): if sorted(arr) == arr: break if arr[i] > x: x, arr[i] = arr[i], x count += 1 i += 1 if sorted(arr) == arr: print(count) else: print(-1) n -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
t = int(input()) for i in range(t): n, x = map(int, input().split()) arr = list(map(int, input().split())) if arr == sorted(arr): print(0) else: res = 0 for i in range(n): if arr[i] > x: temp = x x = arr[i] arr[i] = temp res += 1 if arr == sorted(arr): print(res) break elif i == n - 1: print(-1) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def f(l): last = l[0] bool = True for i in l[1:]: if last > i: bool = False break last = i return bool for _ in range(int(input())): n, x = map(int, input().split()) l = list(map(int, input().split())) bool = False ans = 0 if f(l): print(0) continue while True: ans += 1 i = 0 while i < n and l[i] <= x: i += 1 if i == n: ans = -1 break l[i], x = x, l[i] if f(l): break print(ans)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
test = int(input()) for t in range(0, test): n, x = input().split() n = int(n) x = int(x) a = [int(i) for i in input().split(" ")] ans = 0 while 1: if a == sorted(a): print(ans) break f = 0 for i in range(0, len(a)): if a[i] > x: temp = a[i] a[i] = x x = temp ans += 1 f = 1 break if a == sorted(a): print(ans) break if f == 0: print(-1) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
import sys from sys import maxsize def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) def get_list_string(): return list(map(str, sys.stdin.readline().strip().split())) def get_string(): return sys.stdin.readline().strip() def get_int(): return int(sys.stdin.readline().strip()) def get_print_int(x): sys.stdout.write(str(x) + "\n") def get_print(x): sys.stdout.write(x + "\n") def arrsort(arr, n): for i in range(1, n): if arr[i] < arr[i - 1]: return False return True def solve(): for _ in range(get_int()): n, x = get_ints() arr = get_list() if arrsort(arr, n): get_print_int(0) else: ans = 0 f = 0 while True: ans += 1 i = 0 while i < n and arr[i] <= x: i += 1 if i == n: ans = -1 break arr[i], x = x, arr[i] if arrsort(arr, n): break get_print_int(ans) solve()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
for u in range(int(input())): n, x = map(int, input().split()) a = list(map(int, input().split())) i = 0 ans = 0 while i < n: k = list(a) k.sort() if k == a: break if a[i] > x: a[i], x = x, a[i] ans += 1 if x > max(a): break i += 1 k = list(a) k.sort() if k == a: print(ans) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
t = int(input()) for _ in range(t): n, x = tuple(map(int, input().split(" "))) ans = -1 a = list(map(int, input().split(" "))) for i in range(0, n): if a[i] > x and sorted(a) != a: a[i], x = x, a[i] ans = ans + 1 if sorted(a) != a: ans = -1 print(ans) else: print(ans + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def check(a): if sorted(a) == a: return 1 return 0 t = int(input()) for _ in range(t): kt = 0 cnt = 0 n, x = map(int, input().split()) a = list(map(int, input().split())) if check(a): print(0) continue for i in range(n): if x < a[i]: a[i] = a[i] ^ x x = a[i] ^ x a[i] = a[i] ^ x cnt = cnt + 1 if check(a): kt = 1 break if kt: print(cnt) else: print(-1)
FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def sol(): for _ in range(int(input())): n, x = map(int, input().split()) arr = list(map(int, input().split())) if arr == sorted(arr): print(0) else: count = 0 flag = False for i in range(n): if arr[i] > x: arr[i], x = x, arr[i] count += 1 if arr == sorted(arr): flag = True break if flag == False: print(-1) else: print(count) sol()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def notsorted(l, n): for i in range(0, n - 1): if l[i] > l[i + 1]: return True return False for _ in range(int(input())): n, x = map(int, input().split()) l = list(map(int, input().split())) ans = 0 i = 0 flag = True while notsorted(l, n): ans += 1 while i < n and l[i] <= x: i += 1 if i == n: print(-1) flag = False break else: x, l[i] = l[i], x if flag: print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def func(x, l1): num = 0 x0 = 0 repeat = 0 for i in range(len(l1) - 1): if l1[i] <= x: x0 = i + 1 if x0 < i: if l1[i] == l1[i - 1]: repeat += 1 if l1[i] > l1[i + 1]: if x > l1[i + 1] or i > 0 and l1[i - 1] > l1[i + 1]: return -1 num += i + 1 - x0 - repeat repeat = 0 xx = x x = l1[i] l1[i] = xx return num for test_num in range(int(input())): x = int(input().split(" ")[1]) l = input().split(" ") l1 = [] for i in l: l1.append(int(i)) print(func(x, l1))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
import sys input = sys.stdin.readline def solve(): n, x = map(int, input().split()) arr = list(map(int, input().split())) B = sorted(arr) if arr == B: return 0 b = n - 1 while b >= 0 and arr[b] == B[b]: b -= 1 A = arr + [x] A.sort() cnt = 0 prev = x for i in range(b + 1): if arr[i] != A[i]: if A[i] != prev: return -1 prev = arr[i] cnt += 1 return cnt for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) INF = 10**18 for _ in range(t): n, x = map(int, input().split()) a = list(map(int, input().split())) if sorted(a) == a: print(0) continue cnt = 0 for i in range(n): if a[i] > x: a[i], x = x, a[i] cnt += 1 if sorted(a) == a: print(cnt) break else: print(-1)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
t = int(input()) for _ in range(t): n, x = map(int, input().split()) s = 0 a = list(map(int, input().split())) for i in range(n - 1): if a[i] > a[i + 1]: s = 1 if s == 0: print(0) else: c = 0 for i in range(n - 1): if a[i] > x: a[i], x = x, a[i] c += 1 jj = 1 for j in range(i, n - 1): if a[j] > a[j + 1]: jj = 0 if jj == 1: break r = 0 for i in range(n - 1): if a[i] > a[i + 1]: r = 1 if r == 1: print(-1) else: print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
Inp = input for _ in range(int(Inp())): n, k = map(int, Inp().split()) l = [*map(int, Inp().split())] b = 0 for i in range(n): if sorted(l) == l: break if l[i] > k: l[i], k = k, l[i] b += 1 print([-1, b][sorted(l) == l])
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR FUNC_CALL VAR VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def ans(A, x): if A == sorted(A): return 0 i = 0 ret = 0 while i < len(A) and A != sorted(A): if x < A[i]: ret += 1 x, A[i] = A[i], x i += 1 else: i += 1 continue if A == sorted(A): return ret return -1 T = int(input()) for _ in range(T): _, x = input().split() x = int(x) A = list(map(int, input().split())) print(ans(A, x))
FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
t = int(input()) for i in range(t): n, x = map(int, input().split()) array = [int(num) for num in input().split()] ans = 0 j = 0 found = False if array == sorted(array): print(0) continue for i in range(n): if array[i] > x: temp = array[i] array[i] = x x = temp ans += 1 if array == sorted(array): print(ans) found = True break if found == False: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def check(arr): for i in range(len(arr) - 1): if arr[i] > arr[i + 1]: return False return True for _ in range(int(input())): n, x = map(int, input().split()) arr = list(map(int, input().split())) if check(arr): print(0) else: asn = 0 for j in range(n): if arr[j] > x: arr[j], x = x, arr[j] asn += 1 if check(arr): break if check(arr) == False: print(-1) else: print(asn)
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def solve(l, x): count = 0 for i in range(len(l)): if sorted(l) == l: break if l[i] > x: l[i], x = x, l[i] count += 1 if sorted(l) == l: return count else: return -1 for _ in range(int(input())): n, x = map(int, input().split()) l = list(map(int, input().split())) print(solve(l, x))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
t = int(input()) all = [] x_arr = [] for i in range(t): k = input() k = [int(m) for m in k.split()] l, x = k x_arr.append(x) h = input() a = [int(z) for z in h.split()] all.append(a) for i in range(t): x = x_arr[i] arr = all[i] sorted_ = sorted(arr) if sorted_ == arr: print(0) else: step = 0 for idx in range(len(arr)): if x < arr[idx]: swa = x x = arr[idx] arr[idx] = swa step += 1 s = sorted(arr) if s == arr: break if sorted(arr) == arr: print(step) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def not_sorted(n, a): for i in range(1, n): if a[i] < a[i - 1]: return -1 return 0 def solve(n, x, a): cnt = 0 while not_sorted(n, a): cnt += 1 i = 0 while i < n and a[i] <= x: i += 1 if i == n: return -1 a[i], x = x, a[i] return cnt t = int(input()) while t: n, x = map(int, input().split()) a = list(map(int, input().split())) print(solve(n, x, a)) t -= 1
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
for i in range(int(input())): n, x = [int(_) for _ in input().split()] arr = [int(_) for _ in input().split()] c = 0 for i in range(n): if sorted(arr) == arr: break if arr[i] > x: x, arr[i] = arr[i], x c += 1 if sorted(arr) == arr: print(c) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
from sys import stdin nii = lambda: map(int, stdin.readline().split()) lnii = lambda: list(map(int, stdin.readline().split())) t = int(input()) for tt in range(t): n, x = nii() a = lnii() if a == sorted(a): print(0) continue ans = 0 for i in range(n): if a[i] > x: ans += 1 a[i], x = x, a[i] if a == sorted(a): print(ans) break else: print(-1)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
for _ in range(int(input())): n, x = map(int, input().split()) a = [int(i) for i in input().split()] ans = 0 temp = 0 x1 = x for i in range(n - 1): if a[i] > a[i + 1]: if a[i] <= x: ans = -1 break elif x > a[i + 1]: ans = -1 break else: ans += 1 + temp x = a[i] temp = 0 elif x < a[i] and i == 0: temp += 1 x = a[i] elif i != 0: if x < a[i] and a[i] != a[i - 1]: temp += 1 x = a[i] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
for i in range(int(input())): a = list(map(int, input().split())) b = list(map(int, input().split())) n = a[0] x = a[1] if n == 1: print(0) continue i = 0 count = 0 f = 0 flag = 0 while i < n: ans = 0 for j in range(i, n): if j == 0: continue if b[j] < b[j - 1]: flag = 1 break for z in range(i, j + 1): if z == j and b[z] < b[z - 1] and x < b[z] and flag == 1: ans += 1 q = b[z] b[z] = x x = q break elif b[z] > x and flag == 1: q = b[z] b[z] = x x = q ans += 1 i = j + 1 flag = 0 for g in range(1, j + 1): if b[g] < b[g - 1]: f = 1 if f == 1: count = -1 break else: count += ans if f == 1: print(-1) else: print(count)
FOR VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def swap(l, r, x): temp = arr[r] for o in range(r, l, -1): arr[o] = arr[o - 1] arr[l] = x return temp t = int(input()) for z in range(t): n, x = map(int, input().split()) arr = list(map(int, input().split())) count = 0 ok = 0 for i in range(n - 1): if arr[i] > arr[i + 1]: if x <= arr[i + 1]: j = i - 1 while j >= 0: if x < arr[j]: j = j - 1 else: break count = count + len(set(arr[j + 1 : i + 1])) x = swap(j + 1, i, x) if arr[i] > arr[i + 1]: ok = 1 else: ok = 1 if ok == 1: print("-1") else: print(count)
FUNC_DEF ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
t = int(input()) ans = "" def is_sorted(lst, key=lambda x: x): for i, el in enumerate(lst[1:]): if key(el) < key(lst[i]): return False return True for j in range(t): n, x = list(map(int, input().split())) a = list(map(int, input().split())) cnt = 0 if is_sorted(a) == False: for i in range(n): if x < a[i]: x, a[i] = a[i], x cnt += 1 if is_sorted(a): break if is_sorted(a) == False: cnt = -1 ans += str(cnt) + "\n" print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FUNC_DEF VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
I = lambda: map(int, input().split()) s = sorted (T,) = I() for _ in range(T): n, k = I() l = [*I()] a = 0 for i in range(n): if s(l) == l: break if l[i] > k: l[i], k = k, l[i] a += 1 print([a, -1][s(l) < l])
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER FUNC_CALL VAR VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def ans(arr, x): if len(arr) == 1: return 0 elif arr == sorted(arr): return 0 else: cnt = 0 for i in range(len(arr)): if arr[i] > x: arr[i], x = x, arr[i] cnt += 1 if arr == sorted(arr): return cnt break return -1 for _ in range(int(input())): n, x = map(int, input().split()) arr = list(map(int, input().split())) print(ans(arr, x))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def check(ll): for i in range(len(l) - 1): if l[i] <= l[i + 1]: continue else: return False return True for i in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) c = 0 if len(l) == 1: print(0) continue if check(l): print(0) continue for i in range(len(l)): if l[i] > k: t = l[i] l[i] = k k = t c += 1 if not check(l): continue else: break if not check(l): c = -1 print(c)
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
def calc_swaps(nums, x): if nums == sorted(nums): return 0 swaps = 0 for ind in range(len(nums)): if nums[ind] > x: x, nums[ind] = nums[ind], x swaps += 1 if nums == sorted(nums): return swaps return -1 data_count = int(input()) ans = [] for _ in range(data_count): cnt, x = list(map(int, input().split())) nums = list(map(int, input().split())) ans.append(calc_swaps(nums, x)) for res in ans: print(res)
FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$. For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible: choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$; choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$; choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$. Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$) β€” the number of test cases. Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β€” the number of elements in the sequence and the initial value of $x$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$). The sum of values of $n$ over all test cases in the input does not exceed $500$. -----Output----- For each test case, print one integer β€” the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible. -----Examples----- Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 Output 3 0 0 -1 1 3 -----Note----- None
import sys input = sys.stdin.readline (T,) = map(int, input().split()) for _ in range(T): N, x = map(int, input().split()) A = list(map(int, input().split())) f = 0 for i in range(N - 1): if A[i] > A[i + 1]: f = 1 break if not f: print(0) continue for K in range(1, N): for j in range(N): if A[j] > x: A[j], x = x, A[j] break f = 0 for i in range(N - 1): if A[i] > A[i + 1]: f = 1 break if not f: break if f: print(-1) else: print(K)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR