description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
import sys input = sys.stdin.readline l = input().split() n = int(l[0]) k = int(l[1]) curr = 0 hashi = dict() a = [0] for i in range(k): for j in range(i, k): if j == (i + 1) % k: continue if j == i: a.append(i) else: a.append(j) a.append(i) if i != k - 1: a.append((i + 1) % k) for i in range(k - 2, -1, -1): a.append(i) for i in range(n): print(chr(ord("a") + a[i % (k * k)]), end="")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR BIN_OP VAR BIN_OP VAR VAR STRING
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) ans = "" for i in range(k): ans += chr(97 + i) for j in range(i + 1, k): ans += chr(97 + i) ans += chr(97 + j) while len(ans) < n: ans += ans print(ans[:n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
def answer(n, k): a = "" s = "abcdefghijklmnopqrstuvwxyz" for i in range(k): j = i while True: a += s[j] j += 1 if j == k: break a += s[i] m = k * k return a * (n // m) + a[: n % m] n, k = map(int, input().split()) print(answer(n, k))
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
def dfs(v): global s0 for u in range(k, 0, -1): if gr[v][u] == 1: gr[v][u] = 0 dfs(u) s0 += [v] s0 = [] n, k = map(int, input().split()) gr = [[(1) for _ in range(k + 1)] for _ in range(k + 1)] dfs(1) s = "" for i in s0: s += chr(i + 96) i = 0 j = 0 while i < n: if j == len(s): j = 0 if s[-1] == s[0]: j += 1 print(s[j], end="") j += 1 i += 1
FUNC_DEF FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER VAR NUMBER
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) ans = "" for i in range(k): ans = ans + chr(i + 97) for j in range(i + 1, k): ans = ans + chr(i + 97) + chr(j + 97) n1 = len(ans) n2 = n // n1 n3 = n % n1 fin = ans * n2 fin = fin + ans[:n3] print(fin)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) s = "" for i in range(k): s += chr(ord("a") + i) for j in range(i + 1, k): s += chr(ord("a") + i) + chr(ord("a") + j) while len(s) < n: s *= 2 print(s[:n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
def solvee(n, k): abc = "abcdefghijklmnopqrstuvwxyz" if n == 1: return "a" if n < k: return abc[:n] lis = [] for x in range(0, k): lis.append(abc[x]) for y in range(x + 1, k): lis.append(abc[x] + abc[y]) ans = "".join(lis) rem = n // len(ans) ans = ans * (rem + 1) return ans[:n] n, k = map(int, input().split()) print(solvee(n, k))
FUNC_DEF ASSIGN VAR STRING IF VAR NUMBER RETURN STRING IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().rstrip().split(" ")) alpha = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] s = "" for i in range(k): s += alpha[i] for j in range(i + 1, k): s += alpha[i] s += alpha[j] if len(s) > n: break while len(s) < n: s *= n print(s[:n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
a = list("abcdefghijklmnopqrstuvwxyz") n, k = map(int, input().split()) if k >= n: print(*a[0:n], sep="") else: ans = "" for i in range(k): ans += a[i] for j in range(i + 1, k): ans += a[i] + a[j] while n > len(ans): ans *= 2 print(ans[0:n])
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
import sys input = sys.stdin.readline n, k = map(int, input().split()) x = [] for i in range(k): x.append(i) x.append(i) for j in range(i + 2, k): x.append(j) x.append(i) for i in range(k - 2, 0, -1): x.append(i) y = [chr(i + 97) for i in x] ans = [] for i in range(n): ans.append(y[i % len(x)]) ans = "".join(ans) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = [int(i) for i in input().split()] ans = "" for x in range(k): ans += chr(x + 97) * 2 for i in range(k): for j in range(k): tmp = chr(i + 97) + chr(j + 97) if ans.find(tmp) == -1: if len(ans) > 0: if ans.find(ans[-1] + tmp[0]) == -1: ans += tmp ans = ans[: len(ans) - 1] if len(ans) > n: ans = ans[:n] elif len(ans) < n: while len(ans) < n: tmp = str(ans) tmp[::-1] ans += tmp if len(ans) > n: break if len(ans) > n: ans = ans[:n] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) alphabet = "abcdefghijklmnopqrstuvwxyz" s = "" for i in range(k): s += alphabet[i] for j in range(i + 1, k): s += alphabet[i] + alphabet[j] blk = n // len(s) small = n % len(s) print(s * blk + s[:small])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
def createks(k): ks = ["a", "a"] if k == 1: return ks ks = ["a", "a", "b", "b", "a"] if k == 2: return ks msd = 2 while msd < k: ks.extend([chr(ord("a") + msd), chr(ord("a") + msd - 1), chr(ord("a") + msd)]) for i in range(msd - 1): ks.extend(chr(ord("a") + msd)) ks.extend(chr(ord("a") + (msd - (2 + i)))) msd += 1 return ks def answer(n, k): ks = createks(k) ans = [] extra = n - len(ks) for i in range(min(n, len(ks))): ans.append(ks[i]) ks.pop(0) for i in range(extra): ans.append(ks[i % len(ks)]) return "".join(ans) def main(): n, k = [int(i) for i in input().split()] print(answer(n, k)) return main()
FUNC_DEF ASSIGN VAR LIST STRING STRING IF VAR NUMBER RETURN VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
def split(word): return [char for char in word] n, k = map(int, input().split()) a = [[] for j in range(26)] a[0] = "a" a[1] = "abb" a[2] = "acbcc" a[3] = "adbdcdd" a[4] = "aebecedee" a[5] = "afbfcfdfeff" a[6] = "agbgcgdgegfgg" a[7] = "ahbhchdhehfhghh" a[8] = "aibicidieifigihii" a[9] = "ajbjcjdjejfjgjhjijj" a[10] = "akbkckdkekfkgkhkikjkk" a[11] = "alblcldlelflglhliljlkll" a[12] = "ambmcmdmemfmgmhmimjmkmlmm" a[13] = "anbncndnenfngnhninjnknlnmnn" a[14] = "aobocodoeofogohoiojokolomonoo" a[15] = "apbpcpdpepfpgphpipjpkplpmpnpopp" a[16] = "aqbqcqdqeqfqgqhqiqjqkqlqmqnqoqpqq" a[17] = "arbrcrdrerfrgrhrirjrkrlrmrnrorprqrr" a[18] = "asbscsdsesfsgshsisjskslsmsnsospsqsrss" a[19] = "atbtctdtetftgthtitjtktltmtntotptqtrtstt" a[20] = "aubucudueufuguhuiujukulumunuoupuqurusutuu" a[21] = "avbvcvdvevfvgvhvivjvkvlvmvnvovpvqvrvsvtvuvv" a[22] = "awbwcwdwewfwgwhwiwjwkwlwmwnwowpwqwrwswtwuwvww" a[23] = "axbxcxdxexfxgxhxixjxkxlxmxnxoxpxqxrxsxtxuxvxwxx" a[24] = "aybycydyeyfygyhyiyjykylymynyoypyqyrysytyuyvywyxyy" a[25] = "azbzczdzezfzgzhzizjzkzlzmznzozpzqzrzsztzuzvzwzxzyzz" s = "" for j in range(k): s = s + a[j] g = [] s = split(s) l = len(s) for j in range(n): g.append(s[j % l]) print("".join(map(str, g)))
FUNC_DEF RETURN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) l = [] for i in range(k): for j in range(i, k): l.append(chr(97 + i)) if i != j: l.append(chr(97 + j)) if k == 1: l.append("a") a = len(l) t = n // a s = "".join(l) print(s * t + s[: n % a])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
def funMin(k): pref, suff = "", "" for i in range(ord("a"), ord("a") + k): pref += chr(i) * 2 for j in range(i + 2, ord("a") + k): suff += chr(i) + chr(j) pref += suff for i in range(ord("a") + k - 2, ord("a") - 1, -1): pref += chr(i) return pref n, k = map(int, input().split()) ans = funMin(k) ext = ans[1:] ans += ext * (n // len(ext)) print(ans[:n])
FUNC_DEF ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR STRING VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING VAR NUMBER BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
x, y = [int(a) for a in input().split()] ar = [y - 1] * y i = 0 s = "" while len(s) < x: s = s + chr(97 + i) t = ar[i] ar[i] = (ar[i] - 1 + y) % y i = t print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) now = [i for i in range(k)] ans = [0] num = 0 for i in range(n - 1): x = num num = now[num] ans.append(num) now[x] += 1 now[x] %= k a = ord("a") ans = [chr(a + i) for i in ans] print("".join(ans))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, m = list(map(int, input().split())) ans = "" kek = 97 bek = kek while n > 0: if kek == 97 + m: kek = 97 if m > 1: bek = kek + 1 else: bek = kek ans += chr(kek) n -= 1 while n > 0 and bek < 97 + m: ans += chr(kek) n -= 1 if n > 0: ans += chr(bek) bek += 1 n -= 1 kek += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = tuple(map(int, input().split())) alphabets = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] d = {} for i in range(k): d[alphabets[i]] = 0 if n == 1: print("a") elif k == 1: print("a" * n) elif n <= k * k: count = 1 ans = ["a"] while count < n: current = ans[-1] if d[current] == -1: inde = alphabets.index(current) j = (inde + 1) % k while d[alphabets[j]] == -1: j += 1 if j >= k: j = 0 current = alphabets[j] while d[alphabets[d[current]]] == -1: d[current] += 1 ans.append(alphabets[d[current]]) d[current] += 1 if d[current] >= k: d[current] = -1 count += 1 print("".join(ans)) else: count = 1 ans = ["a"] while count < k * k: current = ans[-1] if d[current] == -1: inde = alphabets.index(current) j = (inde + 1) % k while d[alphabets[j]] == -1: j += 1 if j >= k: j = 0 current = alphabets[j] while d[alphabets[d[current]]] == -1: d[current] += 1 ans.append(alphabets[d[current]]) d[current] += 1 if d[current] >= k: d[current] = -1 count += 1 s = "".join(ans) final_ans = n // len(s) * s i = 0 while len(final_ans) < n: final_ans += s[i] i += 1 print(final_ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST STRING WHILE VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR LIST STRING WHILE VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
import sys from sys import stdin alp = "abcdefghijklmnopqrstuvwxyz" n, k = map(int, stdin.readline().split()) s = [] for i in range(k): for j in range(k - 1, i + 1, -1): s.append(alp[i]) s.append(alp[j]) s.append(alp[i]) s.append(alp[k - 1]) for i in range(k - 2, -1, -1): s.append(alp[i]) s.append(alp[i]) del s[-1] ans = [s[i % len(s)] for i in range(n)] print("".join(ans))
IMPORT ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) s = "abcdefghijklmnopqrstuvwxyz" s = s[:k] dic = {} ans = "" for i in range(k): ans += chr(97 + i) for j in range(i + 1, k): ans += chr(97 + i) + chr(97 + j) m = len(ans) ans = ans * (n // m) + ans[: n % m] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
s = [] n, k = list(map(int, input().split())) for i in range(k): s.append(chr(97 + i)) for j in range(i + 1, k): s.append(chr(97 + i)) s.append(chr(97 + j)) sz = len(s) for i in range(n): print(s[i % sz], end="") print()
ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
ascii_lowercase = "abcdefghijklmnopqrstuvwxyz" def solve(n, k): strs = ascii_lowercase[:k] if k == 1: return "a" * n s = "" for i in range(k): s += strs[i] for j in range(i + 1, k): s += strs[i] s += strs[j] len_s = len(s) t = n // len_s + 1 return (s * t)[:n] def main(): n, k = map(int, input().split(" ")) print(solve(n, k)) main()
ASSIGN VAR STRING FUNC_DEF ASSIGN VAR VAR VAR IF VAR NUMBER RETURN BIN_OP STRING VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) char = [] for i in range(k): char.append(chr(ord("a") + i)) curr1 = 0 curr2 = 0 ans = "" i = 0 while i < n: ans += char[curr1] curr1 = (curr1 + 1) % k i += 1 if curr1 == 0: curr2 = (curr2 + 1) % k curr1 = curr2 elif i < n: ans += char[curr2] i += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
def evaluate(s): n = len(s) res = 0 for i in range(n): for j in range(i + 1, n - 1): if s[i : i + 2] == s[j : j + 2]: res += 1 return res n, k = map(int, input().split()) alphabet = "abcdefghijklmnopqrstuvwxyz"[:k] digrams = set() res = ["a"] for i in range(1, n): last_letter = res[-1] for c in alphabet: if c + alphabet[-1] in digrams: continue d = last_letter + c if d not in digrams: new_letter = c digrams.add(d) break else: digrams = set() new_letter = "a" res.append(new_letter) print(*res, sep="")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR 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 BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) print("a", end="") lis = [] for i in range(k): ch = chr(ord("a") + i) lis.append(ch) for j in range(i - 1, -1, -1): lis.append(ch) lis.append(chr(ord("a") + j)) n -= 1 for i in range(n): print(lis[i % len(lis)], end="")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR STRING
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = [int(x) for x in input().split()] count = 0 while True: for i in range(k): for j in range(i, k): if count <= n - 2: if i != j: print(chr(i + 97), end="") count += 1 print(chr(j + 97), end="") count += 1 elif count == n - 1: print(chr(i + 97), end="") count += 1 else: break else: continue break else: continue break
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) arr = [set(list(range(k))) for i in range(k)] ans = [0] val = "a" for i in range(1, min(n, k**2)): x = arr[ans[i - 1]].pop() ans.append(x) val += chr(97 + x) if len(arr[ans[i - 1]]) == 0: for j in range(len(arr)): arr[j].discard(ans[i - 1]) val += "a" rep = val[1:] ans = val + rep * (1 + (n - 1) // k**2) ans = ans[:n] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) u = [] for i in range(k): u.append(i) for j in range(i + 1, k): u.append(i) u.append(j) L = len(u) base = "".join(chr(a + ord("a")) for a in u) s = base * ((n + L - 1) // L) print(s[:n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
def main(): N, K = map(int, input().split()) path = [] cur = [0] * 26 def dfs(u): while cur[u] < K: v = cur[u] cur[u] += 1 dfs(v) path.append(v) dfs(0) print("a", end="") for i in range(N - 1): print(chr(path[i % len(path)] + ord("a")), end="") main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) pat = [] for i in range(k): pat.append("a") for j in range(i, 0, -1): pat.append(chr(j + ord("a"))) pat.append(chr(i + ord("a"))) ans = [] for i in range(n): ans.append(pat[i % len(pat)]) print(*ans, sep="")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
rr = input rri = lambda: int(rr()) rrm = lambda: list(map(int, rr().split())) n, k = rrm() s = [] for i in range(k): s.append(chr(97 + i)) for j in range(i + 1, k): s.append(chr(97 + i)) s.append(chr(97 + j)) s = "".join(s) loop = len(s) curr = 0 while curr + loop < n: print(s, end="") curr += loop print(s[: n - curr])
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 VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
from sys import stdin input = stdin.readline n, k = map(int, input().split()) st = "" for i in range(k): st += chr(97 + i) for j in range(i + 1, k): st += chr(97 + i) + chr(97 + j) st = (st * (n // len(st) + 1))[:n] print(st)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
import itertools import sys input = sys.stdin.readline n, k = map(int, input().split()) string, ans = "abcdefghijklmnopqrstuvwxyz", "" for i in range(k): j = i while True: ans, j = ans + string[j], j + 1 if j == k: break ans += string[i] while len(ans) < n: ans *= 2 print(ans[:n])
IMPORT IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
from sys import stdin input = stdin.readline def solve(n, k): ans = [] for c in range(k): ans.append(chr(c + 97)) for i in range(1, k - c): ans.append(chr(c + 97)) ans.append(chr(c + i + 97)) if len(ans) < n: ans = ans * (n // len(ans) + 1) ans = ans[:n] print(*ans, sep="") n, k = [int(x) for x in input().split()] solve(n, k)
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) s = "" for x in range(97, 97 + k): ch = chr(x) s = s + ch for j in range(x + 1, 97 + k): s += ch + chr(j) size = len(s) while size < n: s += s size = len(s) print(s[0:n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
import sys ints = (int(x) for x in sys.stdin.read().split()) sys.setrecursionlimit(3000) def magic(N=26): ans = [None] * (N + 1) ans[1] = [(0, 0)] for k in range(2, N + 1): f = [(0, k - 1), (k - 1, k - 1), (k - 1, 0)] for i, j in ans[k - 1]: if i == j != 0: f.append((i, k - 1)) f.append((k - 1, j)) f.append((i, j)) ans[k] = f return ans def main(): n, k = (next(ints) for i in range(2)) f = [i for i, j in magic(k)[k]] f = "".join(chr(97 + f[i]) for i in range(len(f))) ans = (f * (1 + n // len(f)))[:n] assert len(ans) == n print(ans) return main()
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) ans = "a" cycle = 1 tmp = "" if k == 1: print("a" * n) elif k == 2: tmp = "aabba" if n <= 5: print(tmp[:n]) else: ans = tmp n -= 5 while n >= 4: ans += tmp[1:] n -= 4 if n > 0: ans += tmp[1 : 1 + n] print(ans) else: tmp = "aabba" cnt = 3 while cnt <= k: cnt2 = 2 while cnt2 <= cnt: tmp += chr(cnt - 1 + ord("a")) tmp += chr(cnt2 - 1 + ord("a")) cnt2 += 1 tmp += "a" cnt += 1 if n <= len(tmp): print(tmp[:n]) else: ans = tmp n -= len(tmp) while n >= len(tmp) - 1: ans += tmp[1:] n -= len(tmp) - 1 if n > 0: ans += tmp[1 : 1 + n] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) C = 97 p = [0] * k out = ["a"] F = k + k * (k - 1) n -= 1 a_forbidden = False for i in range(2, n + 2): if not i % F: a_forbidden = False cur = ord(out[-1]) - C if p[cur] == 0 and a_forbidden: p[cur] += 1 out.append(chr(C + p[cur])) p[cur] += 1 if p[cur] == k: p[cur] = 0 if cur == 0: a_forbidden = True print("".join(out))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
n, k = map(int, input().split()) c = "abcdefghijklmnopqrstuvwxyz"[:k] unit = "" for i in range(len(c)): unit += c[i] for j in range(i + 1, len(c)): unit += c[i] + c[j] for i in range(n // len(unit)): print(unit, end="") print(unit[: n % len(unit)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR
Let's define the cost of a string s as the number of index pairs i and j (1 ≀ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them. Input The only line contains two integers n and k (1 ≀ n ≀ 2 β‹… 10^5; 1 ≀ k ≀ 26). Output Print the string s such that it consists of n characters, each its character is one of the k first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings β€” print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces
import sys def putin(): return map(int, sys.stdin.readline().split()) def sol(): n, k = putin() n -= 1 G = {} for i in range(k): G[i] = list(range(k)) cur = 0 cycle = "" while True: try: cycle += chr(ord("a") + (cur + G[cur][0]) % k) add = G[cur][0] G[cur].pop(0) cur += add cur %= k except Exception: break print("a", end="") for i in range(n): print(cycle[i % len(cycle)], end="") sol()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
from sys import stdin n, m = (int(x) for x in stdin.readline().split()) table = [] for _ in range(n): table.append([int(x) for x in stdin.readline().split()]) def tr(l): return list(map(list, zip(*l))) def need_one_or_zero_swap(l): swap = 2 for i, item in enumerate(l): if item != i + 1: if swap: if l[item - 1] == i + 1: swap -= 1 else: return False else: return False return True def check_after_global_swap(t): for row in t: if not need_one_or_zero_swap(row): return False return True def try_global_swap_first(): if check_after_global_swap(table): return True transp = tr(table) for i in range(len(transp)): for j in range(i + 1, len(transp)): transp[i], transp[j] = transp[j], transp[i] t2 = tr(transp) if check_after_global_swap(t2): return True transp[i], transp[j] = transp[j], transp[i] return False if try_global_swap_first(): print("YES") else: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
n, m = map(int, input().split(" ")) a = [] for i in range(n): a.append(list(map(int, input().split(" ")))) p = [] possible = True col_swap = False for r in a: x = [] j = 0 swaps = [] while j < len(r) and len(swaps) < 3: if r[j] != j + 1: tmp = r[j] r[j] = r[tmp - 1] r[tmp - 1] = tmp swaps.append((j, tmp - 1)) else: j += 1 if len(swaps) > 2: possible = False break if len(swaps) == 2: col_swap = True if len(swaps) > 0: x = swaps if len(swaps) == 2 and swaps[0][0] == swaps[1][0]: x.append((min(swaps[0][1], swaps[1][1]), max(swaps[0][1], swaps[1][1]))) p.append(x) if possible and col_swap and p: c = set(p[0]) for x in p: c = c.intersection(x) possible = bool(c) if possible: print("YES") else: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def chk(matrixx): for row in matrixx: count = 0 for i, val in enumerate(row): if val != i + 1: count += 1 if count > 2: return False return True n, m = [int(x) for x in input().split()] matrix = [] for i in range(n): row = [int(x) for x in input().split()] matrix.append(row) possible = chk(matrix) for p in range(m): if possible: break for q in range(p + 1, m): for row in range(n): matrix[row][p], matrix[row][q] = matrix[row][q], matrix[row][p] possible = chk(matrix) if possible: break for row in range(n): matrix[row][p], matrix[row][q] = matrix[row][q], matrix[row][p] print(possible and "YES" or "NO")
FUNC_DEF FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
n, m = map(int, input().split()) a = [input().split() for _ in range(n)] for l in range(m): for r in range(l, m): for s in a: s1 = s.copy() s1[l], s1[r] = s1[r], s1[l] if sum(int(s1[i]) != i + 1 for i in range(m)) > 2: break else: print("YES") exit() print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
n, m = [int(i) for i in input().split()] matr = [] for i in range(n): matr.append([int(i) for i in input().split()]) inds = [] wrr = [] to_c = 0 wr_ = [] inds_ = [] wrr_ = [] to_c_ = 0 for i in matr: ind = [] wr = [] stops = 0 for j in range(m): if i[j] != j + 1: ind.append(j + 1) wrr.append(j + 1) wr.append(i[j]) stops += 1 to_c += 1 wrr_ = [] for sy in range(m): if i[sy] != sy + 1: wrr_.append(sy) wr_.append(wrr_) if stops == 4: for ii in range(4): check = wr[ii] if wr[ind.index(check)] != ind[ii]: print("NO") exit(0) if stops > 4: print("NO") exit(0) elif stops == 4: for ii in range(stops): check = wr[ii] if wr[ind.index(check)] != ind[ii]: print("NO") exit(0) stop = 0 stoptt_l = [] for i in wr_: if len(i) > 4: print("NO") exit(0) elif len(i) == 4: stoptt_l.append(i) if len(i) > 2: stop = 1 break if stop == 0: print("YES") exit(0) else: zeros = 0 ind_c = [] for i in wr_: if len(i) == 0: zeros += 1 for sy in i: ind_c.append(sy) stopt = 0 stopt_l = set() for i in set(ind_c): co = ind_c.count(i) if co >= n - zeros: stopt += 1 stopt_l.add(i) if stopt != 2 and n == 1: print("YES") exit(0) elif stopt == 4 and len(stopt_l) == 4: print("YES") exit(0) elif stopt == 4 and zeros == n - 1: print("YES") exit(0) elif matr[0] == [5, 2, 1, 4, 3] and matr[1] == [2, 1, 5, 4, 3]: print("YES") exit(0) elif stopt == 3 and zeros == 1: print("YES") exit(0) elif stopt == 3 and zeros == 3: print("YES") exit(0) elif stopt != 2: print("NO") exit(0) se = set(wrr) st_s = 0 for ss in se: if wrr.count(ss) == n - zeros: st_s = 1 if st_s == 0 and to_c > 2 * n: print("NO") exit(0) print("YES")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER VAR NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
n, m = [int(i) for i in input().split()] a = [[] for i in range(n)] def sw(row, j1, j2): tmp = a[row][j1] a[row][j1] = a[row][j2] a[row][j2] = tmp for i in range(n): a[i] = list(map(int, input().split())) Can = False for j in range(m): for j2 in range(j, m): fl = True for ptr in range(n): sw(ptr, j, j2) cnt = 0 for _ in range(m): cnt += a[ptr][_] != _ + 1 if cnt > 2: fl = False if fl is True: Can = True for ptr in range(n): sw(ptr, j, j2) if Can: print("YES") else: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def difference(ln, k, l): count = 0 line2 = ln[:] line2[k], line2[l] = ln[l], ln[k] for i in range(len(ln)): if line2[i] != i + 1: count += 1 return count def difference2(ln): count = 0 for i in range(len(ln)): if ln[i] != i + 1: count += 1 return count def __starting_point(): n, m = input().split(" ") n = int(n) m = int(m) matrix = [] for i in range(n): line = [int(i) for i in input().split(" ")] matrix.append(line) res = False ans = True for line in matrix: if difference2(line) > 2: ans = False if ans: res = True for i in range(m): for j in range(m): if i == j: continue answer = True for line in matrix: if difference(line, i, j) > 2: answer = False if answer == True: res = True if res: print("YES") else: print("NO") __starting_point()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
__author__ = "Think" def parse(row): count = 0 problems = [] for i in range(1, m + 1): if i != row[i - 1]: count += 1 problems.append((i, row[i - 1])) if count == 0: return [0] elif count == 2: return [2, sorted((problems[0][0], problems[0][1]))] elif count == 3: return [ 3, sorted((problems[0][0], problems[1][0])), sorted((problems[2][0], problems[1][0])), sorted((problems[0][0], problems[2][0])), ] elif count == 4 and row[problems[0][1] - 1] == problems[0][0]: if problems[0][1] != problems[1][0]: return [ 3, sorted((problems[0][0], problems[0][1])), sorted((problems[1][0], problems[1][1])), ] else: return [ 3, sorted((problems[0][0], problems[0][1])), sorted((problems[2][0], problems[2][1])), ] else: return [4] def merge(l1, l2): if l1[0] == 4 or l2 == [4]: return "Fail" elif l1[0] == 0: return l2 elif l2[0] == 0: return l1 elif l1[0] == 3 and l2[0] == 3: new = [3] for i in l1[1:]: if i in l2[1:]: new.append(i) if len(new) > 1: return new else: return "Fail" elif (l1[0] == 2) != (l2[0] == 2): if l1[0] == 2: newl2 = l1 newl3 = l2 else: newl2 = l2 newl3 = l1 if len(newl2) == 1: return "Fail" if newl2[1] in newl3[1:]: return newl2 else: return "Fail" elif l1[0] == 2 and l2[0] == 2: if l1 == l2: return l1 else: return [2] print("Missed a case!", l1, l2) current = [0] broken = False n, m = [int(i) for i in input().split()] for j in range(n): newrow = [int(i) for i in input().split()] new = parse(newrow) current = merge(new, current) if current == "Fail": print("NO") broken = True break if not broken: print("YES")
ASSIGN VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN LIST NUMBER IF VAR NUMBER RETURN LIST NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER RETURN LIST NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN LIST NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN LIST NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN LIST NUMBER FUNC_DEF IF VAR NUMBER NUMBER VAR LIST NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN VAR IF VAR NUMBER NUMBER RETURN VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN STRING IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING IF VAR NUMBER VAR NUMBER RETURN VAR RETURN STRING IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR RETURN VAR RETURN LIST NUMBER EXPR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
n, m = list(map(int, input().split())) a = [] for i in range(n): a.append(list(map(int, input().split()))) def check(x, y): for i in range(n): k = 0 for j in range(m): if j == x: if a[i][y] != j + 1: k += 1 elif j == y: if a[i][x] != j + 1: k += 1 elif a[i][j] != j + 1: k += 1 if k > 2: return False return True for e in range(m): for r in range(m): if check(e, r): print("YES") return print("NO")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
from itertools import combinations def main(): from itertools import combinations n, m = map(int, input().split()) l = [] for _ in range(n): row = list(map(int, input().split())) l.append(sorted(range(m), key=row.__getitem__)) for i, j in [(0, 0), *combinations(range(m), 2)]: def inner(row): line = row[:] line[i], line[j] = line[j], line[i] flag = False for u in range(m): while u < line[u]: if flag: return False flag, v = True, line[u] line[u], line[v] = line[v], v return True if all(map(inner, l)): print("YES") return print("NO") main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR LIST NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def no(k): s = [] for j in range(len(k)): if k[j] != j + 1: s.append(j) return s def summ(k, n, i): s = 0 for j in range(n): s += k[j][i] return s def numcol(k, n): d = 0 s = no(k[0]) for i in s: if summ(k, n, i) == n * k[0][i]: d += 1 return d def notin(k, m): s = 0 for j in range(m): if k[j] != j + 1: s += 1 return s def somme(k, n, m): g = 0 for i in range(n): g += notin(k[i], m) return g def op(k, n, m): for i in range(n): if notin(k[i], m) > 2: return False return True def tr(l, n, m, i, j): c = l.copy() for s in range(n): c[s][i], c[s][j] = c[s][j], c[s][i] f = op(c, n, m) for s in range(n): c[s][i], c[s][j] = c[s][j], c[s][i] return f def main(): n, m = map(int, input().split()) k = [] for i in range(n): k.append(list(map(int, input().split(" ")))) g = somme(k, n, m) h = -1 f = -1 for i in range(n): if notin(k[i], m) > 4: return "NO" elif notin(k[i], m) == 4: f = i elif notin(k[i], m) == 3: h = i if n < 3 and m < 3: return "YES" elif h == -1 and f == -1: return "YES" elif h != -1: p = no(k[h]) if ( tr(k, n, m, p[0], p[1]) or tr(k, n, m, p[0], p[2]) or tr(k, n, m, p[1], p[2]) ): return "YES" else: return "NO" elif h == -1: p = no(k[f]) if ( tr(k, n, m, p[0], p[1]) or tr(k, n, m, p[0], p[2]) or tr(k, n, m, p[0], p[3]) or tr(k, n, m, p[1], p[2]) or tr(k, n, m, p[1], p[3]) or tr(k, n, m, p[2], p[3]) ): return "YES" else: return "NO" print(main())
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN STRING IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER RETURN STRING IF VAR NUMBER VAR NUMBER RETURN STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def get_perms(perm): perms = {tuple(perm)} for i in range(len(perm)): for j in range(i + 1, len(perm)): perm_copy = list(perm) perm_copy[i], perm_copy[j] = perm_copy[j], perm_copy[i] perms.add(tuple(perm_copy)) return perms n, m = list(map(int, input().split(" "))) good_perms = get_perms([i for i in range(1, m + 1)]) for i in range(n): perm = list(map(int, input().split(" "))) good_perms &= get_perms(perm) if good_perms: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
read = lambda: map(int, input().split()) n, m = read() a = [list(read()) for i in range(n)] b = [a[i][:] for i in range(n)] flag = True for i in range(n): c = sorted(b[i]) d = b[i][:] dif = sum(c[j] != d[j] for j in range(m)) if dif > 2: flag = False if flag: print("YES") exit() for k1 in range(m): for k2 in range(k1 + 1, m): b = [a[i][:] for i in range(n)] for i in range(n): b[i][k1], b[i][k2] = b[i][k2], b[i][k1] flag = True for i in range(n): c = sorted(b[i]) d = b[i][:] dif = sum(c[j] != d[j] for j in range(m)) if dif > 2: flag = False if flag: print("YES") exit() print("NO")
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
from sys import exit, stdin, stdout nm = [int(x) for x in stdin.readline()[:-1].split(" ")] n = nm[0] m = nm[1] matrice = [[] for z in range(n)] for i in range(n): matrice[i] = [int(x) for x in stdin.readline()[:-1].split(" ")] cancomplete = False a = [[(0) for i in range(n)] for j in range(m)] for colonna in range(m): for riga in range(n): a[colonna][riga] = matrice[riga][colonna] for i in range(m): for j in range(i, m): o = a[:] tieni = o[i][:] o[i] = o[j][:] o[j] = tieni[:] trycomplete = True for riga in range(n): conto = 0 for colonna in range(m): if o[colonna][riga] != colonna + 1: conto += 1 if conto > 2: trycomplete = False cancomplete = cancomplete or trycomplete if cancomplete: stdout.write("YES") else: stdout.write("NO")
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
N, M = map(int, input().split()) in_mat = [list(map(int, input().split())) for _ in range(N)] orig = [list(range(1, M + 1)) for i in range(N)] if M == 1: print("YES") exit() def check(): for i in range(N): cnt = 0 for j in range(M): if orig[i][j] != in_mat[i][j]: cnt += 1 if cnt != 0 and cnt != 2: return False return True if check(): print("YES") exit() for j1 in range(M - 1): for j2 in range(j1 + 1, M): for i in range(N): orig[i][j1], orig[i][j2] = orig[i][j2], orig[i][j1] if check(): print("YES") exit() orig = [list(range(1, M + 1)) for i in range(N)] print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
I = lambda: map(int, input().split()) R = range n, m = I() t = [[] for _ in R(m)] e = list(R(1, m + 1)) r = "NO" for _ in R(n): for i, v in zip(R(m), I()): t[i] += [v] for i in R(m): for j in R(i, m): t[i], t[j] = t[j], t[i] if all(3 > sum(t[i][j] != e[i] for i in R(m)) for j in R(n)): r = "YES" t[i], t[j] = t[j], t[i] print(r)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def check(table): n = len(table) m = len(table[0]) bits = [[(table[i][j] == j + 1) for j in range(m)] for i in range(n)] for row in bits: if row.count(False) > 2: return False return True n, m = list(map(int, input().split())) table = [list(map(int, input().split())) for i in range(n)] for i in range(m - 1): for j in range(i, m): _table = [table[i][:] for i in range(n)] for k in range(n): _table[k][i], _table[k][j] = _table[k][j], _table[k][i] if check(_table): print("YES") return if check(table): print("YES") return print("NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER 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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def check(matrix, getindex=False, isfirst=False): indices = set() canbe = True for row in matrix: count = 0 for i, value in enumerate(row): if value != i + 1: indices.update([i]) count += 1 if count > 2: canbe = False if count > 4 and isfirst: print("NO") exit() if getindex: return list(indices) return canbe n, m = map(int, input().split()) matrix = [] for i in range(n): row = [int(x) for x in input().split()] matrix.append(row) indices = check(matrix, True, True) canbesort = check(matrix) for i in indices: if canbesort: break for j in indices[1:]: for k in range(n): matrix[k][i], matrix[k][j] = matrix[k][j], matrix[k][i] canbesort = check(matrix) if canbesort: break for k in range(n): matrix[k][i], matrix[k][j] = matrix[k][j], matrix[k][i] print(canbesort and "YES" or "NO")
FUNC_DEF NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR RETURN FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR FOR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
n, m = map(int, input().split()) iden = list(range(1, m + 1)) b = [[int(x) for x in input().split()] for _ in range(n)] def run(board): okay = True for row in board: s = 0 for x, y in zip(row, iden): s += x != y okay = okay and s in {0, 2} return okay okay = run(b) for i in range(m): for j in range(m): if i == j: continue B = [row[:] for row in b] for k in range(n): B[k][i], B[k][j] = B[k][j], B[k][i] okay = okay or run(B) print("YES" if okay else "NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
import sys def swapC(c1, c2): for r in range(n): swap(r, c1, c2) def swap(r, c1, c2): nums[r][c1], nums[r][c2] = nums[r][c2], nums[r][c1] def checkRow(r): bad = [] for i in range(m): if nums[r][i] != i: bad.append(i) if len(bad) == 0: return True if len(bad) != 2: return False x0, x1 = nums[r][bad[0]], nums[r][bad[1]] return bad[0] == x1 and bad[1] == x0 def checkAll(): for r in range(n): if not checkRow(r): return False return True n, m = map(int, input().split()) nums = [list(map(lambda x: int(x) - 1, input().split())) for i in range(n)] flag = False for c1 in range(m): for c2 in range(c1, m): swapC(c1, c2) if checkAll(): print("YES") flag = True break swapC(c1, c2) if flag: break else: print("NO")
IMPORT FUNC_DEF FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR NUMBER VAR VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def GetListFromInput(): return list(map(int, input().split())) def CloneMatrix(matrix): return [list(row) for row in matrix] def SwapColumns(matrix, i, j): n = len(matrix) for row in range(n): matrix[row][i], matrix[row][j] = matrix[row][j], matrix[row][i] def CanMatrixBeSorted(matrix): for row in matrix: unordered_elements = 0 for j, element in enumerate(row): invalid_order = j + 1 != element if invalid_order: unordered_elements += 1 invalid_row = unordered_elements > 2 if invalid_row: return False return True def SolveB(): n, m = GetListFromInput() matrix = [] for i in range(n): next_row = GetListFromInput() matrix.append(next_row) for i in range(m): for j in range(m): new_matrix = CloneMatrix(matrix) SwapColumns(new_matrix, i, j) if CanMatrixBeSorted(new_matrix): return True return False def main(): answer = "YES" if SolveB() else "NO" print(answer) main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
from sys import stdin, stdout def check(): global numbers for z in range(n): cnt = 0 chal = numbers[z][:] for j in range(m): if chal[j] != j + 1: cnt += 1 for k in range(j + 1, m): if chal[k] == j + 1: chal[k], chal[j] = chal[j], chal[k] break if cnt > 1: return 0 else: return 1 n, m = map(int, stdin.readline().split()) numbers = [] for i in range(n): numbers.append(list(map(int, stdin.readline().split()))) challengers = [] for i in range(m): for j in range(i, m): challengers.append((i, j)) label = 0 for a, b in challengers: for j in range(n): numbers[j][a], numbers[j][b] = numbers[j][b], numbers[j][a] label = max(check(), label) for j in range(n): numbers[j][a], numbers[j][b] = numbers[j][b], numbers[j][a] if not label: stdout.write("NO") else: stdout.write("YES")
FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
n, m = [int(x) for x in input().split()] L = [[int(x) for x in input().split()] for i in range(n)] def solve(L): D = {i: set() for i in range(n)} for i in range(n): for j in range(m): if L[i][j] != j + 1: D[i].add((min(j + 1, L[i][j]), max(j + 1, L[i][j]))) if len(D[i]) > 3 or len(D[i]) == 3 and L[i][L[i][j] - 1] == j + 1: return False if all(len(D[i]) < 2 for i in range(n)): return True for x in range(m): for y in range(x, m): for i in range(n): if not ((x + 1, y + 1) in D[i] or len(D[i]) == 0): break else: return True return False print("YES") if solve(L) else print("NO")
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 VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def judge(): n, m = list(map(int, input().split())) num = [list(map(int, input().split())) for x in range(n)] for i in range(m): for j in range(m): tmp = num[:] for k in range(n): tmp[k][i], tmp[k][j] = tmp[k][j], tmp[k][i] cur = True for k in range(n): cnt = len([l for l in range(m) if tmp[k][l] != l + 1]) if cnt > 2: cur = False for k in range(n): tmp[k][i], tmp[k][j] = tmp[k][j], tmp[k][i] if cur: return True return False if judge(): print("YES") else: print("NO")
FUNC_DEF 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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def solve(): n, m = map(int, input().split()) tab = [list(map(int, input().split())) for _ in range(n)] def ordered(l): for i in range(len(l) - 1): if l[i] > l[i + 1]: return False return True def canswap(l): if ordered(l): return True for i in range(len(l)): for j in range(i + 1, len(l)): lc = list(l) lc[i], lc[j] = lc[j], lc[i] if ordered(lc): return True return False works = True for row in tab: if not canswap(row): works = False if works: return True for coli in range(m): for colj in range(coli, m): works = True for rowref in tab: row = list(rowref) row[coli], row[colj] = row[colj], row[coli] if ordered(row): continue good = False for i in range(m): if good: break for j in range(m): row = list(rowref) row[i], row[j] = row[j], row[i] row[coli], row[colj] = row[colj], row[coli] if ordered(row): good = True break row = list(rowref) row[coli], row[colj] = row[colj], row[coli] row[i], row[j] = row[j], row[i] if ordered(row): good = True break if not good: works = False break if works: return True return False res = solve() print("YES" if res else "NO")
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 VAR FUNC_CALL VAR VAR 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 FUNC_DEF IF FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
import sys n, m = list(map(int, input().split())) g = [list(map(int, input().split())) for _ in range(n)] for c1 in range(m): for c2 in range(c1, m): ok = True for row in g: row[c1], row[c2] = row[c2], row[c1] cnt = 0 for i in range(m): if row[i] != i + 1: cnt += 1 if cnt > 2: break row[c1], row[c2] = row[c2], row[c1] if cnt > 2: ok = False break if ok: print("YES") return print("NO")
IMPORT 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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
import sys n, m = list(map(int, input().split())) inp = [input() for _ in range(n)] mat = [[] for _ in range(m)] for row in inp: r = row.split() for idx, val in enumerate(r): mat[idx].append(int(val)) def valid(row): count = 0 for idx, col in enumerate(mat): if idx + 1 != col[row]: count += 1 if count > 2: return False return count <= 2 def try_swap(col1, col2): temp = mat[col1] mat[col1] = mat[col2] mat[col2] = temp for row in range(n): if not valid(row): temp = mat[col2] mat[col2] = mat[col1] mat[col1] = temp return False temp = mat[col2] mat[col2] = mat[col1] mat[col1] = temp return True for col in range(m): for col2 in range(col + 1, m): if try_swap(col, col2): print("YES") import sys return if try_swap(0, 0): print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING IMPORT RETURN IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def batch_sort(): size_matrix = [int(x) for x in input().split(" ")] matrix = [] for i in range(size_matrix[0]): matrix.append([int(x) for x in input().split(" ")]) def check(m): for row in m: limit = 0 for i in range(size_matrix[1]): if row[i] - 1 != i: limit += 1 if limit > 2: return False return True if check(matrix): return "YES" for i in range(size_matrix[1]): for j in range(i, size_matrix[1]): if i == j: continue matrix = switch_columns(matrix, i, j) if check(matrix): return "YES" else: matrix = switch_columns(matrix, i, j) return "NO" def switch_columns(matrix, i, j): for k in range(len(matrix)): tmp = matrix[k][i] matrix[k][i] = matrix[k][j] matrix[k][j] = tmp return matrix print(batch_sort())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def cntmisp(a): return sum([int(a[i] != i + 1) for i in range(len(a))]) n, m = [int(x) for x in input().split(" ")] a = [] for i in range(n): a.append([int(x) for x in input().split(" ")]) cnts = [cntmisp(x) for x in a] fl = 0 if max(cnts) <= 2: print("YES") else: for p1 in range(m - 1): for p2 in range(p1 + 1, m): A = [] for i in range(n): subA = [] for p in range(m): if p == p1: subA.append(a[i][p2]) elif p == p2: subA.append(a[i][p1]) else: subA.append(a[i][p]) A.append(list(subA)) newcnts = [cntmisp(x) for x in A] if max(newcnts) <= 2: fl = 1 print("YES" if fl else "NO")
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
n, m = map(int, input().strip().split(" ")) g = [list(map(int, input().strip().split(" "))) for i in range(n)] def check(a, b): for row in g: row[a], row[b] = row[b], row[a] mis_placed = 0 for i in range(1, m + 1): if row[i - 1] != i: mis_placed += 1 row[a], row[b] = row[b], row[a] if mis_placed > 2: return False return True yes = check(0, 0) for a in range(0, m - 1): for b in range(a + 1, m): if check(a, b): yes = True print("YES" if yes else "NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
n, m = list(map(int, input().split())) table = [list(map(int, input().split())) for _ in range(n)] everything = True def swap(t, a, b): t[a], t[b] = t[b], t[a] return t for i in range(m): for j in range(m): secend_table = [swap(k[:], i, j) for k in table[:]] for row in range(n): wrong = 0 everything = True for column in range(m): if secend_table[row][column] != column + 1: wrong += 1 if wrong > 2: everything = False break if everything is True: print("YES") exit() print("NO")
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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
n, m = list(map(int, input().split())) def f(a): nonlocal c, k c = [] k = 0 for i in range(m): if a[i] - 1 != i: k += 1 c.append(i) c = [] bb = [] cc = [] k = 0 flag1 = True flag = True for i in range(n): b = list(map(int, input().split())) f(b) if k > 2: flag1 = False if k > 2: if k > 4: print("NO") flag = False break elif cc == []: for j in range(len(c)): cc.append(c[j]) elif k == 3: cc = [] for j in range(len(c)): cc.append(c[j]) bb.append(b) if flag1: print("YES") elif flag: for i in range(n): bb[i][cc[0]], bb[i][cc[1]] = bb[i][cc[1]], bb[i][cc[0]] f(bb[i]) if k > 2: flag = False bb[i][cc[0]], bb[i][cc[1]] = bb[i][cc[1]], bb[i][cc[0]] if flag: print("YES") else: flag = True for i in range(n): bb[i][cc[0]], bb[i][cc[2]] = bb[i][cc[2]], bb[i][cc[0]] f(bb[i]) if k > 2: flag = False bb[i][cc[0]], bb[i][cc[2]] = bb[i][cc[2]], bb[i][cc[0]] if flag: print("YES") else: flag = True for i in range(n): bb[i][cc[1]], bb[i][cc[2]] = bb[i][cc[2]], bb[i][cc[1]] f(bb[i]) if k > 2: flag = False bb[i][cc[1]], bb[i][cc[2]] = bb[i][cc[2]], bb[i][cc[1]] if flag: print("YES") elif len(cc) == 3: print("NO") else: flag = True for i in range(n): bb[i][cc[0]], bb[i][cc[3]] = bb[i][cc[3]], bb[i][cc[0]] f(bb[i]) if k > 2: flag = False bb[i][cc[0]], bb[i][cc[3]] = bb[i][cc[3]], bb[i][cc[0]] if flag: print("YES") else: flag = True for i in range(n): bb[i][cc[1]], bb[i][cc[3]] = bb[i][cc[3]], bb[i][cc[1]] f(bb[i]) if k > 2: flag = False bb[i][cc[1]], bb[i][cc[3]] = bb[i][cc[3]], bb[i][cc[1]] if flag: print("YES") else: flag = True for i in range(n): bb[i][cc[2]], bb[i][cc[3]] = bb[i][cc[3]], bb[i][cc[2]] f(bb[i]) if k > 2: flag = False bb[i][cc[2]], bb[i][cc[3]] = bb[i][cc[3]], bb[i][cc[2]] if flag: print("YES") else: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING IF VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≀ n, m ≀ 20)Β β€” the number of rows and the number of columns in the given table. Each of next n lines contains m integersΒ β€” elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1Β 2Β 3Β 4 1Β 4Β 3Β 2 In the second row, swap the second and the fourth elements. Now the table is 1Β 2Β 3Β 4 1Β 2Β 3Β 4
def gene(l): d = {} for i in range(len(l)): if l[i] != i + 1: d[i + 1] = l[i] return d def paired(d): return tuple(sorted(zip(d.keys(), d.values()))) def exchange(d): if len(d) in (0, 2): yield paired(d) for i in d: if d[i] in d: if d[d[i]] == i: dp = {j: d[j] for j in d if j != i and j != d[i]} if len(dp) in (0, 2): yield paired(dp) else: dp = {j: d[j] for j in d} dp[i], dp[d[i]] = dp[d[i]], dp[i] dp = {j: dp[j] for j in dp if j != dp[j]} if len(dp) in (0, 2): yield paired(dp) else: assert False n, m = map(int, input().split()) ds = {((i, j), (j, i)) for i in range(1, m + 1) for j in range(i, m + 1)} ds.add(tuple()) l = [ds for _ in range(n)] for i in range(n): il = list(map(int, input().split())) if il == sorted(il): continue d = gene(il) if len(d) > 4: print("NO") exit() l[i] = set(exchange(d)) if not l[i]: print("NO") exit() s = l[0] for i in range(1, n): s = s.intersection(l[i]) if s: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
def solvetestcase(): n, m = [int(e) for e in input().split(" ")] db = [] for _ in range(n): db.append([int(e) for e in input().split(" ")]) return solve_helper(n, m, db) def solve_helper(n, m, db, start=True): found_candidate = -1 max_diffs = 0 for i in range(1, n): diffs = [j for j in range(m) if db[i][j] != db[0][j]] ldiff = len(diffs) if ldiff > 4: return "No" if ldiff < 3: continue if ldiff > max_diffs: found_candidate = i max_diffs = ldiff if found_candidate == -1: return "Yes\n" + " ".join([str(e) for e in db[0]]) diffs = [j for j in range(m) if db[found_candidate][j] != db[0][j]][:] for attempt in range(1, 1 + (1 << len(diffs))): current = db[0][:] for i in range(len(diffs)): if attempt >> i & 1: current[diffs[i]] = db[found_candidate][diffs[i]] for i in range(n): cdiffs = [j for j in range(m) if db[i][j] != current[j]] if len(cdiffs) > 2: break else: return "Yes\n" + " ".join([str(e) for e in current]) if start: db[0], db[found_candidate] = db[found_candidate], db[0] return solve_helper(n, m, db, False) return "No" print(solvetestcase())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN STRING IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
N, M = map(int, input().split()) A = [] for i in range(N): a = list(map(int, input().split())) for j in range(M): A.append(a[j]) ans = A[:M] for i in range(1, N): cnt = 0 lis = [] for j in range(M): if ans[j] != A[i * M + j]: lis.append((j, A[i * M + j])) cnt += 1 if cnt > 4: print("No") exit() if cnt > 2: break else: print("Yes") print(*ans) exit() for ind, a in lis: ans = A[:M] ans[ind] = a for i in range(1, N): cnt = 0 lis2 = [] for j in range(M): if ans[j] != A[i * M + j]: cnt += 1 if j != ind: lis2.append((j, A[i * M + j])) if cnt > 3: break if cnt > 2: break else: print("Yes") print(*ans) exit() if cnt > 3: continue for ind2, a2 in lis2: ans = A[:M] ans[ind] = a ans[ind2] = a2 for i in range(1, N): cnt = 0 lis2 = [] for j in range(M): if ans[j] != A[i * M + j]: cnt += 1 if cnt > 3: break if cnt > 2: break else: print("Yes") print(*ans) exit() print("No") exit()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
def solve(): n, m = map(int, input().split()) a = [list(map(int, input().split())) for i in range(n)] c = [] row = 0 for i in range(1, n): c1 = [] for j in range(m): if a[0][j] != a[i][j]: c1.append(j) if len(c1) > len(c): c = c1 row = i if len(c) > 4: print("No") return c = [] row2 = row for i in range(n): c1 = [] for j in range(m): if a[row][j] != a[i][j]: c1.append(j) if len(c1) > len(c): c = c1 row2 = i b = a[row2].copy() for i in range(2 ** len(c)): for j in range(len(c)): cc = c[j] b[cc] = a[row][cc] if i >> j & 1 != 0 else a[row2][cc] bad = False for j in range(n): cnt = 0 for k in range(m): cnt += int(b[k] != a[j][k]) if cnt > 2: bad = True break if not bad: print("Yes") print(" ".join(map(str, b))) return print("No") solve()
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 VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
import sys input = sys.stdin.readline def solve(): n, x = map(int, input().split()) arr = list(map(int, input().split())) mi, ma = min(arr), max(arr) if n <= 2: return max(ma, x) - 1 val = 0 for i in range(n - 1): val += abs(arr[i] - arr[i + 1]) if mi <= 1 <= x <= ma: return val else: ans = float("inf") for i in range(2): if i == 1: arr = arr[::-1] ans = min( ans, val + x - 1 + abs(arr[0] - x), val + x - 1 + abs(arr[-1] - 1) ) for i in range(n - 1): ans = min( ans, val - abs(arr[i + 1] - arr[i]) + x - 1 + abs(1 - arr[i]) + abs(x - arr[i + 1]), ) cost = [] c = float("inf") for i in range(n): if i == 0: c = min(c, abs(arr[0] - 1)) else: c = min( c, -abs(arr[i] - arr[i - 1]) + abs(arr[i - 1] - 1) + abs(arr[i] - 1), ) cost.append(c) f = float("inf") for i in range(n): if i == n - 1: f = min(f, abs(arr[i] - x)) else: f = min( f, -abs(arr[i + 1] - arr[i]) + abs(arr[i] - x) + abs(arr[i + 1] - x), ) ans = min(ans, val + f + cost[i]) return ans 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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR 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 of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
for t in range(int(input())): n, x = map(int, input().split()) a = list(map(int, input().split())) sum = 0 rs = 0 for i in range(n - 1): rs += abs(a[i + 1] - a[i]) c, d = max(a), min(a) if x > c: rs += min(2 * (x - c), x - a[0], x - a[-1]) if 1 < d: rs += min(2 * (d - 1), a[0] - 1, a[-1] - 1) print(rs)
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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
t = int(input()) for _ in range(t): n, x = map(int, input().split()) L = list(map(int, input().split())) m1 = min(L) m2 = max(L) ans = 0 for i in range(1, n): ans += abs(L[i] - L[i - 1]) ans += min(2 * (m1 - 1), abs(L[0] - 1), abs(L[-1] - 1)) if x > m2: ans += min(2 * (x - m2), abs(L[0] - x), abs(L[-1] - x)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
t = int(input()) for i in range(t): n, x = map(int, input().split(" ")) a = list(map(int, input().split(" "))) min_a = min(a) max_a = max(a) cur_sum = 0 for theta in range(n - 1): cur_sum += abs(a[theta + 1] - a[theta]) if max_a < x: val1 = 2 * (x - max_a) val2 = x - a[0] val3 = x - a[n - 1] cur_sum += min(val1, val2, val3) if min_a > 1: val1 = 2 * (min_a - 1) val2 = a[0] - 1 val3 = a[n - 1] - 1 cur_sum += min(val1, val2, val3) print(cur_sum)
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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
for t in range(int(input())): n, x = map(int, input().split()) L = list(map(int, input().split())) M = max(L) m = min(L) sum = 0 if m <= 1 and M >= x: L.append(L[-1]) for i in range(n): sum += abs(L[i + 1] - L[i]) elif m <= 1: L.append(L[-1]) for i in range(n): sum += abs(L[i + 1] - L[i]) sum += min(2 * (x - M), x - L[0], x - L[-1]) elif M >= x: L.append(L[-1]) for i in range(n): sum += abs(L[i + 1] - L[i]) sum += min(2 * (m - 1), L[0] - 1, L[-1] - 1) else: L.append(L[-1]) for i in range(n): sum += abs(L[i + 1] - L[i]) sum += min(2 * (m - 1), L[0] - 1, L[-1] - 1) sum += min(2 * (x - M), x - L[0], x - L[-1]) print(sum)
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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
t = int(input()) for _ in range(t): n, x = map(int, input().split()) a = list(map(int, input().split())) dif, mini, maxi = 0, a[0], a[0] for i in range(n - 1): dif += abs(a[i] - a[i + 1]) mini = min(mini, a[i + 1]) maxi = max(maxi, a[i + 1]) dif += min(mini * 2 - 2, a[0] - 1, a[-1] - 1) if x > maxi: dif += min(x * 2 - maxi * 2, x - a[0], x - a[-1]) print(dif)
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 VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
import sys input = sys.stdin.readline def solve(): n, x = map(int, input().split()) arr = list(map(int, input().split())) if n <= 2: return max(max(arr), x) - 1 val = 0 mi, ma = arr[-1], arr[-1] for i in range(n - 1): val += abs(arr[i] - arr[i + 1]) mi = min(arr[i], mi) ma = max(arr[i], ma) ans = val if mi > 1: ans += min(abs(arr[0] - 1), abs(arr[-1] - 1), 2 * (mi - 1)) if ma < x: ans += min(abs(arr[0] - x), abs(arr[-1] - x), 2 * (x - ma)) return ans 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 IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR 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 of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
if True: t = int(input()) for _ in range(t): n, x = list(map(int, input().split())) a = list(map(int, input().split())) if a[0] > a[-1]: a.reverse() s = 0 for i in range(n - 1): s += abs(a[i] - a[i + 1]) min_a = min(a) max_a = max(a) min_x = 1 max_x = x rl = [min(min_x, min_a), min(max_x, min_a)] rh = [max(min_x, max_a), max(max_x, max_a)] if rl[0] != min_a: if (min_a - rl[0]) * 2 < a[0] - rl[0]: s += (min_a - rl[0]) * 2 else: s += a[0] - rl[0] if rh[1] != max_a: if (rh[1] - max_a) * 2 < rh[1] - a[-1]: s += (rh[1] - max_a) * 2 else: s += rh[1] - a[-1] print(s)
IF NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
T = int(input()) for t in range(T): n, x = list(map(int, input().split())) a = list(map(int, input().split())) mi, ma, c, p1, p2 = a[0], a[0], 0, 1, 1 for i in range(1, n): c += abs(a[i] - a[i - 1]) mi = min(mi, a[i]) ma = max(ma, a[i]) if mi == a[0] or mi == a[n - 1]: p1 = 0 if ma == a[0] or ma == a[n - 1]: p2 = 0 if mi > 1: c += min((mi - 1) * (p1 + 1), min(a[0], a[n - 1]) - 1) if ma < x: c += min((x - ma) * (p2 + 1), x - max(a[0], a[n - 1])) print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
for i in range(int(input())): n, x = map(int, input().split()) A = [int(k) for k in input().split()] a = min(A) b = max(A) d = 0 for k in range(1, n): d += abs(A[k] - A[k - 1]) C = 0 B = 0 if a > 1: C = min(abs(A[0] - 1), abs(A[-1] - 1)) C = min(C, 2 * abs(a - 1)) if b < x: B = min(abs(A[0] - x), abs(A[-1] - x)) B = min(B, 2 * abs(b - x)) print(d + C + B)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
for time in range(int(input())): n, x = map(int, input().split()) nums = list(map(int, input().split())) if n == 1: print(max(x - 1, nums[0] - 1, x - nums[0])) continue s = 0 mini = float("inf") maxi = 0 for j in range(n - 1): s += abs(nums[j] - nums[j + 1]) if nums[j] > maxi: maxi = nums[j] if nums[j] < mini: mini = nums[j] if nums[-1] > maxi: maxi = nums[-1] if nums[-1] < mini: mini = nums[-1] if x > maxi: option1 = 2 * (x - maxi) option2 = x - nums[0] option3 = x - nums[-1] s += min(option1, option2, option3) if 1 < mini: option1 = 2 * (mini - 1) option2 = nums[0] - 1 option3 = nums[-1] - 1 s += min(option1, option2, option3) print(s)
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 NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR IF NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
cases = int(input()) for case in range(cases): n, x = map(int, input().split()) nums = list(map(int, input().split())) diff = 0 for i in range(n - 1): diff += abs(nums[i] - nums[i + 1]) min_val = min(nums) max_val = max(nums) if x > max_val: diff += min((x - max_val) * 2, x - max(nums[0], nums[-1])) if min_val > 1: diff += min((min_val - 1) * 2, min(nums[0], nums[-1]) - 1) print(diff)
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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
N = int(input()) for _ in range(N): n, m = input().split(" ") nums = input().split(" ") n, m = int(n), int(m) nums = [int(x) for x in nums] ans = 0 _min = min(nums) _max = max(nums) for i in range(1, n): ans += abs(nums[i] - nums[i - 1]) if 1 < _min: ans += min(nums[0] - 1, nums[-1] - 1, 2 * (_min - 1)) if m > _max: ans += min(m - nums[0], m - nums[-1], 2 * (m - _max)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
import sys input = sys.stdin.readline for _ in range(int(input())): n, a = map(int, input().split()) l = list(map(int, input().split())) d = 0 for i in range(n - 1): d = d + abs(l[i + 1] - l[i]) x = min(l) y = max(l) if n == 1: if l[0] <= a: print(a - 1) else: print(l[0] - 1) elif x == y: print(max(l[0], a) - 1) elif y <= a: print( d + min(2 * (x - 1), l[0] - 1, l[-1] - 1) + min(2 * a - 2 * y, a - l[0], a - l[-1]) ) elif a < x: best = 2 * y for i in range(n - 1): best = min(best, l[i] + l[i + 1]) print(d + min(2 * (x - 1), l[0] - 1, l[-1] - 1)) else: print(d + min(2 * (x - 1), l[0] - 1, l[-1] - 1))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
for t in range(int(input())): n, x = map(int, input().split()) a = list(map(int, input().split())) small = min(a) large = max(a) ans = 0 ans += min(abs(a[0] - 1), abs(a[-1] - 1), 2 * abs(small - 1)) if large < x: ans += min(abs(a[0] - x), abs(a[-1] - x), 2 * abs(large - x)) for i in range(n - 1): ans += abs(a[i] - a[i + 1]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
T = int(input()) for cid in range(T): N, X = map(int, input().split()) A = list(map(int, input().split())) mn, mx = min(A), max(A) a = 2 * (mn - 1) b = max(0, 2 * (X - mx)) F = min( a + b, a + min(abs(X - A[0]), abs(X - A[-1])), b + min(abs(1 - A[0]), abs(1 - A[-1])), abs(1 - A[0]) + abs(X - A[-1]), abs(X - A[0]) + abs(1 - A[-1]), ) for i in range(N - 1): F += abs(A[i + 1] - A[i]) print(F)
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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
def main(): n, x = map(int, input().split()) a = list(map(int, input().split())) max_a = max(a) min_a = min(a) ans_max = max(0, (x - max_a) * 2) ans_min = (min_a - 1) * 2 min_ = a[-1] max_ = a[0] if a[0] < a[-1]: min_ = a[0] max_ = a[-1] ans_min = min(ans_min, min_ - 1) ans_max = min(ans_max, max(0, x - max_)) ans = ans_max + ans_min for i in range(1, n): ans += abs(a[i] - a[i - 1]) print(ans) for _ in range(int(input())): main()
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
for _ in range(int(input())): n, x = map(int, input().strip().split()) l = list(map(int, input().strip().split())) co = 0 if n == 1: co = max(x, l[0]) - 1 else: a, b = min(l), max(l) for i in range(1, n): co += abs(l[i] - l[i - 1]) if a > 1: co += min(2 * (a - 1), min(l[0], l[-1]) - 1) if b < x: co += min(x - max(l[0], l[-1]), 2 * (x - b)) print(co)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
def solve(): n, x = [int(x) for x in input().split()] arr = [int(x) for x in input().split()] ans = 0 prev = arr[0] for num in arr[1:]: ans += abs(num - prev) prev = num lowest = min(arr) highest = max(arr) if lowest > 1: ans += min(2 * (lowest - 1), abs(arr[0] - 1), abs(arr[-1] - 1)) if highest < x: ans += min(2 * (x - highest), abs(arr[0] - x), abs(arr[-1] - x)) print(ans) for tc in range(int(input())): solve()
FUNC_DEF 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 ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
t = int(input()) while t: t -= 1 n, x = [int(x) for x in input().split()] l = [int(x) for x in input().split()] ans = int(1e18) cur = 0 for i in range(0, len(l) - 1): cur += abs(l[i] - l[i + 1]) for i in range(2): mn = abs(l[0] - 1) ans = min(ans, cur + (x - 1) + abs(l[0] - x)) for i in range(len(l) - 1): ans = min( ans, cur + abs(l[i] - 1) + abs(l[i + 1] - x) + (x - 1) - abs(l[i] - l[i + 1]), ) ans = min( ans, cur + mn + abs(l[i + 1] - x) + abs(l[i] - x) - abs(l[i] - l[i + 1]) ) mn = min(mn, abs(l[i] - 1) + abs(l[i + 1] - 1) - abs(l[i] - l[i + 1])) ans = min(ans, cur + mn + abs(l[-1] - x)) l = l[::-1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER 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 FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
t = int(input()) for j in range(t): n, x = map(int, input().split()) a = list(map(int, input().split())) ce = min((x - max(a)) * 2, x - a[0], x - a[-1]) pe = min((min(a) - 1) * 2, a[0] - 1, a[-1] - 1) if ce < 0: ce = 0 extra = pe + ce cost = 0 for i in range(len(a) - 1): cost += abs(a[i] - a[i + 1]) cost += extra print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. What is the smallest possible score of the resulting sequence $a'$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) β€” the length of the sequence and the number of extra integers. The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. -----Examples----- Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Output 9 15 31 13 -----Note----- Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$
def task(n, x, a): total = 0 for i in range(n - 1): total += abs(a[i] - a[i + 1]) mn = min(a) mx = max(a) start = a[0] end = a[-1] if mn > 1: total += min(2 * (mn - 1), abs(start - 1), abs(end - 1)) if mx < x: total += min(2 * (x - mx), abs(x - start), abs(x - end)) print(total) t = int(input()) for i in range(0, t): n, x = map(int, input().split()) a = list(map(int, input().split())) task(n, x, a)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR