description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
def check(stx, sty, x, y): for i in range(stx, stx + x): for j in range(sty, sty + y): if g[i][j] != g[stx][sty]: return False return True n, m = map(int, input().split()) g = [input() for i in range(n)] if ( n % 3 == 0 and check(0, 0, n // 3, m) and check(n // 3, 0, n // 3, m) and check(n // 3 * 2, 0, n // 3, m) and g[0][0] != g[n // 3 * 2][0] and g[0][0] != g[n // 3][0] and g[n // 3][0] != g[n // 3 * 2][0] or m % 3 == 0 and check(0, 0, n, m // 3) and check(0, m // 3, n, m // 3) and check(0, m // 3 * 2, n, m // 3) and g[0][0] != g[0][m // 3] and g[0][0] != g[0][m // 3 * 2] and g[0][m // 3] != g[0][m // 3 * 2] ): print("YES") else: print("NO")
FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
def is_horizontal(): if n % 3 == 0: colors = {} diff_colors = set() stripe_hight = n // 3 for k in range(3): colors[k] = flag[k * stripe_hight][0] diff_colors.add(colors[k]) for i in range(k * stripe_hight, k * stripe_hight + stripe_hight): for j in range(m): if flag[i][j] != colors[k]: return False if len(diff_colors) != 3: return False return True def is_vertical(): if m % 3 == 0: colors = {} diff_colors = set() width = m // 3 for k in range(3): colors[k] = flag[0][k * width] diff_colors.add(colors[k]) for j in range(k * width, k * width + width): for i in range(n): if flag[i][j] != colors[k]: return False if len(diff_colors) != 3: return False return True def is_flag(): if n % 3 == 0 or m % 3 == 0: if is_horizontal() or is_vertical(): return "YES" return "NO" n, m = map(int, input().split()) flag = [] for i in range(n): flag.append(input()) print(is_flag())
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR RETURN STRING RETURN STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = (int(i) for i in input().split()) flag = [] for i in range(n): flag += [input()] count = {"R": 0, "G": 0, "B": 0} for line in flag: for let in line: count[let] += 1 check1 = True change1 = 0 for i in range(n): if i < n - 1 and flag[i][0] != flag[i + 1][0]: change1 += 1 for j in range(m): if j < m - 1 and flag[i][j] != flag[i][j + 1]: check1 = False if change1 != 2 or len({count["R"], count["G"], count["B"]}) > 1: check1 = False check2 = True change2 = 0 for j in range(m): if j < m - 1 and flag[0][j] != flag[0][j + 1]: change2 += 1 for i in range(n): if i < n - 1 and flag[i][j] != flag[i + 1][j]: check2 = False if change2 != 2 or len({count["R"], count["G"], count["B"]}) > 1: check2 = False if check2 or check1: 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 VAR LIST FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR STRING VAR STRING VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR STRING VAR STRING VAR STRING NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = list(map(int, input().split())) field = [input() for i in range(n)] if n % 3 == 0: size = n // 3 flag = True block = set() stripes = set() for i in range(n): if i % size == 0: block = set() for j in range(m): block.add(field[i][j]) if (i + 1) % size == 0: if len(block) > 1: flag = False else: stripes.add(list(block)[0]) if len(stripes) != 3: flag = False if flag: print("YES") return if m % 3 == 0: size = m // 3 flag = True block = set() stripes = set() for j in range(m): if j % size == 0: block = set() for i in range(n): block.add(field[i][j]) if (j + 1) % size == 0: if len(block) > 1: flag = False else: stripes.add(list(block)[0]) if len(stripes) != 3: flag = False if flag: print("YES") return print("NO")
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 IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING RETURN IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = map(int, input().split()) fl = [input() for i in range(n)] c1 = fl[0][0] BOOL = False for i in range(n): if fl[i][0] != c1: BOOL = True break if BOOL: BOOL = False if n % 3 == 0: for i in range(n // 3)[: n // 3]: for j in range(m): if fl[i][j] != c1: BOOL = True c2 = fl[n // 3][0] if not BOOL: for i in range(n)[n // 3 : -(n // 3)]: for j in range(m): if fl[i][j] != c2: BOOL = True c3 = fl[-(n // 3)][0] if not BOOL: for i in range(n)[-(n // 3) :]: for j in range(m): if fl[i][j] != c3: BOOL = True if c1 == c2 or c2 == c3 or c1 == c3: print("NO") elif BOOL: print("NO") else: print("YES") else: print("NO") elif m % 3 == 0: for i in range(m)[: m // 3]: for j in range(n): if fl[j][i] != c1: BOOL = True c2 = fl[0][m // 3] if not BOOL: for i in range(m)[m // 3 : -(m // 3)]: for j in range(n): if fl[j][i] != c2: BOOL = True c3 = fl[0][-(m // 3)] if not BOOL: for i in range(m)[-(m // 3) :]: for j in range(n): if fl[j][i] != c3: BOOL = True if c1 == c2 or c2 == c3 or c1 == c3: print("NO") elif BOOL: print("NO") else: print("YES") else: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, _ = map(int, input().split()) s = input() n -= 1 a = "" k = 0 if "R" in s: k += 1 if "G" in s: k += 1 if "B" in s: k += 1 t = 1 if k > 1: a = s for _ in range(n): s1 = input() if s1 != s: t = 0 break elif k == 1: a += s[0] for _ in range(n): s = input() a += s[0] k = 0 if "R" in s: k += 1 if "G" in s: k += 1 if "B" in s: k += 1 if k != 1: t = 0 break def f(a): if len(a) % 3 > 0: return 1 b = len(a) // 3 if a[0] * b + a[b] * b + a[-1] * b != a: return 1 k = 0 if "R" in a: k += 1 if "G" in a: k += 1 if "B" in a: k += 1 if k != 3: return 1 return 0 if t == 0 or f(a): print("NO") else: print("YES")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER IF STRING VAR VAR NUMBER IF STRING VAR VAR NUMBER IF STRING VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR VAR NUMBER IF STRING VAR VAR NUMBER IF STRING VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF STRING VAR VAR NUMBER IF STRING VAR VAR NUMBER IF STRING VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
def satisfy_line(line): total = len(line) size = total // 3 if total % 3 != 0: return False first_part = line[0:size] second_part = line[size : 2 * size] third_part = line[2 * size : 3 * size] first_set = set(first_part) second_set = set(second_part) third_set = set(third_part) if len(first_set) == len(second_set) == len(third_set) == 1: all_color = set().union(first_set, second_set, third_set) if all_color == {"R", "G", "B"}: return True return False def satisfy_flag(flag): first_line = flag[0] if not satisfy_line(first_line): return False for line in flag: if line != first_line: return False return True def rotate(flag, n, m): rotated_flag = [] for i in range(m): line = [] for j in range(n): line.append(flag[j][i]) rotated_flag.append(line) return rotated_flag def main(): n, m = [int(t) for t in input().split()] flag = [input() for _ in range(n)] if satisfy_flag(flag): print("YES") elif satisfy_flag(rotate(flag, n, m)): print("YES") else: print("NO") def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR IF VAR STRING STRING STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = [list(input().rstrip()) for i in range(n)] tate = True yoko = True if n < 3: yoko = False if m < 3: tate = False for i in range(n - 1): if a[i][0] != a[i + 1][0]: tate = False for j in range(m - 1): if a[0][j] != a[0][j + 1]: yoko = False if not tate ^ yoko: print("NO") exit() if tate: if m % 3 != 0: print("NO") exit() c1 = a[0][0] c2 = a[0][m // 3] c3 = a[0][2 * m // 3] if c1 == c2 or c2 == c3 or c3 == c1: print("NO") exit() flag = True for i in range(n): for j in range(m // 3): if c1 != a[i][j]: flag = False for j in range(m // 3, 2 * m // 3): if c2 != a[i][j]: flag = False for j in range(2 * m // 3, m): if c3 != a[i][j]: flag = False if flag: print("YES") else: print("NO") else: if n % 3 != 0: print("NO") exit() c1 = a[0][0] c2 = a[n // 3][0] c3 = a[2 * n // 3][0] if c1 == c2 or c2 == c3 or c3 == c1: print("NO") exit() flag = True for j in range(m): for i in range(n // 3): if c1 != a[i][j]: flag = False for i in range(n // 3, 2 * n // 3): if c2 != a[i][j]: flag = False for i in range(2 * n // 3, n): if c3 != a[i][j]: flag = False if flag: print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = map(int, input().split()) A = [input() for _ in range(n)] if n % 3 == 0: k = n // 3 s1 = set() s2 = set() s3 = set() for i in range(k): for j in range(m): s1.add(A[i][j]) for i in range(k, 2 * k): for j in range(m): s2.add(A[i][j]) for i in range(2 * k, 3 * k): for j in range(m): s3.add(A[i][j]) if len(s1) == len(s2) == len(s3) == 1 and len(s1 | s2 | s3) == 3: print("YES") exit(0) B = [(["0"] * n) for _ in range(m)] for i in range(n): for j in range(m): B[j][i] = A[i][j] A = B n, m = m, n if n % 3 == 0: k = n // 3 s1 = set() s2 = set() s3 = set() for i in range(k): for j in range(m): s1.add(A[i][j]) for i in range(k, 2 * k): for j in range(m): s2.add(A[i][j]) for i in range(2 * k, 3 * k): for j in range(m): s3.add(A[i][j]) if len(s1) == len(s2) == len(s3) == 1 and len(s1 | s2 | s3) == 3: print("YES") exit(0) print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST STRING 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 ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = [int(el) for el in input().split()] fl = [input().split() for i in range(n)] fl1 = ( [["R" * m] for i in range(n // 3)] + [["G" * m] for i in range(n // 3)] + [["B" * m] for i in range(n // 3)] ) fl2 = ( [["R" * m] for i in range(n // 3)] + [["B" * m] for i in range(n // 3)] + [["G" * m] for i in range(n // 3)] ) fl3 = ( [["B" * m] for i in range(n // 3)] + [["G" * m] for i in range(n // 3)] + [["R" * m] for i in range(n // 3)] ) fl4 = ( [["B" * m] for i in range(n // 3)] + [["R" * m] for i in range(n // 3)] + [["G" * m] for i in range(n // 3)] ) fl5 = ( [["G" * m] for i in range(n // 3)] + [["R" * m] for i in range(n // 3)] + [["B" * m] for i in range(n // 3)] ) fl6 = ( [["G" * m] for i in range(n // 3)] + [["B" * m] for i in range(n // 3)] + [["R" * m] for i in range(n // 3)] ) fl7 = [["R" * (m // 3) + "G" * (m // 3) + "B" * (m // 3)] for i in range(n)] fl8 = [["R" * (m // 3) + "B" * (m // 3) + "G" * (m // 3)] for i in range(n)] fl9 = [["G" * (m // 3) + "B" * (m // 3) + "R" * (m // 3)] for i in range(n)] fl10 = [["G" * (m // 3) + "R" * (m // 3) + "B" * (m // 3)] for i in range(n)] fl11 = [["B" * (m // 3) + "G" * (m // 3) + "R" * (m // 3)] for i in range(n)] fl12 = [["B" * (m // 3) + "R" * (m // 3) + "G" * (m // 3)] for i in range(n)] if ( fl == fl1 or fl == fl2 or fl == fl3 or fl == fl4 or fl == fl5 or fl == fl6 or fl == fl7 or fl == fl8 or fl == fl9 or fl == fl10 or fl == fl11 or fl == fl12 ): print("YES") else: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
def f(n, m, a): d = dict() if n % 3 == 0: k = n // 3 else: return False c = a[0][0] d = {} d[1] = c for i in range(k): for j in range(m): if c == a[i][j]: pass else: return False c = a[k][0] d[2] = c for i in range(k): for j in range(m): if c == a[k + i][j]: pass else: return False c = a[2 * k][0] d[3] = c for i in range(k): for j in range(m): if c == a[2 * k + i][j]: pass else: return False if d[1] != d[2] and d[2] != d[3] and d[3] != d[1]: return True else: return False n, m = list(map(int, input().strip().split())) h, v = [], [] for i in range(n): s = str(input()) r = [] for el in s: r.append(el) h.append(r) for i in range(m): r = [] for j in range(n): r.append(h[j][i]) v.append(r) if f(n, m, h) or f(m, n, v): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
b, k = map(int, input().split()) arr = [] for i in range(b): s = input() arr.append(s) x = arr[0][0] nb = 1 nk = 1 pola = str(x) while nk < k and arr[0][nk] == x: nk += 1 pola += x if nk == k or nk == k / 3: if nk == k / 3: arr = list(map(lambda x: "".join(x), map(list, zip(*arr)))) b, k = k, b nb = 1 nk = 1 pola = str(x) while nk < k and arr[0][nk] == x: nk += 1 pola += x while nb < b and arr[nb][:nk] == pola: nb += 1 if nb == b / 3: y = arr[nb][0] polay = "" for i in range(nk): polay += y nby = 0 brs = nb while nby + nb < b and arr[nby + nb] == polay: nby += 1 if nby == nb: z = arr[nby + nb][0] if z != x: polaz = "" for i in range(nk): polaz += z nbz = 0 brs = nby + nb while nbz + nby + nb < b and arr[nbz + nby + nb] == polaz: nbz += 1 if nbz == nb: print("YES") else: print("NO") else: print("NO") else: print("NO") else: print("NO") else: print("NO")
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 EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = list(map(int, input().split())) flag = [] def letterwidth(i): res = flag[i][0] for item in flag[i]: if item != res: return None return res def letterheight(i): res = flag[0][i] for j in range(n): if flag[j][i] != res: return None return res for i in range(n): flag.append(input()) result = False if n % 3 == 0 and not result: w = n // 3 letters = [] for i in range(n): curres = letterwidth(i) letters.append(curres) if curres is None: break if letters.count(None) == 0: answers = [] counter = 0 for i in range(3): res = letters[counter] answers.append(res) counter += 1 for j in range(w - 1): if letters[counter] != res: letters.append(None) break counter += 1 if letters.count(None) > 0: break if letters.count(None) == 0: if len(answers) == len(set(answers)): result = True if m % 3 == 0 and not result: w = m // 3 letters = [] for i in range(m): curres = letterheight(i) letters.append(curres) if curres is None: break if letters.count(None) == 0: answers = [] counter = 0 for i in range(3): res = letters[counter] answers.append(res) counter += 1 for j in range(w - 1): if letters[counter] != res: letters.append(None) break counter += 1 if letters.count(None) > 0: break if letters.count(None) == 0: if len(answers) == len(set(answers)): result = True if result: print("YES") else: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR RETURN NONE RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NONE RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE IF FUNC_CALL VAR NONE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NONE VAR NUMBER IF FUNC_CALL VAR NONE NUMBER IF FUNC_CALL VAR NONE NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE IF FUNC_CALL VAR NONE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NONE VAR NUMBER IF FUNC_CALL VAR NONE NUMBER IF FUNC_CALL VAR NONE NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
def check(a): cur = a[0][0] d = {} for p in a: if p[0] != cur and p[0] in d: return 0 cur = p[0] if not all([(x == cur) for x in p]): return 0 d[cur] = d.get(cur, 0) + 1 if len(d) != 3: return 0 if not "R" in d or not "G" in d or not "B" in d: return 0 cnt = d["R"] if d["G"] != cnt or d["B"] != cnt: return 0 return 1 n, m = map(int, input().split()) a = [list(input()) for i in range(n)] if check(a): print("YES") else: a = list(map(list, zip(*a))) if check(a): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF STRING VAR STRING VAR STRING VAR RETURN NUMBER ASSIGN VAR VAR STRING IF VAR STRING VAR VAR STRING 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 VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = list(map(int, input().split())) ls = [] for i in range(n): ls.append(input()) if ls[0] != "R" * m and ls[0] != "G" * m and ls[0] != "B" * m: ls1 = [] for i in range(m): row = "" for j in range(n): row += ls[j][i] ls1.append(row) ls = ls1 m = n n = len(ls1) if n % 3 != 0: print("NO") else: first = ls[0][0] i = 0 while i < n and ls[i] == first * m: i += 1 if i < n and i == n // 3: second = ls[i][0] while i < n and ls[i] == second * m: i += 1 if i < n and i == 2 * (n // 3): third = ls[i][0] while i < n and ls[i] == third * m: i += 1 if i == n and third != first: print("YES") else: print("NO") else: print("NO") else: 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 IF VAR NUMBER BIN_OP STRING VAR VAR NUMBER BIN_OP STRING VAR VAR NUMBER BIN_OP STRING VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
def solve(h, w, f): if h % 3: return False a = f[0][0] b = f[h // 3][0] c = f[2 * h // 3][0] if a == b or b == c or c == a: return False for y in range(h // 3): if f[y].count(a) != w: return False for y in range(h // 3, 2 * h // 3): if f[y].count(b) != w: return False for y in range(2 * h // 3, h): if f[y].count(c) != w: return False return True def main(): h, w = map(int, input().split()) f = [input() for _ in range(h)] g = ["".join([f[y][x] for y in range(h)]) for x in range(w)] ans = solve(h, w, f) or solve(w, h, g) print(ans and "YES" or "NO") main()
FUNC_DEF IF BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = list(map(int, input().split(" "))) ls, col = [], [] for x in range(n): ls.append(input()) for i in range(m): elem = "" for x in ls: elem = "".join([elem, x[i]]) col.append(elem) def ans(): if n % 3 != 0 and m % 3 != 0: return "NO" for x in ls: if any(y not in ["R", "G", "B"] for y in x): return "NO" if ( n % 3 == 0 and all(x == ls[0] for x in ls[0 : n // 3]) and all(x == ls[n // 3] for x in ls[n // 3 : 2 * n // 3]) and all(x == ls[2 * n // 3] for x in ls[2 * n // 3 : n]) ): if ls[0] != ls[n // 3] and ls[n // 3] != ls[2 * n // 3]: for z in ["R", "G", "B"]: tmp = [ bool(z in ls[0]), bool(z in ls[n // 3]), bool(z in ls[2 * n // 3]), ] if tmp.count(True) > 1: return "NO" return "YES" if ( m % 3 == 0 and all(x == col[0] for x in col[0 : m // 3]) and all(x == col[m // 3] for x in col[m // 3 : 2 * m // 3]) and all(x == col[2 * m // 3] for x in col[2 * m // 3 : m]) ): if col[0] != col[m // 3] and col[m // 3] != col[2 * m // 3]: for z in ["R", "G", "B"]: tmp = [ bool(z in col[0]), bool(z in col[m // 3]), bool(z in col[2 * m // 3]), ] if tmp.count(True) > 1: return "NO" return "YES" return "NO" print(ans())
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL STRING LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN STRING FOR VAR VAR IF FUNC_CALL VAR VAR LIST STRING STRING STRING VAR VAR RETURN STRING IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR LIST STRING STRING STRING ASSIGN VAR LIST FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER RETURN STRING RETURN STRING IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR LIST STRING STRING STRING ASSIGN VAR LIST FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER RETURN STRING RETURN STRING RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
def re(a): if a == "R": return 0 elif a == "B": return 1 else: return 2 def llk(a): dd = "".join(a) i = 0 n = len(dd) su = 0 while i < n - 1: if dd[i] != dd[i + 1]: su += 1 i += 1 if su == 2: return 1 else: return 0 a = [int(i) for i in input().split()] k = [] lk = [] for i in range(a[0]): aa = input() k.append(aa) lk.append(set(aa)) ml = 0 ch = [0, 0, 0] for i in k: if len(set(i)) == 1: ch[re(i[0])] += 1 else: ml = 1 break mll = 0 gk = [""] * a[1] for i in range(a[0]): dk = k[i] for j in range(a[1]): gk[j] += dk[j] ch1 = [0, 0, 0] for i in gk: if len(set(i)) == 1: ch1[re(i[0])] += 1 else: mll = 1 break if len(set(ch)) == 1 and ml == 0 and llk(k): print("YES") elif len(set(ch1)) == 1 and mll == 0 and llk(gk): print("YES") else: print("NO")
FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
def checkline(i): mark = d[i][0] for j in range(m): if d[i][j] != mark: return 0 return mark def checkrow(i): mark = d[0][i] for j in range(n): if d[j][i] != mark: return 0 return mark n, m = map(int, input().split()) if n % 3 != 0 and m % 3 != 0: print("NO") else: k = False d = [] f = dict(zip(["R", "G", "B"], [0, 0, 0])) for i in range(n): h = [] for x in input(): h.append(x) f[x] += 1 d.append(h) if f["R"] == f["G"] and f["G"] == f["B"] and f["B"] == f["R"]: if n % 3 == 0: a = [] kk = True for i in range(n): a.append(checkline(i)) if ( (a[i] != a[i - 1] and i != 0) and not (i == n // 3 or i == 2 * n // 3) or a[i] == 0 ): kk = False if kk: k = True a = [] if m % 3 == 0 and not k: kkk = True for i in range(m): a.append(checkrow(i)) if ( (a[i] != a[i - 1] and i != 0) and not (i == m // 3 or i == 2 * m // 3) or a[i] == 0 ): kkk = False break if kkk: k = True if k: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST STRING STRING STRING LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
class CodeforcesTask837BSolution: def __init__(self): self.result = "" self.n_m = [] self.flag = [] def read_input(self): self.n_m = [int(x) for x in input().split(" ")] for x in range(self.n_m[0]): self.flag.append(input()) def process_task(self): if self.n_m[0] % 3 and self.n_m[1] % 3: self.result = "NO" else: variants = [] if not self.n_m[0] % 3: c1 = self.flag[0][0] c2 = self.flag[self.n_m[0] // 3][0] c3 = self.flag[self.n_m[0] // 3 * 2][0] if c1 != c2 and c1 != c3 and c3 != c2: l1 = [] l2 = [] l3 = [] for x in range(self.n_m[0] // 3): l1.append(c1 * self.n_m[1]) l2.append(c2 * self.n_m[1]) l3.append(c3 * self.n_m[1]) v = l1 + l2 + l3 variants.append(v) if not self.n_m[1] % 3: c1 = self.flag[0][0] c2 = self.flag[0][self.n_m[1] // 3] c3 = self.flag[0][self.n_m[1] // 3 * 2] if c1 != c2 and c1 != c3 and c3 != c2: x = self.n_m[1] // 3 l = c1 * x + c2 * x + c3 * x v = [l for z in range(self.n_m[0])] variants.append(v) canbe = False for v in variants: c = True for x in range(self.n_m[0]): if self.flag[x] != v[x]: c = False break if c: canbe = True break self.result = "YES" if canbe else "NO" def get_result(self): return self.result Solution = CodeforcesTask837BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR STRING STRING FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = map(int, input().split()) arr = [0] * n for i in range(n): arr[i] = input() was = [] dist = -1 m_dist = 0 chk1 = 1 chk2 = 1 prev_ans = arr[0][0] was.append(arr[0][0]) allowed = ["R", "B", "G"] if not arr[0][0] in allowed: chk1 = 0 chk2 = 0 for i in range(n): dist += 1 for j in range(m): if arr[i][j] != prev_ans: if ( arr[i][j] in was or m_dist != 0 and m_dist != dist or not arr[i][j] in allowed ): chk1 = 0 else: was.append(arr[i][j]) prev_ans = arr[i][j] m_dist = dist dist = 0 prev_ans = arr[i][j] dist += 1 if len(was) != 3 or m_dist != dist: chk1 = 0 was = [] dist = -1 m_dist = 0 chk2 = 1 prev_ans = arr[0][0] was.append(arr[0][0]) allowed = ["R", "B", "G"] for j in range(m): dist += 1 for i in range(n): if arr[i][j] != prev_ans: if ( arr[i][j] in was or m_dist != 0 and m_dist != dist or not arr[i][j] in allowed ): chk2 = 0 else: was.append(arr[i][j]) prev_ans = arr[i][j] m_dist = dist dist = 0 prev_ans = arr[i][j] dist += 1 if len(was) != 3 or m_dist != dist: chk2 = 0 if chk1 or chk2: print("YES") else: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING IF VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING FOR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
[n, m] = [int(i) for i in input().split()] flag = [] flip_flag = [] for i in range(m): flip_flag.append([]) tmp = n while tmp: tmp -= 1 flag.append(list(input())) for i in range(m): flip_flag[i].append(flag[-1][i]) if len(set(flag[0])) != 1: flag = flip_flag strip_count = {} valid = True strip_count[flag[0][0]] = 1 if len(set(flag[0])) != 1: valid = False for i in range(1, len(flag)): if len(set(flag[i])) != 1 or valid == False: valid = False break if flag[i][0] not in strip_count.keys(): strip_count[flag[i][0]] = 1 elif flag[i][0] != flag[i - 1][0]: valid = False break else: strip_count[flag[i][0]] += 1 if len(set(strip_count.values())) != 1: valid = False if not ( all(i in strip_count.keys() for i in ["R", "G", "B"]) and len(set(strip_count.keys())) == 3 ): valid = False print("YES" if valid else "NO")
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST STRING STRING STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
import sys n, m = map(int, input().split()) s = [] for i in range(n): string = input() s.append(string) no = 0 r_mix = 0 for i in range(n): cnt_r = s[i].count("R") cnt_b = s[i].count("B") cnt_g = s[i].count("G") if cnt_r and cnt_b or cnt_b and cnt_g or cnt_g and cnt_r: r_mix = 1 c_mix = 0 for j in range(m): cnt_r = cnt_b = cnt_g = 0 for i in range(n): if s[i][j] == "R": cnt_r += 1 elif s[i][j] == "B": cnt_b += 1 elif s[i][j] == "G": cnt_g += 1 if cnt_r and cnt_b or cnt_b and cnt_g or cnt_g and cnt_r: c_mix = 1 if r_mix == 1 and c_mix == 1 or r_mix == 0 and c_mix == 0: no = 1 if r_mix == 1: cnt_b = cnt_g = cnt_r = 0 if s[0][0] == "R": cnt_r += 1 elif s[0][0] == "G": cnt_g += 1 elif s[0][0] == "B": cnt_b += 1 f, c = -1, 1 for j in range(0, m - 1): if s[0][j] == s[0][j + 1]: c += 1 else: if s[0][j + 1] == "R": cnt_r += 1 elif s[0][j + 1] == "G": cnt_g += 1 elif s[0][j + 1] == "B": cnt_b += 1 if f == -1: f = c if f != c: no = 1 break c = 1 if f != c or (cnt_r != 1 or cnt_b != 1 or cnt_g != 1): no = 1 if c_mix: f = -1 c = 1 cnt_b = 0 cnt_g = 0 cnt_r = 0 if s[0][0] == "R": cnt_r += 1 elif s[0][0] == "G": cnt_g += 1 elif s[0][0] == "B": cnt_b += 1 for i in range(0, n - 1): if s[i][0] == s[i + 1][0]: c += 1 else: if s[i + 1][0] == "R": cnt_r += 1 elif s[i + 1][0] == "G": cnt_g += 1 elif s[i + 1][0] == "B": cnt_b += 1 if f == -1: f = c if f != c: no = 1 break c = 1 if f != c or (cnt_r != 1 or cnt_b != 1 or cnt_g != 1): no = 1 if no == 1: print("NO") else: print("YES")
IMPORT 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 EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = map(int, input().split()) t = [] for i in range(n): s = input() t.append([]) for j in s: t[-1].append(j) c = -1 col = {"R": 0, "G": 0, "B": 0} for i in range(n): for j in range(m): if type(t[i][j]) == str: v = [[i, j]] s = t[i][j] col[s] += 1 c += 1 t[i][j] = c while v != []: a, b = v[0][0], v[0][1] v.pop(0) if a > 0 and t[a - 1][b] == s: t[a - 1][b] = c v.append([a - 1, b]) if b > 0 and t[a][b - 1] == s: t[a][b - 1] = c v.append([a, b - 1]) if a < n - 1 and t[a + 1][b] == s: t[a + 1][b] = c v.append([a + 1, b]) if b < m - 1 and t[a][b + 1] == s: t[a][b + 1] = c v.append([a, b + 1]) if c != 2 or not col["R"] == col["G"] == col["B"]: print("NO") else: f = False col = [0, 0, 0] for i in range(n): c = t[i][0] col[c] += 1 for j in range(m): if t[i][j] != c: f = True break if f: f = False col = [0, 0, 0] for i in range(m): c = t[0][i] col[c] += 1 for j in range(n): if t[j][i] != c: f = True break if f: print("NO") elif col[0] == col[1] == col[2]: print("YES") else: print("NO") elif col[0] == col[1] == col[2]: print("YES") else: print("NO")
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 EXPR FUNC_CALL VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST LIST VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR LIST ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
N, M = map(int, input().split()) flag = [input() for n in range(N)] def get_rect_of_color(color): rect_points = [] for i, row in enumerate(flag): first = True seq_is_broken = False tmp = [None] * 4 for j, char in enumerate(row): if char == color: if first: first = False tmp[0], tmp[1] = i, j if seq_is_broken: raise Exception() tmp[2], tmp[3] = i, j elif not first: seq_is_broken = True if tmp[0] is not None: rect_points += [tmp] return rect_points def get_bounding_rect(rect): last_pos = rect[0] first = True for row in rect: if first: first = False continue ly1, lx1, ly2, lx2 = last_pos y1, x1, y2, x2 = row if y1 - ly1 != 1 or y2 - ly2 != 1 or x1 - lx1 != 0 or x2 - lx2 != 0: raise Exception() last_pos = row return (rect[0][0], rect[0][1]), (rect[-1][2], rect[-1][3]) try: R = get_bounding_rect(get_rect_of_color("R")) G = get_bounding_rect(get_rect_of_color("G")) B = get_bounding_rect(get_rect_of_color("B")) Rh = abs(R[1][0] - R[0][0]) Bh = abs(B[1][0] - B[0][0]) Gh = abs(G[1][0] - G[0][0]) Rw = abs(R[1][1] - R[0][1]) Bw = abs(B[1][1] - B[0][1]) Gw = abs(G[1][1] - G[0][1]) if len(set([Rw, Bw, Gw])) != 1 or len(set([Rh, Bh, Gh])) != 1: print("NO") else: print("YES") except Exception: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR NUMBER NONE VAR LIST VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
import sys def verifyFlag(flagList, row, column): if row % 3 != 0: return False q = row // 3 u = [flagList[0 * q], flagList[1 * q], flagList[2 * q]] us = set(u) patternToMatch = set() for color in ["R", "G", "B"]: patternToMatch.add(color * column) if us != patternToMatch: return False for i, row in enumerate(flagList): if row != u[i // q]: return False return True def main(): n, m = map(int, sys.stdin.readline().split()) a = [] for i in range(n): a.append(sys.stdin.readline().rstrip()) b = [] for i in range(m): temp = "" for j in range(n): temp += a[j][i] b.append(temp) rv1 = verifyFlag(a, n, m) rv2 = verifyFlag(b, m, n) if rv1 or rv2: print("YES") else: print("NO") main()
IMPORT FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR LIST STRING STRING STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER 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 FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
def solve_h(lines): if len(lines) == 0: return False if len(lines) % 3 != 0: return False strip = len(lines) // 3 colors = [] for i, line in enumerate(lines): if i % strip == 0: colors.append(line[0]) if not all(ch == colors[-1] for ch in line): return False if set(colors) != set("RGB"): return False return True def transpose(lines): return list("".join(chs) for chs in zip(*lines)) def solve_v(lines): return solve_h(transpose(lines)) def solve(lines): return solve_h(lines) or solve_v(lines) h, w = map(int, input().split()) lines = [input() for _ in range(h)] print("YES" if solve(lines) else "NO")
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING
The flag of Berland is such rectangular field n Γ— m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n Γ— m, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). -----Input----- The first line contains two integer numbers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' β€” the description of the field. -----Output----- Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). -----Examples----- Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Output YES Input 4 3 BRG BRG BRG BRG Output YES Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Output NO Input 4 4 RRRR RRRR BBBB GGGG Output NO -----Note----- The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights β€” 2, 1 and 1.
n, m = map(int, input().split()) l = [] for i in range(n): l.append([]) s = input() for j in s: l[i].append(j) A = "YES" a = True b = True if n % 3 != 0 and m % 3 != 0: A = "NO" if n % 3 == 0: s1 = l[0][0] for i in range(n // 3): for j in range(m): if l[i][j] != s1: a = False s2 = l[n // 3][0] for i in range(n // 3, n // 3 * 2): for j in range(m): if l[i][j] != s2: a = False s3 = l[n // 3 * 2][0] for i in range(n // 3 * 2, n): for j in range(m): if l[i][j] != s3: a = False if s1 == s2 or s2 == s3 or s1 == s3: a = False else: a = False if m % 3 == 0: s1 = l[0][0] for i in range(n): for j in range(m // 3): if l[i][j] != s1: b = False s2 = l[0][m // 3] for i in range(n): for j in range(m // 3, m // 3 * 2): if l[i][j] != s2: b = False s3 = l[0][m // 3 * 2] for i in range(n): for j in range(m // 3 * 2, m): if l[i][j] != s3: b = False if s1 == s2 or s2 == s3 or s1 == s3: b = False else: b = False if not a and not b: A = "NO" print(A)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
import sys input = sys.stdin.readline t = int(input()) for you in range(t): n = int(input()) l = input().split() li = [(int(i) % 2) for i in l] ind = -1 z = 0 ans = 10**12 for i in range(n): if li[i]: ind = i break if ind == -1: print(-1) continue c1 = 0 for i in range(n): if li[i] != i % 2: c1 += 1 c2 = 0 for i in range(n): if li[i] != (i + 1) % 2: c2 += 1 if c1 < c2: print(c1) for i in range(n): if li[i] != i % 2: if i == ind: continue print(i + 1, ind + 1) if li[ind] != ind % 2: for i in range(n): if i != ind and i % 2: print(ind + 1, i + 1) break else: print(c2) for i in range(n): if li[i] != (i + 1) % 2: if i == ind: continue print(i + 1, ind + 1) if li[ind] != (ind + 1) % 2: for i in range(n): if i != ind and (i + 1) % 2: print(ind + 1, i + 1) break
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for _ in range(int(input())): n = int(input()) a = list(i % 2 for i in map(int, input().split())) if a.count(1) == 0: print(-1) else: l0, l1 = -1, -1 for i in range(n): if a[i] % 2: if i % 2: l1 = i else: l0 = i Answ121 = [] if l0 != -1: for i in range(n): if i % 2 == a[i] % 2: Answ121.append([i + 1, l0 + 1]) Answ122 = [] if l1 != -1: for i in range(n): if i % 2 != a[i] % 2: Answ122.append([i + 1, l1 + 1]) if l0 == -1: print(len(Answ122)) for i in Answ122: print(*i) elif l1 == -1: print(len(Answ121)) for i in Answ121: print(*i) else: print(min(len(Answ121), len(Answ122))) if len(Answ121) < len(Answ122): for i in Answ121: print(*i) else: for i in Answ122: print(*i)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
T = int(input()) for _ in range(T): N = int(input()) A = input().split(" ") oePatternXor = [] eoPatternXor = [] eoPivot = -1 oePivot = -1 for i, n in enumerate(A): V = int(n) % 2 if i % 2 == 0 and V == 0 or i % 2 == 1 and V == 1: eoPatternXor.append(i + 1) if oePivot == -1 and V == 1: oePivot = i + 1 elif i % 2 == 1 and V == 0 or i % 2 == 0 and V == 1: oePatternXor.append(i + 1) if eoPivot == -1 and V == 1: eoPivot = i + 1 if eoPivot == -1 and oePivot == -1: print(-1) else: if eoPivot != -1 and oePivot != -1: if oePivot != -1 and len(oePatternXor) <= len(eoPatternXor): res, pivot = oePatternXor, oePivot else: res, pivot = eoPatternXor, eoPivot elif eoPivot != -1: res, pivot = eoPatternXor, eoPivot elif oePivot != -1: res, pivot = oePatternXor, oePivot print(len(res)) for v in res: print(v, pivot)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
t = int(input()) while t: t -= 1 n = int(input()) arr = list(map(int, input().split())) arr = [(x % 2) for x in arr] if len(set(arr)) == 1 and arr[0] == 0: print(-1) continue ans = [] s1 = [(0) for i in range(n)] s2 = [(1) for i in range(n)] cnt1, cnt2 = 0, 0 for i in range(n): if i & 1: s1[i] = 1 s2[i] = 0 if s1[i] != arr[i]: cnt1 += 1 if s2[i] != arr[i]: cnt2 += 1 mask = [] if cnt1 <= cnt2: mask = s1[:] else: mask = s2[:] ind = arr.index(1) ind2 = mask.index(1) for i in range(n): if arr[i] == 0 and mask[i] == 1: ans.append((i + 1, ind + 1)) for i in range(n): if arr[i] == 1 and mask[i] == 0: ans.append((i + 1, ind2 + 1)) print(min(cnt1, cnt2)) for i in range(len(ans)): print(*ans[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for _ in range(int(input())): n = int(input()) lst = list(map(int, input().strip().split())) arr_odd = [] arr_even = [] even = 0 for i in range(len(lst)): lst[i] = lst[i] % 2 if lst[i] == 0: even += 1 arr_even.append(i % 2) arr_odd.append((i + 1) % 2) if even == len(lst): print(-1) continue chose_arr = [] count_even = 0 count_odd = 0 for i in range(len(lst)): if lst[i] == arr_even[i]: count_even += 1 if lst[i] == arr_odd[i]: count_odd += 1 if count_even >= count_odd: chose_arr = arr_even else: chose_arr = arr_odd result = [] j = len(lst) - 1 while j >= 0: if lst[j] == 1: break j -= 1 odd_index = 0 res = 0 for i in range(2): if lst[i] != chose_arr[i]: res += 1 lst[i] = chose_arr[i] result.append([i + 1, j + 1]) for i in range(len(lst)): if lst[i] % 2 != 0: odd_index = i break for i in range(2, len(lst)): if lst[i] != chose_arr[i]: res += 1 result.append([i + 1, odd_index + 1]) print(res) for i in range(res): print(result[i][0], result[i][1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
try: t = int(float(input())) for _ in range(t): n = int(float(input())) a = list(map(int, input().split())) b = [(i % 2) for i in a] if not any(b): print(-1) else: oe, eo = 0, 0 o1, o2 = 0, 0 for i in range(n): if i % 2 == 0 and b[i] == 1: eo += 1 elif i % 2 == 1 and b[i] == 0: eo += 1 if i % 2 == 1 and b[i] == 1: o1 = i for i in range(n): if i % 2 == 0 and b[i] == 0: oe += 1 elif i % 2 == 1 and b[i] == 1: oe += 1 if i % 2 == 0 and b[i] == 1: o2 = i if eo <= oe: print(eo) for i in range(n): if i % 2 == 0 and b[i] == 1: print(i + 1, o1 + 1) elif i % 2 == 1 and b[i] == 0: print(i + 1, o1 + 1) else: print(oe) for i in range(n): if i % 2 == 0 and b[i] == 0: print(i + 1, o2 + 1) elif i % 2 == 1 and b[i] == 1: print(i + 1, o2 + 1) except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) eo = 0 oe = 0 ans = 0 for j in range(n): if a[j] % 2 == 0: ans += 1 if ans == n: print(-1) else: for i in range(n): if i % 2 != 0 and a[i] % 2 != 0: b = i if i % 2 == 0 and a[i] % 2 != 0: c = i if i % 2 == 0 and a[i] % 2 == 0 or i % 2 != 0 and a[i] % 2 != 0: eo += 1 elif i % 2 == 0 and a[i] % 2 != 0 or i % 2 != 0 and a[i] % 2 == 0: oe += 1 if oe <= eo: print(n - eo) for x in range(n): if x % 2 == 0: if a[x] % 2 != 0: print(x + 1, b + 1) elif a[x] % 2 == 0: print(x + 1, b + 1) else: print(n - oe) for y in range(n): if y % 2 == 0: if a[y] % 2 == 0: print(y + 1, c + 1) elif a[y] % 2 != 0: print(y + 1, c + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) valid, odd, even = False, False, False odd_pos, even_pos = -1, -1 for i in range(n): if a[i] % 2: valid = True if i % 2: odd = True odd_pos = i else: even = True even_pos = i if valid == False: print(-1) continue r_odd = [] r_even = [] ops_odd = 0 ops_even = 0 if odd: for i in range(n): if i % 2: if a[i] % 2 == 0: ops_odd += 1 r_odd.append([i + 1, odd_pos + 1]) elif a[i] % 2 != 0: ops_odd += 1 r_odd.append([i + 1, odd_pos + 1]) if even: for i in range(n): if i % 2: if a[i] % 2 != 0: ops_even += 1 r_even.append([i + 1, even_pos + 1]) elif a[i] % 2 == 0: ops_even += 1 r_even.append([i + 1, even_pos + 1]) if odd and even: if ops_even <= ops_odd: print(ops_even) for i in r_even: print(i[0], i[1]) else: print(ops_odd) for i in r_odd: print(i[0], i[1]) elif odd: print(ops_odd) for i in r_odd: print(i[0], i[1]) else: print(ops_even) for i in r_even: print(i[0], i[1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
def printi(ans): print(len(ans)) for i in ans: print(i[0] + 1, i[1] + 1) for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) l = [0] * len(arr) odd = [] flag = 0 for i in range(len(arr)): l[i] = arr[i] % 2 if arr[i] % 2 == 1: odd.append(i) if odd == []: print(-1) continue ans, ans1 = [], [] if l[0] % 2 != 1: l[0] = 1 ans.append([0, odd[-1]]) for i in range(1, len(l)): if i % 2 == 0 and l[i] == 0: ans.append([i, 0]) elif i % 2 == 1 and l[i] == 1: ans.append([i, 0]) l[0] = arr[0] % 2 if l[1] % 2 != 1: l[1] = 1 ans1.append([1, odd[-1]]) for i in range(len(l)): if i != 1: if i % 2 == 0 and l[i] == 1: ans1.append([i, 1]) elif i % 2 == 1 and l[i] == 0: ans1.append([i, 1]) if len(ans) < len(ans1): printi(ans) else: printi(ans1)
FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST LIST IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for _ in range(int(input())): n = int(input()) a = [(int(x) % 2) for x in input().split()] target1 = [(x % 2) for x in range(n)] target2 = [((x + 1) % 2) for x in range(n)] chk1 = chk2 = 0 changeind = -1 for i in range(n): if a[i] != target1[i]: chk1 += 1 elif a[i] != target2[i]: chk2 += 1 else: pass if chk1 < chk2: target = target1 chk = chk1 else: target = target2 chk = chk2 for i in range(n): if a[i] == 1 and target[i] == 1: changeind = i break if changeind == -1: print(-1) else: resinds = [] for i in range(n): if a[i] != target[i] and i != changeind: resinds.append(i) print(chk) for i in range(len(resinds)): print(resinds[i] + 1, changeind + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
from sys import stdin, stdout inp_num = lambda: int(input()) inp_lis = lambda: list(map(int, input().split())) for _ in range(inp_num()): n = inp_num() l1 = inp_lis() l = [] re = [] re1 = [] a = 0 cou1 = -1 for i in l1: if i % 2 == 0: l.append(0) else: l.append(1) cou1 = a if a % 2 == 0: re.append(1) re1.append(0) else: re.append(0) re1.append(1) a += 1 if cou1 == -1: print(-1) continue an1 = 0 an2 = 0 for i in range(n): if l[i] == re[i]: an2 += 1 else: an1 += 1 print(min(an1, an2)) if an2 >= an1: for i in range(n): if l[i] == re[i]: continue elif i == 0 and l[0] == 0: print(i + 1, cou1 + 1, sep=" ") elif i == 1 and l[i] == 1: print(2, 1, sep=" ") elif l[i] != re[i]: if l[i] == 1: print(i + 1, 1, sep=" ") else: print(i + 1, 1, sep=" ") else: for i in range(n): if l[i] == re1[i]: continue elif i == 0 and l[i] == 1 and l[1] == 1: print(1, 2, sep=" ") elif i == 0 and l[i] == 1 and l[1] == 0: print(2, 1, sep=" ") l[1] = 1 print(1, 2, sep=" ") elif i == 1: print(2, cou1 + 1, sep=" ") elif l[i] != re1[i]: if l[i] == 1: print(i + 1, 2, sep=" ") else: print(i + 1, 2, sep=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER STRING IF VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER STRING IF VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
t = int(input()) for _ in range(t): n = int(input()) lst = list(map(int, input().split())) a = [] ce = 0 co = 0 pe = 0 po = 0 for i in range(n): if lst[i] % 2 == 0: a.append(0) ce = ce + 1 else: a.append(1) co = co + 1 if co == 0: print(-1) elif co == n: print(n // 2) j = 1 while j < n: print(j + 1, 1) j = j + 2 else: ans = [] ans1 = [] po1 = 0 po2 = 0 pe1 = 0 pe2 = 0 i = 0 flag = 1 while flag == 1 and i < n: if i % 2 == 0 and a[i] == 1: po1 = i flag = 0 i = i + 1 i = 0 flag = 1 while flag == 1 and i < n: if i % 2 == 1 and a[i] == 0: pe1 = i flag = 0 i = i + 1 i = 0 flag = 1 while flag == 1 and i < n: if i % 2 == 0 and a[i] == 0: pe2 = i flag = 0 i = i + 1 i = 0 flag = 1 while flag == 1 and i < n: if i % 2 == 1 and a[i] == 1: po2 = i flag = 0 i = i + 1 for i in range(len(lst)): if i % 2 == 0 and a[i] == 0: ans.append(i + 1) ans.append(po1 + 1) if i % 2 != 0 and a[i] == 1: ans.append(i + 1) ans.append(po1 + 1) for i in range(len(lst)): if i % 2 == 0 and a[i] == 1: ans1.append(i + 1) ans1.append(po2 + 1) if i % 2 != 0 and a[i] == 0: ans1.append(i + 1) ans1.append(po2 + 1) if min(len(ans1), len(ans)) == len(ans): print(len(ans) // 2) j = 0 while j < len(ans): print(ans[j], ans[j + 1]) j = j + 2 else: print(len(ans1) // 2) j = 0 while j < len(ans1): print(ans1[j], ans1[j + 1]) j = j + 2
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) cnt = 0 g = -1 for x in range(len(l)): if x % 2 == 0 and l[x] % 2 == 0: cnt = cnt + 1 elif x % 2 == 1 and l[x] % 2 == 1: cnt = cnt + 1 if l[x] % 2 == 1: g = x if g == -1: print("-1") elif cnt <= int(len(l) / 2): print(cnt) if l[0] % 2 == 0: l[0] = l[0] ^ l[g] print("1", g + 1) g = 0 else: g = 0 for x in range(len(l)): if x % 2 == 0 and l[x] % 2 == 0: print(x + 1, g + 1) elif x % 2 == 1 and l[x] % 2 == 1: print(x + 1, g + 1) else: print(len(l) - cnt) if l[1] % 2 == 0: l[1] = l[1] ^ l[g] print("2", g + 1) g = 1 else: g = 1 for x in range(len(l)): if x % 2 == 1 and l[x] % 2 == 0: print(x + 1, g + 1) elif x % 2 == 0 and l[x] % 2 == 1: print(x + 1, g + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
T = int(input()) for _ in range(T): n = int(input()) A = list(map(int, input().split())) flag = False for i in range(n): if A[i] % 2 == 1: A[i] = 1 else: A[i] = 0 first = [] second = [] no_alt_id1 = [] no_alt_id2 = [] for i in range(n): if i % 2 == 0 and A[i] == 0: pass elif i % 2 == 1 and A[i] == 1: no_alt_id1.append(i) else: first.append(i) for i in range(n): if i % 2 == 1 and A[i] == 0: pass elif i % 2 == 0 and A[i] == 1: no_alt_id2.append(i) else: second.append(i) ans = [] if len(first) < len(second): for i in range(len(first)): if len(no_alt_id1) == 0: flag = True print(-1) break else: ans.append(first[i] + 1) ans.append(no_alt_id1[0] + 1) else: for i in range(len(second)): if len(no_alt_id2) == 0: flag = True print(-1) break else: ans.append(second[i] + 1) ans.append(no_alt_id2[0] + 1) if flag != True: print(len(ans) // 2) for i in range(0, len(ans), 2): print(ans[i], end=" ") print(ans[i + 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
t = int(input()) while t: t = t - 1 n = int(input()) a = list(map(int, input().split())) count0 = 0 count1 = 0 a0 = [] a1 = [] ans = 0 for i in range(n): if i % 2 == 0 and a[i] % 2 == 1: x = i count0 = count0 + 1 if i % 2 == 1 and a[i] % 2 == 1: y = i count1 = count1 + 1 if count0 > count1: for i in range(n): if i % 2 == 1 and a[i] % 2 == 1 or i % 2 == 0 and a[i] % 2 == 0: ans = ans + 1 a0.append(i) print(ans) for i in range(len(a0)): print(a0[i] + 1, x + 1) elif count0 == 0 and count1 == 0: print("-1") else: for i in range(n): if i % 2 == 0 and a[i] % 2 == 1 or i % 2 == 1 and a[i] % 2 == 0: ans = ans + 1 a1.append(i) print(ans) for i in range(len(a1)): print(a1[i] + 1, y + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
def main(): n = int(input()) arr = list(map(int, input().split())) arr1 = [] arr2 = [] t = ans = x = odd = 0 for i in range(n): if arr[i] % 2: odd += 1 if odd: for i in range(n): if arr[i] % 2 != t: arr1.append(i) ans += 1 if arr[i] % 2 and i % 2: x = i if t == 0: t = 1 else: t = 0 t, ans1, x1 = 1, 0, 0 for i in range(n): if arr[i] % 2 != t: arr2.append(i) ans1 += 1 if arr[i] % 2 and i % 2 == 0: x1 = i if t == 0: t = 1 else: t = 0 if ans1 > ans: print(ans) for i in range(ans): print(f"{arr1[i] + 1} {x + 1}") else: print(ans1) for i in range(ans1): print(f"{arr2[i] + 1} {x1 + 1}") else: print(-1) for _ in range(int(input())): main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) c = 0 o = 0 e = 0 for i in range(n): if a[i] & 1: c += 1 if i & 1: o += 1 else: e += 1 if c == 0: print(-1) elif o >= e: ans = [] x = -1 for i in range(n): if a[i] & 1 and i & 1: x = i break for i in range(n): if i & 1 and a[i] % 2 == 0 or i % 2 == 0 and a[i] & 1: ans.append((i + 1, x + 1)) print(len(ans)) for i in ans: print(*i) else: ans = [] x = -1 for i in range(n): if a[i] & 1 and i % 2 == 0: x = i break for i in range(n): if i & 1 and a[i] & 1 or i % 2 == 0 and a[i] % 2 == 0: ans.append((i + 1, x + 1)) print(len(ans)) for i in ans: print(*i)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
def find_k(a): e = 0 i = 0 o = 0 d = 0 for m in range(len(a)): if a[m] % 2 != 0: d += 1 break else: continue while True: if 2 * i >= len(a): break if a[2 * i] % 2 != 0: e += 1 if a[2 * i] % 2 == 0: o += 1 if 2 * i + 1 >= len(a): break if a[2 * i + 1] % 2 == 0: e += 1 if a[2 * i + 1] % 2 != 0: o += 1 i += 1 return e, o, d T = int(input()) for i in range(T): N = int(input()) a = list(map(int, input().split())) i = 0 e, o, d = find_k(a) if d == 0: print(-1) else: print(min(e, o)) while True: if d == 0 or min(e, o) == 0: break if min(e, o) == e: if 2 * i >= len(a): break if a[2 * i] % 2 != 0: for j in range(len(a)): if a[j] % 2 != 0 and 2 * i != j: a[2 * i] = a[2 * i] ^ a[j] print(2 * i + 1, j + 1) break else: continue if 2 * i + 1 >= len(a): break if a[2 * i + 1] % 2 == 0: for j in range(len(a)): if a[j] % 2 != 0: a[2 * i + 1] = a[2 * i + 1] ^ a[j] print(2 * i + 1 + 1, j + 1) break else: continue else: if 2 * i >= len(a): break if a[2 * i] % 2 == 0: for j in range(len(a)): if a[j] % 2 != 0: a[2 * i] = a[2 * i] ^ a[j] print(2 * i + 1, j + 1) break else: continue if 2 * i + 1 >= len(a): break if a[2 * i + 1] % 2 != 0: for j in range(len(a)): if a[j] % 2 != 0 and 2 * i + 1 != j: a[2 * i + 1] = a[2 * i + 1] ^ a[j] print(2 * i + 2, j + 1) break else: continue i += 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER WHILE NUMBER IF BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR WHILE NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER IF BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for t in range(int(input())): n = int(input()) A = list(map(int, input().split())) num = A for i in range(n): num[i] %= 2 if 1 in num: eve = odd = 0 for i in range(n): if num[i] == i % 2: eve += 1 else: odd += 1 temp = [] if eve > odd: for i in range(n): temp.append(i % 2) else: for i in range(n): temp.append((i + 1) % 2) one = 0 flag = 0 for i in range(n): if num[i] == 1 and temp[i] == 1: one = i flag = 1 break if flag != 1: for i in range(n): if temp[i] == 1: temp[i] = 0 else: temp[i] = 1 for i in range(n): if nos[i] == 1 and temp[i] == 1: one = i break m = 0 out = [] for i in range(n): if temp[i] != num[i]: m += 1 if num[i] == 0: out.append([i + 1, one + 1]) else: out.append([i + 1, one + 1]) print(m) for i in out: print(i[0], i[1], sep=" ") else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split(" "))) a = a[:n] even_a = 0 even_b = 0 indices = [] op = 0 for i in range(n): if a[i] % 2 == 0: if i % 2 == 0: even_a += 1 else: even_b += 1 if even_a > even_b: for i in range(n): if i % 2 == 0: if a[i] % 2 != 0: poss = False for j in range(n): if a[j] % 2 != 0 and i != j: a[i] = a[i] ^ a[j] indices.append([i + 1, j + 1]) poss = True op += 1 break if not poss: op = -1 break elif a[i] % 2 == 0: poss = False for j in range(n): if a[j] % 2 != 0 and i != j: a[i] = a[i] ^ a[j] indices.append([i + 1, j + 1]) poss = True op += 1 break if not poss: op = -1 break else: for i in range(n): if i % 2 != 0: if a[i] % 2 != 0: poss = False for j in range(n): if a[j] % 2 != 0 and i != j: a[i] = a[i] ^ a[j] indices.append([i + 1, j + 1]) poss = True op += 1 break if not poss: op = -1 break elif a[i] % 2 == 0: poss = False for j in range(n): if a[j] % 2 != 0 and i != j: a[i] = a[i] ^ a[j] indices.append([i + 1, j + 1]) poss = True op += 1 break if not poss: op = -1 break print(op) for i in indices: print(i[0], i[1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
t = int(input()) while t > 0: n = int(input()) a = list(map(int, input().split())) count = 0 for i in range(n): if a[i] % 2 == 1: count += 1 if count == 0: print(-1) else: ans1 = [] ans2 = [] oddpos = -1 evenpos = -1 ele1 = 0 ele2 = 0 for i in range(n): if a[i] % 2 == 1 and i % 2 == 1: oddpos = i ele1 = a[i] if a[i] % 2 == 1 and i % 2 == 0: evenpos = i ele2 = a[i] if oddpos >= 0: for i in range(n): if a[i] & 1 == 1 and i & 1 == 0: ans1.append((i + 1, oddpos + 1)) if a[i] & 1 == 0 and i & 1 == 1: ans1.append((i + 1, oddpos + 1)) if evenpos >= 0: for i in range(n): if a[i] & 1 == 1 and i & 1 == 1: ans2.append((i + 1, evenpos + 1)) if a[i] & 1 == 0 and i & 1 == 0: ans2.append((i + 1, evenpos + 1)) ans1len = len(ans1) ans2len = len(ans2) if ans2len == 0: print(ans1len) for i in ans1: print(i[0], i[1]) elif ans1len == 0: print(ans2len) for i in ans2: print(i[0], i[1]) elif len(ans1) < len(ans2): print(ans1len) for i in ans1: print(i[0], i[1]) else: print(ans2len) for i in ans2: print(i[0], i[1]) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) evenI, oddI = 0, 0 even, odd, oddP = False, False, False kE, kO = 0, 0 wE, wO = [], [] for i in range(n): if a[i] % 2 != 0: oddP = True if i % 2 == 0: evenI = i even = True else: oddI = i odd = True if not oddP: print("-1") else: if even: for i in range(n): if i % 2 == 0 and a[i] % 2 == 0: kE += 1 wE.append(i + 1) if i % 2 != 0 and a[i] % 2 != 0: kE += 1 wE.append(i + 1) if odd: for i in range(n): if i % 2 == 0 and a[i] % 2 != 0: kO += 1 wO.append(i + 1) if i % 2 != 0 and a[i] % 2 == 0: kO += 1 wO.append(i + 1) if kE > kO: if kO > 0: print(kO) for i in range(kO): print(wO[i], oddI + 1, end=" ") else: print(kE) for i in range(kE): print(wE[i], evenI + 1, end=" ") elif kE <= kO: if kE > 0: print(kE) for i in range(kE): print(wE[i], evenI + 1, end=" ") else: print(kO) for i in range(kO): print(wO[i], oddI + 1, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
p1 = [0, 1] p2 = [1, 0] def parity(x): return x & 1 for t in range(int(input())): n = int(input()) a = tuple(map(int, input().strip().split())) idx = 0 c1 = 0 c2 = 0 sol_exists = False s1 = [] s2 = [] o1 = -1 o2 = -1 for i in range(n): p = parity(a[i]) if p != p1[idx]: s1.append(i + 1) c1 += 1 elif p == 1: o1 = i + 1 if p != p2[idx]: s2.append(i + 1) c2 += 1 elif p == 1: o2 = i + 1 idx = (idx + 1) % 2 if o1 == -1 and o2 == -1: print(-1) continue m = min(c1, c2) print(m) if c1 <= c2: for i in s1: print(i, o1) else: for i in s2: print(i, o2)
ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FUNC_DEF RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
t = int(input()) while t: n = int(input()) ls = list(map(int, input().split(" "))) lastOddPos, evenPos, oddPos = -1, 0, 0 if [(ele % 2) for ele in ls].count(1) == 0: print(-1) t -= 1 continue for i in range(1, n + 1): if i % 2: if ls[i - 1] % 2: evenPos += 1 else: oddPos += 1 elif ls[i - 1] % 2: oddPos += 1 else: evenPos += 1 if ls[i - 1] % 2: lastOddPos = i if oddPos < evenPos: print(oddPos) for i in range(1, n + 1): if i % 2: if ls[i - 1] % 2 == 0: print(i, lastOddPos) elif ls[i - 1] % 2 == 1: print(i, lastOddPos) if i == 1: lastOddPos = 1 else: print(evenPos) for i in range(1, n + 1): if i % 2: if ls[i - 1] % 2 == 1: print(i, lastOddPos) elif ls[i - 1] % 2 == 0: print(i, lastOddPos) if i == 2: lastOddPos = 2 t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) mp = -1 a = [] for i in range(n): a.append(arr[i] & 1) if a[i] == 1: mp = i if mp == -1: print(-1) continue a1 = [] for i in range(1, n, 2): if a[i] == 0: a1.append([i + 1, mp + 1]) for i in range(0, n, 2): if a[i] == 1: a1.append([i + 1, 2]) a2 = [] for i in range(0, n, 2): if a[i] == 0: a2.append([i + 1, mp + 1]) for i in range(1, n, 2): if a[i] == 1: a2.append([i + 1, 1]) if len(a1) > len(a2): a1 = a2 print(len(a1)) for i in a1: print(*i)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
def solver(n, arr): def utility(arr): indices = [[], []] for i in range(n): if parity[i] == arr[i]: indices[parity[i]].append(i) altered = [] for i in range(n): if parity[i] != arr[i]: search_val = 1 found_index = -1 for j in indices[search_val]: if j != i: found_index = j break if found_index == -1: return None altered.append((i, found_index)) parity[i] = search_val return altered parity = [(i % 2) for i in arr] s1 = [(i % 2) for i in range(n)] s2 = [((i + 1) % 2) for i in range(n)] score1 = score2 = 0 for i in range(n): if parity[i] == s1[i]: score1 += 1 else: score2 += 1 if score1 > score2: c1 = utility(s1) c2 = None elif score2 > score1: c2 = utility(s2) c1 = None else: c1 = utility(s1) parity = [(i % 2) for i in arr] c2 = utility(s2) if c1 is None and c2 is None: print("-1") else: if c1 is None: changes = c2 elif c2 is None: changes = c1 elif len(c1) <= len(c2): changes = c1 else: changes = c2 print(len(changes)) for tup in changes: print(f"{tup[0] + 1} {tup[1] + 1}") t = int(input()) while t > 0: t -= 1 n = int(input()) arr = list(map(int, input().split(" "))) solver(n, arr)
FUNC_DEF FUNC_DEF ASSIGN VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NONE EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE VAR NONE EXPR FUNC_CALL VAR STRING IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
for _ in range(int(input())): l = int(input()) seq = [(int(i) % 2) for i in input().split()] if not any(seq): print(-1) else: count = [[], []] k = 0 for i in range(l): count[k % 2].append(i) if i < l - 1 and seq[i] == seq[i + 1]: k += 1 min = count[0] if len(count[0]) <= len(count[1]) else count[1] print(len(min)) for i in min: seq[i] = -1 print(i + 1, 1 + seq.index(1))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
from sys import stdin, stdout input = stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] if [(ele % 2) for ele in a].count(1) == 0: print(-1) continue x = delta = 0 for i in range(n): if a[i] % 2 != i % 2: x += 1 if x > n - x: delta += 1 ind = -1 for i in range(n): if a[i] % 2 and (i + delta) % 2: ind = i break assert ind != -1 ops = [] for i in range(n): if a[i] % 2 != (i + delta) % 2: ops.append((i + 1, ind + 1)) print(len(ops)) assert len(ops) == min(x, n - x) for i in ops: a[i[0] - 1] ^= a[i[1] - 1] print(*i) for i in range(n - 1): assert a[i] % 2 != a[i + 1] % 2
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) odd_index_odd = [] odd_index_even = [] even_index_odd = [] even_index_even = [] count = 0 for i in range(n): if i % 2 == 0: if arr[i] % 2 == 0: even_index_even.append(i) count += 1 else: even_index_odd.append(i) elif arr[i] % 2 == 0: odd_index_even.append(i) count += 1 else: odd_index_odd.append(i) if count == n: print(-1) continue no_of_even_at_odd = len(odd_index_even) no_of_odd_at_even = len(even_index_odd) tot1 = no_of_even_at_odd + no_of_odd_at_even no_of_odd_at_odd = len(odd_index_odd) no_of_even_at_even = len(even_index_even) tot2 = no_of_odd_at_odd + no_of_even_at_even if tot1 < tot2: print(tot1) if no_of_odd_at_odd != 0: ind = odd_index_odd[0] else: ind = even_index_odd[0] for i in odd_index_even: print(i + 1, ind + 1) for i in even_index_odd: print(i + 1, 2) else: print(tot2) if no_of_odd_at_even != 0: ind = even_index_odd[0] else: ind = odd_index_odd[0] for i in even_index_even: print(i + 1, ind + 1) for i in odd_index_odd: print(i + 1, 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER
Chef loves bitwise operations. So, he creates the following problem but needs your help to solve it. Chef calls a sequence of integers A_{1}, A_{2}, \ldots, A_{N} tasty if it satisfies the following condition: parity(A_{i} \& A_{i+1}) \neq parity(A_{i} | A_{i+1}) for 1 ≀ i < N. Chef gives you a sequence A_{1}, A_{2}, \ldots, A_{N}. You may perform the following operation on the sequence any number of times (possibly 0): Choose 2 indices i and j (1 ≀ i, j ≀ n ; i \neq j) and change A_{i} to A_{i} \oplus A_{j}. Chef is asking you to make the sequence tasty using the minimum number of operations, or report that it is impossible. As a friendly reminder, parity(x) denotes the remainder of dividing x by 2 \& denotes the [bitwise AND operation] | denotes the [bitwise OR operation] \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case: - If it is impossible to convert the given sequence into a tasty sequence using the given operations, print a single line containing the integer -1. - Otherwise, first, print a line containing a single integer M ― the minimum number of operations you have to perform. - Then, print M lines describing these operations in the order in which you want to perform them. For each k (1 ≀ k ≀ M), the k-th of these lines should contain two space-separated integers i and j (1 ≀ i, j ≀ n ; i \neq j) ― the indices on which the k-th operation is performed. If there are multiple solutions, you may find any one of them. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{3}$ $2 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} < 2^{30}$ for each valid $i$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 0 0 3 6 16 69 ----- Sample Output 1 ------ -1 1 1 3 ----- explanation 1 ------ Test case $1$: It is impossible to make $A$ tasty using the given operations. Test case $2$: We can choose $i=1$ and $j=3$ and apply the operation which converts $A$ into $[\textcolor{blue}{67},16,\textcolor{blue}{69}]$ which is tasty.
t = int(input()) while t: t -= 1 n = int(input()) l = list(map(int, input().split())) if all([(i & 1 == 0) for i in l]): print("-1") continue l1 = [] l2 = [] l3 = [] odd_index = 0 for i in range(n): if i & 1 == 0: if l[i] & 1 == 0: l2.append(i) else: l1.append(i) elif l[i] & 1 == 0: l1.append(i) else: l2.append(i) op_req = min(len(l1), len(l2)) if len(l1) > len(l2): l3 = l2 else: l3 = l1 for i in range(len(l)): if l[i] & 1 == 1 and i not in l3: odd_index = i break else: continue print(op_req) for i in l3: print(i + 1, odd_index + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. -----Input----- The first line contains two integers n and m (1 ≀ n, m ≀ 10^6) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers a_{i} (1 ≀ a_{i} ≀ 10^9) β€” the elements of a. -----Output----- In the first line print two integers l and k_{max} (1 ≀ l ≀ m, 0 ≀ k_{max} ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print k_{max} integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. -----Examples----- Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3
from sys import stdin input = stdin.buffer.readline n, m = map(int, input().split()) cnt = [0] * (m + 1) (*b,) = map(int, input().split()) for i in b: if i <= m: cnt[i] += 1 a = [0] * (m + 1) for i in range(1, m + 1): for j in range(i, m + 1, i): a[j] += cnt[i] mx = idx = 0 for i in range(1, m + 1): if mx < a[i]: mx, idx = a[i], i if mx == 0: exit(print(1, 0)) print(idx, mx) for i in range(n): if idx % b[i] == 0: print(i + 1)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. -----Input----- The first line contains two integers n and m (1 ≀ n, m ≀ 10^6) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers a_{i} (1 ≀ a_{i} ≀ 10^9) β€” the elements of a. -----Output----- In the first line print two integers l and k_{max} (1 ≀ l ≀ m, 0 ≀ k_{max} ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print k_{max} integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. -----Examples----- Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3
import sys n, m = [int(x) for x in input().split()] A = [int(x) for x in input().split()] B, C = [0] * (m + 1), [0] * (m + 1) for a in A: if a <= m: B[a] += 1 for i in range(2, m + 1): for j in range(i, m + 1, i): C[j] += B[i] k, l = 1, 0 for i in range(2, m + 1): if C[i] > l: l = C[i] k = i print(k, l + B[1]) for i, a in enumerate(A): if k % a == 0: sys.stdout.write(str(i + 1) + " ")
IMPORT 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 VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING
You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. -----Input----- The first line contains two integers n and m (1 ≀ n, m ≀ 10^6) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers a_{i} (1 ≀ a_{i} ≀ 10^9) β€” the elements of a. -----Output----- In the first line print two integers l and k_{max} (1 ≀ l ≀ m, 0 ≀ k_{max} ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print k_{max} integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. -----Examples----- Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3
import sys n, m = map(int, sys.stdin.buffer.readline().decode("utf-8").split()) a = list(map(int, sys.stdin.buffer.readline().decode("utf-8").split())) dp = [0] * (m + 1) for x in a: if x <= m: dp[x] += 1 for i in range(m, 0, -1): for j in range(2, m + 1): if i * j > m: break dp[i * j] += dp[i] lcm = max(1, dp.index(max(dp))) ans = [i for i, x in enumerate(a, start=1) if lcm % x == 0] sys.stdout.buffer.write( (str(lcm) + " " + str(len(ans)) + "\n" + " ".join(map(str, ans))).encode("utf-8") )
IMPORT 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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING
Little C loves number Β«3Β» very much. He loves all things about it. Now he is playing a game on a chessboard of size n Γ— m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (x_i,y_i) and (x_j,y_j) is defined as |x_i-x_j|+|y_i-y_j|. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. Input A single line contains two integers n and m (1 ≀ n,m ≀ 10^9) β€” the number of rows and the number of columns of the chessboard. Output Print one integer β€” the maximum number of chessmen Little C can place. Examples Input 2 2 Output 0 Input 3 3 Output 8 Note In the first example, the Manhattan distance between any two cells is smaller than 3, so the answer is 0. In the second example, a possible solution is (1,1)(3,2), (1,2)(3,3), (2,1)(1,3), (3,1)(2,3).
n, m = map(int, input().split()) q = min(n, m) w = max(n, m) if q == 1 and w <= 3 or q == 2 == w: print("0") elif q == 1 and w == 4: print("2") elif q == 1 and w == 5: print("4") elif q == 1 and w == 6: print("6") elif q == 2 and w == 3: print("4") elif q == 1: r = w // 6 if w % 6 == 0: print(w) elif w % 6 <= 3: print(r * 6) elif w % 6 == 4: print(r * 6 + 2) elif w % 6 == 5: print(r * 6 + 4) elif q == 2 and w == 7: print("12") elif q * w % 2 == 0: print(q * w) else: print(q * w - 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
Little C loves number Β«3Β» very much. He loves all things about it. Now he is playing a game on a chessboard of size n Γ— m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (x_i,y_i) and (x_j,y_j) is defined as |x_i-x_j|+|y_i-y_j|. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. Input A single line contains two integers n and m (1 ≀ n,m ≀ 10^9) β€” the number of rows and the number of columns of the chessboard. Output Print one integer β€” the maximum number of chessmen Little C can place. Examples Input 2 2 Output 0 Input 3 3 Output 8 Note In the first example, the Manhattan distance between any two cells is smaller than 3, so the answer is 0. In the second example, a possible solution is (1,1)(3,2), (1,2)(3,3), (2,1)(1,3), (3,1)(2,3).
n, m = map(int, input().split(" ")) if n == 1 or m == 1: print(n * m // 6 * 6 + (n * m % 6 > 3) * (n * m % 6 % 3 * 2)) elif n * m < 6: print(0) elif n * m == 6: print(4) elif n * m == 14: print(12) else: print(n * m - n * m % 2)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER
Little C loves number Β«3Β» very much. He loves all things about it. Now he is playing a game on a chessboard of size n Γ— m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (x_i,y_i) and (x_j,y_j) is defined as |x_i-x_j|+|y_i-y_j|. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. Input A single line contains two integers n and m (1 ≀ n,m ≀ 10^9) β€” the number of rows and the number of columns of the chessboard. Output Print one integer β€” the maximum number of chessmen Little C can place. Examples Input 2 2 Output 0 Input 3 3 Output 8 Note In the first example, the Manhattan distance between any two cells is smaller than 3, so the answer is 0. In the second example, a possible solution is (1,1)(3,2), (1,2)(3,3), (2,1)(1,3), (3,1)(2,3).
n, m = map(int, input().split()) if n >= 4 and m >= 4: print(n * m - n * m % 2) elif min(n, m) == 1: if max(n, m) % 6 == 5: print(max(n, m) // 6 * 6 + 4) elif max(n, m) % 6 == 4: print(max(n, m) // 6 * 6 + 2) else: print(max(n, m) // 6 * 6) elif min(n, m) == 2 and max(n, m) == 7: print(12) elif min(n, m) == 2 and max(n, m) >= 4 or min(n, m) == 3 and max(n, m) >= 4: print(n * m - n * m % 2) else: d = [ [0, 0, 0, 2, 4], [0, 0, 4, 8, 10], [0, 0, 8, 12, 14], [0, 0, 0, 16, 20], [0, 0, 0, 0, 24], ] print(d[min(n, m) - 1][max(n, m) - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER
Little C loves number Β«3Β» very much. He loves all things about it. Now he is playing a game on a chessboard of size n Γ— m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (x_i,y_i) and (x_j,y_j) is defined as |x_i-x_j|+|y_i-y_j|. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. Input A single line contains two integers n and m (1 ≀ n,m ≀ 10^9) β€” the number of rows and the number of columns of the chessboard. Output Print one integer β€” the maximum number of chessmen Little C can place. Examples Input 2 2 Output 0 Input 3 3 Output 8 Note In the first example, the Manhattan distance between any two cells is smaller than 3, so the answer is 0. In the second example, a possible solution is (1,1)(3,2), (1,2)(3,3), (2,1)(1,3), (3,1)(2,3).
class Solution(object): def run(self): n, m = [int(x) for x in input().split()] if n > m: n, m = m, n res = n * m // 2 if n == 1 and (m + 1) % 6 // 3: res -= 1 elif n == 2: if m == 2: res = 0 elif m == 3: res = 2 elif m == 7: res = 6 print(res * 2) Solution().run()
CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL FUNC_CALL VAR
Little C loves number Β«3Β» very much. He loves all things about it. Now he is playing a game on a chessboard of size n Γ— m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (x_i,y_i) and (x_j,y_j) is defined as |x_i-x_j|+|y_i-y_j|. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. Input A single line contains two integers n and m (1 ≀ n,m ≀ 10^9) β€” the number of rows and the number of columns of the chessboard. Output Print one integer β€” the maximum number of chessmen Little C can place. Examples Input 2 2 Output 0 Input 3 3 Output 8 Note In the first example, the Manhattan distance between any two cells is smaller than 3, so the answer is 0. In the second example, a possible solution is (1,1)(3,2), (1,2)(3,3), (2,1)(1,3), (3,1)(2,3).
N, M = map(int, input().split()) l = [0, 1, 2, 3, 2, 1] res = float("inf") for n, m in ((N, M), (M, N)): if l[n % 6] == 0: print(n * m) exit() elif l[n % 6] == 1: tmp = l[m % 6] res = min(res, tmp) elif l[n % 6] == 2: if m >= 4 and m != 7: print(n * m) exit() if m == 2: tmp = 4 else: tmp = 2 res = min(res, tmp) else: if m == 1: tmp = 3 elif m == 2: tmp = 2 elif m % 2 == 0: print(n * m) exit() else: tmp = 1 res = min(res, tmp) print(n * m - res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Little C loves number Β«3Β» very much. He loves all things about it. Now he is playing a game on a chessboard of size n Γ— m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (x_i,y_i) and (x_j,y_j) is defined as |x_i-x_j|+|y_i-y_j|. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. Input A single line contains two integers n and m (1 ≀ n,m ≀ 10^9) β€” the number of rows and the number of columns of the chessboard. Output Print one integer β€” the maximum number of chessmen Little C can place. Examples Input 2 2 Output 0 Input 3 3 Output 8 Note In the first example, the Manhattan distance between any two cells is smaller than 3, so the answer is 0. In the second example, a possible solution is (1,1)(3,2), (1,2)(3,3), (2,1)(1,3), (3,1)(2,3).
n, m = map(int, input().split()) if n > m: n, m = m, n if n == 1: ans = m // 6 * 6 + 2 * max(m % 6 - 3, 0) print(ans) elif n == 2: if m == 2: print(0) elif m == 3: print(4) elif m == 7: print(12) else: print(n * m) else: ans = n * m // 2 * 2 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Little C loves number Β«3Β» very much. He loves all things about it. Now he is playing a game on a chessboard of size n Γ— m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (x_i,y_i) and (x_j,y_j) is defined as |x_i-x_j|+|y_i-y_j|. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. Input A single line contains two integers n and m (1 ≀ n,m ≀ 10^9) β€” the number of rows and the number of columns of the chessboard. Output Print one integer β€” the maximum number of chessmen Little C can place. Examples Input 2 2 Output 0 Input 3 3 Output 8 Note In the first example, the Manhattan distance between any two cells is smaller than 3, so the answer is 0. In the second example, a possible solution is (1,1)(3,2), (1,2)(3,3), (2,1)(1,3), (3,1)(2,3).
n, m = map(int, input().split()) if n < m: n, m = m, n ans = 0 d = {(0): 0, (1): 1, (2): 2, (3): 3, (4): 2, (5): 1} if m == 1: ans = d[n % 6] elif m == 2: if n == 3: ans = 2 elif n == 2: ans = 4 elif n == 7: ans = 2 else: ans = n * m % 2 print(n * m - ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Little C loves number Β«3Β» very much. He loves all things about it. Now he is playing a game on a chessboard of size n Γ— m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (x_i,y_i) and (x_j,y_j) is defined as |x_i-x_j|+|y_i-y_j|. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. Input A single line contains two integers n and m (1 ≀ n,m ≀ 10^9) β€” the number of rows and the number of columns of the chessboard. Output Print one integer β€” the maximum number of chessmen Little C can place. Examples Input 2 2 Output 0 Input 3 3 Output 8 Note In the first example, the Manhattan distance between any two cells is smaller than 3, so the answer is 0. In the second example, a possible solution is (1,1)(3,2), (1,2)(3,3), (2,1)(1,3), (3,1)(2,3).
n, m = map(int, input().strip().split()) f = min(n, m) f1 = max(n, m) def s(g): if g % 6 == 1 or g % 6 == 5: return 1 if g % 6 == 2 or g % 6 == 4: return 2 if g % 6 == 0: return 0 if g % 6 == 3: return 3 if f == 1: f3 = s(f1) print(n * m - f3) exit(0) if f == 2 and f1 == 3: print(4) exit(0) if f == 2 and f1 == 2: print(0) exit(0) if f == 2 and f1 == 7: print(12) exit(0) if f == 3 and f1 == 3: print(8) exit(0) if f % 2 == 0 and f1 % 2 == 0: print(n * m) exit(0) if f % 2 == 1 and f1 % 2 == 0: print(n * m) exit(0) if f % 2 == 0 and f1 % 2 == 1: print(n * m) exit(0) if f % 2 == 1 and f1 % 2 == 1: print(n * m - 1) exit(0)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Little C loves number Β«3Β» very much. He loves all things about it. Now he is playing a game on a chessboard of size n Γ— m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (x_i,y_i) and (x_j,y_j) is defined as |x_i-x_j|+|y_i-y_j|. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. Input A single line contains two integers n and m (1 ≀ n,m ≀ 10^9) β€” the number of rows and the number of columns of the chessboard. Output Print one integer β€” the maximum number of chessmen Little C can place. Examples Input 2 2 Output 0 Input 3 3 Output 8 Note In the first example, the Manhattan distance between any two cells is smaller than 3, so the answer is 0. In the second example, a possible solution is (1,1)(3,2), (1,2)(3,3), (2,1)(1,3), (3,1)(2,3).
a, b = map(int, input().split()) a, b = min(a, b), max(a, b) if a == 1: c = b % 6 if c == 4: print(b // 6 * 6 + 2) elif c == 5: print(b // 6 * 6 + 4) else: print(b // 6 * 6) elif a == 2: if b <= 2: print(0) elif b == 3 or b == 7: print(a * b - 2) else: print(b * 2) else: ans = a * b if ans % 2 == 0: print(ans) else: print(ans - 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) a = list(map(int, input().split())) if n & 1: n -= 1 ans = 0 for i in range(0, n, 2): now = low = a[i] - a[i + 1] ans += min(a[i], a[i + 1]) for j in range(i + 2, n, 2): now += a[j] - a[j + 1] if now >= 0: tmp = a[i] - now ans += max(min(low - now, tmp, a[j + 1]), -1) + 1 else: tmp = a[j + 1] + now ans += max(min(low, a[i], tmp), -1) + 1 low = min(low, now) if low < 0: break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) c = list(map(int, input().split())) ans = 0 for i in range(n): if i % 2: continue min_depth = 0 bal = 0 for j in range(i + 1, n): if (j - i) % 2: ans += max(0, min(c[i], c[j] - bal) - max(-min_depth, 1 - bal) + 1) bal -= c[j] min_depth = min(min_depth, bal) else: bal += c[j] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
import sys input = sys.stdin.readline def main(): t = 1 class RangeQuery: def __init__(self, data, func=min): self.func = func self._data = _data = [list(data)] i, n = 1, len(_data[0]) while 2 * i <= n: prev = _data[-1] _data.append([func(prev[j], prev[j + i]) for j in range(n - 2 * i + 1)]) i <<= 1 def query(self, start, stop): depth = (stop - start).bit_length() - 1 return self.func( self._data[depth][start], self._data[depth][stop - (1 << depth)] ) def __getitem__(self, idx): return self._data[0][idx] for _ in range(t): n = int(input()) ar = list(map(int, input().split())) signs = [0] for i, x in enumerate(ar): if i % 2: signs.append(signs[-1] - x) else: signs.append(signs[-1] + x) rq = RangeQuery(signs, min) ans = 0 for l in range(0, n, 2): for r in range(l + 1, n, 2): left = ar[l] right = ar[r] if r - l == 1: ans += min(left, right) continue stuff = signs[r] - signs[l + 1] minleft = rq.query(l + 2, r + 1) - signs[l + 1] minleft = max(-minleft, 1) equivmin = stuff + minleft ans += max(min(left - minleft + 1, right - equivmin + 1), 0) print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF RETURN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) a = [*map(int, input().split())] t = 0 for l in range(0, n, 2): mn = 0 c = 0 for r in range(l + 1, n, 2): if r - l > 1: mn = min(mn, c - a[r - 2]) c += a[r - 1] - a[r - 2] t += max(-1, min(a[l] + mn, a[r] + mn - c)) + (r - l > 1) print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) a = list(map(int, input().split())) stack = [] ans = 0 for i, c in enumerate(a): if i % 2: while c and stack: if stack[-1] < 0: stack.pop() else: m = min(stack[-1], c) c -= m ans += m if stack[-1] == m: stack.pop() else: stack[-1] -= m if stack and stack[-1] < 0: ans -= stack[-1] stack[-1] -= 1 else: stack.append(-1) if c: stack.clear() elif stack and stack[-1] > 0: stack[-1] += c else: stack.append(c) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER WHILE VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
import sys ints = (int(x) for x in sys.stdin.read().split()) sys.setrecursionlimit(3000) class Stack(list): ans = 0 lzeros = 0 zeros = [] def save(self, n, k): self.ans += n + k * (k - 1) // 2 def read(self, seq): it = iter(seq) for a, b in zip(it, it): self.append(a) self.zeros.append(0) self.close(b) while self: self.pop() self.save(0, self.zeros.pop()) self.save(0, self.lzeros) self.lzeros = 0 return self.ans def close(self, n): assert n > 0 while self and n >= self[-1]: n -= self[-1] self.save(self.pop(), self.zeros.pop()) if self: self.zeros[-1] += 1 else: self.lzeros += 1 if n > 0: if not self: self.save(0, self.lzeros) self.lzeros = 0 elif n < self[-1]: self.save(n, self.zeros[-1]) self[-1] -= n self.zeros[-1] = 1 return def main(): n = next(ints) stack = Stack() stack.read([next(ints) for i in range(n)]) print(stack.ans) return main()
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER CLASS_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER RETURN VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
import sys def il(): return list(map(int, sys.stdin.readline().strip().split())) def ii(): return int(sys.stdin.readline().strip()) def imi(): return map(int, sys.stdin.readline().strip().split()) n = ii() li = il() s = 0 for i in range(0, n, 2): x = 0 a = li[i] ma = 1 for j in range(i + 1, n): if j % 2 == 0: x += li[j] else: x -= li[j] if x < 0: b = -x if b >= ma: s += min(a - ma + 1, b - ma + 1) ma = max(ma, b) if b > a: break print(s)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
def sol(): n = int(input()) arr = [int(x) for x in input().split()] ans = 0 for pocz in range(0, n, 2): nasze = arr[pocz] reszta = 0 skonczone = 0 wyn = 0 for i in range(pocz, n): if not i % 2: if i != pocz: reszta += arr[i] else: my = arr[i] ile = min(reszta, my) my -= ile reszta -= ile if not reszta and ile and skonczone: wyn += 1 if my and nasze: x = min(my, nasze) wyn += x my -= x nasze -= x skonczone += 1 if my: break ans += wyn print(ans) sol()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
import sys input = sys.stdin.readline n = int(input()) c = [0] + list(map(int, input().split())) s = [0] * (n + 1) for i in range(1, n + 1): s[i] = s[i - 1] if i % 2 == 1: s[i] += c[i] else: s[i] -= c[i] ans = 0 for l in range(0, n): mn = s[l + 1] for r in range(l + 1, n): if s[l] > s[l + 1] or s[r] < s[r + 1]: mn = min(mn, s[r]) continue sub = max(s[l], s[r + 1]) sub2 = min(mn, s[l + 1] - 1, s[r] - 1) if sub <= sub2: ans += sub2 - sub + 1 mn = min(mn, s[r]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) q = dict() ans = 0 a = list(map(int, input().split())) for i in range(1, n, 2): cnt = a[i] max_cnt = a[i] for j in range(i + 1, n): if j % 2 == 0: cnt -= a[j] q[i, j] = [cnt, max_cnt] else: cnt += a[j] max_cnt = max(max_cnt, cnt) for i in range(0, n, 2): for j in range(i + 1, n, 2): if i == j - 1: ans += min(a[i], a[j]) else: l = a[i] r = a[j] p = q[i + 1, j - 1] l -= p[1] p[0] -= p[1] r += p[0] if l >= 0 and r >= 0: ans += min(l, r) + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
def process(A): n = len(A) answer = 0 for i in range(0, n, 2): my_upper = A[i] my_lower = -1 * float("inf") my_sum = 0 for j in range(i + 1, n): if j % 2 == 0: my_sum -= A[j] else: this_upper = min(my_upper, A[j] + my_sum) this_lower = max(1, my_lower) my_lower = max(my_lower, A[j] + my_sum) my_sum += A[j] if this_upper >= this_lower: answer += this_upper - this_lower + 1 else: answer += 0 return answer n = int(input()) A = [int(x) for x in input().split()] print(process(A))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) a = list(map(int, input().split())) ans = 0 stk = [[a[0], 0]] i = 1 fi = [-1] * n while i < n: if i % 2 == 0: stk.append([a[i], i]) else: while a[i] > 0 and len(stk): if a[i] < stk[-1][0]: ans += a[i] stk[-1][0] -= a[i] a[i] = 0 fi[i] = stk[-1][1] else: a[i] -= stk[-1][0] x = stk[-1][1] ans += stk[-1][0] stk.pop() if a[i] == 0: fi[i] = x copyi = i i = x - 1 while i >= 0: if a[i] != 0: break if len(stk) and stk[-1][1] > i: break ans += 1 i = fi[i] i -= 1 i = copyi i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR WHILE VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) s = list(map(int, input().split())) h = 0 sign = 1 low = high = 0 t = {} count = 0 for x in s: h += sign * x if sign == 1: high = h else: count += x for hh in t: if hh >= h and hh <= h + x: count += t[hh] elif hh > h: t[hh] = 0 high = h if h < low: count += h - low low = h else: if not h in t: t[h] = 0 t[h] += 1 for hh in t: if hh > h: t[hh] = 0 sign = -sign print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) arr = [int(x) for x in input().split()] ans = [int(0) for x in range(n + 1)] tempans = [int(0) for x in range(n + 1)] arr.insert(0, 0) for i in range(2, n + 1, 2): if arr[i] < arr[i - 1]: ans[i] += arr[i] else: ans[i] += arr[i - 1] temp = arr[i] ans[i] += tempans[i - 2] temp -= arr[i - 1] j = i - 2 tt = temp temp = 0 while tempans[j] and tt > 0 and j > 0: if arr[j - 1] > temp + tt + arr[j]: ans[i] += tt temp = 0 tt = 0 else: if arr[j - 1] >= arr[j] + temp: ans[i] += arr[j - 1] - (arr[j] + temp) if arr[j - 1] - (arr[j] + temp): ans[i] += tempans[j - 2] temp += arr[j] temp -= arr[j - 1] if temp < 0: tt += temp temp = 0 j -= 2 temp = 0 k = 1 j = i while k or temp > 0 and j > 0: k = 0 temp += arr[j] temp -= arr[j - 1] j -= 2 if temp == 0: tempans[i] = 1 + tempans[j] elif temp < 0: tempans[i] = 1 answer = 0 for i in range(n + 1): answer += ans[i] print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) c = list(map(int, input().split())) ans = 0 for j in range(0, n, 2): boo = c[j] remain = 0 for i in range(j, n): if i % 2 == 0: remain += c[i] else: remain -= c[i] if remain <= boo: ans += boo - max(remain, 0) boo = min(remain + 1, boo) if remain < 0: break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) c = [int(x) for x in input().split()] ans = 0 for i in range(0, n, 2): dt = 0 s = 0 for j in range(i + 1, n): if j & 1: L = max(1, -s) R = c[i] if L <= R: L += dt R += dt L = max(L, 1) R = min(R, c[j]) ans += max(0, R - L + 1) dt -= c[j] else: dt += c[j] s = min(s, dt) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
from sys import stdin, stdout ip = lambda: stdin.readline().rstrip("\r\n") ips = lambda: ip().split() out = lambda x, end="\n": stdout.write(str(x) + end) n = int(ip()) a = list(map(int, ips())) ans = 0 for i in range(n): no, nc, p = 0, 0, 0 if i & 1: continue x = a[i] if i + 1 < n: ans += min(a[i], a[i + 1]) x -= min(a[i], a[i + 1]) if i + 1 < n and a[i + 1] > a[i]: continue y = 0 f = 0 for j in range(i + 2, n): if j & 1 == 0: y += a[j] else: z = a[j] if y > z: y -= z continue else: z -= y y = 0 ans += min(x, z) ans += 1 if x >= z: x -= z else: f = 1 if f: break out(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
import sys n = int(sys.stdin.readline()) data = list(map(int, sys.stdin.readline().split())) ans, cur_open = 0, 0 s = [[0, 0]] for i in range(0, n, 2): if i + 1 >= n: continue open_bracket = data[i] close_bracket = data[i + 1] cur_open += open_bracket - close_bracket ans += min(close_bracket, cur_open + close_bracket - s[0][0]) while s and s[-1][0] > cur_open: ans += s[-1][1] s.pop() if s and s[-1][0] == cur_open: ans += s[-1][1] s[-1][1] += 1 elif s: s.append([cur_open, 1]) else: s.append([cur_open, 0]) print(ans)
IMPORT 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 NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER WHILE VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
import sys input = sys.stdin.readline def sv(ls): ret = 0 deep = 0 stack = [] for i in range(len(ls)): v = ls[i] if i % 2 == 0: deep += v else: append = True if v > deep: append = False v = min(v, deep) ret += v deep -= v memo = None while stack: depth, vv = stack[-1] if depth < deep: break depth, vv = stack.pop() if append and depth == deep: memo = [depth, vv + 1] ret += vv if append: if memo: stack.append(memo) else: stack.append([deep, 1]) return ret def main(): input() a = list(map(int, input().split())) print(sv(a)) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NONE WHILE VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
import sys input = lambda: sys.stdin.readline().strip() def solve(): n = int(input()) arr = list(map(int, input().split())) res = 0 for i in range(0, n, 2): x = xmin = 0 for j in range(i + 1, n): if j & 1: l = max(1, 1 - x, -xmin) r = min(arr[i], arr[j] - x) if l <= r: res += r - l + 1 x -= arr[j] xmin = min(xmin, x) if arr[i] + xmin < 0: break else: x += arr[j] return res print(solve())
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
l = int(input()) s = [int(i) for i in input().split()] p = ans = 0 st = [] for i in range(l): if i % 2: if p >= s[i]: p -= s[i] ans += s[i] else: ans += p p = -1 while st and st[-1][0] > p: pp, t = st.pop() ans += t if st and st[-1][0] == p: ans += st[-1][1] st[-1][1] += 1 elif p == -1: p = 0 else: st.append([p, 1]) else: p += s[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
from sys import stdin, stdout input = stdin.readline def output(answer): stdout.write("{}\n".format(answer)) def transfer(stack, other, flag): if flag == 0: while len(other) > 0: stack.append(other.pop()) else: while len(other) > 0: v = other.pop() if v > 0: stack.append(v) for _ in range(1): n = int(input()) arr = list(map(int, input().split())) stack = [None] count = 0 for index in range(n): if index % 2 == 0: stack.append(arr[index]) continue curr = arr[index] other = [] flag = 0 while stack[-1] != None and curr != 0 and stack[-1] <= curr: value = stack.pop() if value < 0: count += 1 other.append(value) elif value > curr: count += curr stack.append(value - curr) stack.append(-1) curr = 0 break elif value <= curr: if value == curr: count += curr curr = 0 itr = len(stack) - 1 while itr > 0 and stack[itr] != None and stack[itr] < 0: count += 1 itr -= 1 stack.append(-1) else: count += value other.append(-1) curr -= value flag = 1 if stack[-1] == None: transfer(stack, other, flag) if curr > 0: stack.append(None) elif curr == 0: transfer(stack, other, flag) elif curr != 0: value = stack.pop() stack.append(value - curr) stack.append(-1) count += curr transfer(stack, other, 1) output(count)
ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF IF VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER NONE VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR NONE VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NONE IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
def main(): res = 0 n = int(input()) a = list(map(int, input().split())) a = [0] + a for i in range(1, n, 2): ans = 0 _ans = 0 for j in range(i + 1, n + 1, 2): if a[i] < -_ans: break l = max(0, ans - _ans) r = min(a[j], ans + a[i]) res += max(0, r - l + 1) _ans = min(_ans, ans - a[j]) if j + 1 < n + 1: ans += a[j + 1] - a[j] print(res - n // 2) main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
N = int(input()) cs = list(map(int, input().split())) stack = [] p = 0 min_height = 0 height = 0 answer = 0 for c in cs: if p == 0: height += c else: new_height = height - c while stack and stack[-1][0] > new_height: h, cnt = stack.pop() answer += cnt - 1 if stack and stack[-1][0] == new_height: answer += stack[-1][1] - 1 answer += height - max(min_height, new_height) if stack and stack[-1][0] == new_height: stack[-1] = stack[-1][0], stack[-1][1] + 1 elif new_height < min_height: stack.append((new_height, 1)) else: stack.append((new_height, 2)) height = new_height min_height = min(min_height, height) p ^= 1 print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) aa = list(map(int, input().split())) ans = 0 stk = [] for i in range(n): if not stk: stk.append([0, 1]) if i & 1: while aa[i] and stk: if stk[-1][1] == 0: stk.pop() if stk: now = stk.pop() sub = min(now[0], aa[i]) aa[i] -= sub now[0] -= sub ans += sub if now[0]: stk.append(now) if stk and stk[-1][1] == 0: ans += stk[-1][0] if aa[i]: stk.append([0, 1]) else: if not stk: stk.append([0, 0]) if stk[-1][1] == 0: stk[-1][0] += 1 else: stk.append([1, 0]) else: if stk[-1][1] == 0: stk.append([0, 1]) stk[-1][0] += aa[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF BIN_OP VAR NUMBER WHILE VAR VAR VAR IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
if 1: n = int(input()) c = list(map(int, input().split())) count = 0 open_brackets_left = 0 prev_brackets_left = [] for i, c_i in enumerate(c): if i % 2: count += min(c_i, open_brackets_left) open_brackets_left -= c_i while prev_brackets_left: if prev_brackets_left[-1][0] > open_brackets_left: _, num = prev_brackets_left.pop() count += num else: break if prev_brackets_left and prev_brackets_left[-1][0] == open_brackets_left: count += prev_brackets_left[-1][1] prev_brackets_left[-1][1] += 1 else: prev_brackets_left.append([open_brackets_left, 1]) if open_brackets_left < 0: open_brackets_left = 0 prev_brackets_left = [] else: open_brackets_left += c_i print(count)
IF NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input().strip()) a = [int(x) for x in input().strip().split(" ")] ans = 0 for i in range(0, n, 2): now = 0 mn_now = a[i] - 1 for j in range(i, n): if j & 1: pre_now = now now -= a[j] ans += max(0, min(pre_now, mn_now + 1) - max(0, now)) mn_now = min(mn_now, now) else: now += a[j] if mn_now < 0: break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
class SegTree: def __init__(self, init_val, ide_ele, segfunc): self.n = len(init_val) self.num = 2 ** (self.n - 1).bit_length() self.ide_ele = ide_ele self.segfunc = segfunc self.seg = [ide_ele] * 2 * self.num for i in range(self.n): self.seg[i + self.num] = init_val[i] for i in range(self.num - 1, 0, -1): self.seg[i] = self.segfunc(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.num self.seg[k] = x while k: k = k >> 1 self.seg[k] = self.segfunc(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, l, r): if r <= l: return self.ide_ele l += self.num r += self.num lres = self.ide_ele rres = self.ide_ele while l < r: if r & 1: r -= 1 rres = self.segfunc(self.seg[r], rres) if l & 1: lres = self.segfunc(lres, self.seg[l]) l += 1 l = l >> 1 r = r >> 1 res = self.segfunc(lres, rres) return res def __str__(self): arr = [self.query(i, i + 1) for i in range(self.n)] return str(arr) def segfuncm(x, y): if x <= y: return x else: return y def segfuncM(x, y): if x >= y: return x else: return y INF = 1 << 60 def main(): n = int(input()) C = list(map(int, input().split())) CS = [0] * (n + 1) for i in range(n): if i % 2 == 0: CS[i + 1] = CS[i] + C[i] else: CS[i + 1] = CS[i] - C[i] segm = SegTree(CS, INF, segfuncm) segM = SegTree(CS, -INF, segfuncM) ans = 0 for l in range(n): if l % 2 == 1: continue for r in range(l, n): if r % 2 == 0: continue x = CS[r] - CS[l + 1] a = C[l] b = C[r] m = segm.query(l + 2, r + 1) - CS[l + 1] M = segM.query(l + 2, r + 1) - CS[l + 1] p = max(1, 1 - x, -m) q = min(a, b - x) if q < p: continue else: ans += q - p + 1 print(ans) main()
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
n = int(input()) u = list(map(int, input().split())) ans = -(n // 2) for i in range(0, n, 2): x = u[i] p = 0 for j in range(i + 1, n): if j % 2: if p > u[j]: p -= u[j] elif x + p < u[j]: ans += x + 1 break else: ans += u[j] - p + 1 x += p - u[j] p = 0 else: p += u[j] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $c_1, c_2, \dots, c_n$ where $c_i$ is the number of consecutive brackets "(" if $i$ is an odd number or the number of consecutive brackets ")" if $i$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $[3, 2, 1, 3]$. You need to find the total number of continuous subsequences (subsegments) $[l, r]$ ($l \le r$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 1000)$, the size of the compressed sequence. The second line contains a sequence of integers $c_1, c_2, \dots, c_n$ $(1 \le c_i \le 10^9)$, the compressed sequence. -----Output----- Output a single integer β€” the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. -----Examples----- Input 5 4 1 2 3 1 Output 5 Input 6 1 3 2 1 2 4 Output 6 Input 6 1 1 1 1 2 2 Output 7 -----Note----- In the first example a sequence (((()(()))( is described. This bracket sequence contains $5$ subsegments which form regular bracket sequences: Subsequence from the $3$rd to $10$th character: (()(())) Subsequence from the $4$th to $5$th character: () Subsequence from the $4$th to $9$th character: ()(()) Subsequence from the $6$th to $9$th character: (()) Subsequence from the $7$th to $8$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described.
import sys input = sys.stdin.readline INF = int(1000000000.0) + 7 dx = [-1, 0, 1, 0] dy = [0, 1, 0, -1] def solve(): n = int(input()) data = list(map(int, input().split())) remain = [] final = [0] * n dp = [0] * n for i in range(n): now = i % 2 if now == 0: remain.append((i, data[i])) else: k = data[i] while k and remain: idx, cost = remain.pop() if cost < k: dp[i] += cost k -= cost if idx > 0: dp[i] += final[idx - 1] else: if k == cost: if idx > 0: final[i] = final[idx - 1] + 1 dp[i] += final[idx - 1] else: final[i] = 1 else: final[i] = 1 dp[i] += k cost -= k k = 0 if cost: remain.append((idx, cost)) print(sum(dp)) t = 1 while t: t -= 1 solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR