description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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 inp = stdin.readline n = int(inp()) arr = [int(x) for x in inp().split()] count = 0 for i in range(0, n, 2): start = arr[i] rest = 0 for j in range(i + 1, n): if j % 2: if arr[j] < rest: rest -= arr[j] elif start + rest >= arr[j]: start -= arr[j] - rest count += arr[j] - rest + (1 if rest > 0 else 0) rest = 0 else: count += start + (1 if rest > 0 else 0) break else: rest += arr[j] print(count)
ASSIGN VAR VAR 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 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 VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER 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 def ri(): return [int(i) for i in input().split()] def common(amin, amax, bmin, bmax): if amax < bmin or bmax < amin: return 0 return min(amax, bmax) - max(amin, bmin) + 1 def solve(Aseg, Bseg, fminopen, faddsopen): if fminopen > Aseg: return 0 amin = max(1, fminopen) + faddsopen amax = Aseg + faddsopen return common(amin, amax, 1, Bseg) def main(): t = 1 for _ in range(t): n = ri()[0] a = ri() res = 0 for st_seg_idx in range(0, n, 2): f_min_open = 0 f_adds_open = 0 seg_result = 0 for end_seg_idx in range(st_seg_idx + 1, n): if end_seg_idx % 2 == 0: f_adds_open += a[end_seg_idx] else: seg_result += solve( a[st_seg_idx], a[end_seg_idx], f_min_open, f_adds_open ) f_adds_open -= a[end_seg_idx] if f_adds_open < 0: f_min_open = max(f_min_open, -f_adds_open) if f_min_open > a[st_seg_idx]: break res += seg_result print(res) main()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER 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 VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR 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.
from sys import stdin def solve(): n = int(stdin.readline().strip()) seq = list(map(int, stdin.readline().split())) ans = 0 for i in range(0, n, 2): curr = seq[i] inter_sum = 0 for j in range(i + 1, n): if j % 2 == 0: inter_sum += seq[j] else: inter_sum -= seq[j] if inter_sum <= 0 and j > i + 1: ans += 1 if inter_sum < 0: if curr + inter_sum >= 0: ans += -inter_sum curr += inter_sum inter_sum = 0 else: ans += curr break print(ans) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR 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 NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER 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())) c1, c2 = [0], [0] for i in range(1, n + 1): ci = c[i] if i % 2: c1.append(ci + c1[-1]) c2.append(c2[-1]) else: c2.append(ci + c2[-1]) c1.append(c1[-1]) dp = [([0] * (n + 1)) for _ in range(n + 1)] ans = 0 for x in range(1, n + 1, 2): for i in range(1, n + 1, 2): ci = c[i] if i + x > n: break j = i + x cj = c[j] if i == j - 1: m = min(ci, cj) ans += m dp[i][j] = 2 * m else: dp0 = dp[i + 2][j - 2] cnt1 = c1[j - 1] - c1[i] - dp0 // 2 cnt2 = c2[j - 1] - c2[i] - dp0 // 2 if cnt1 > cj or cnt2 > ci: m = 0 dp[i][j] = dp0 + 2 * (min(cnt1, cj) + min(cnt2, ci)) else: m = min(ci - cnt2, cj - cnt1) + 1 ans += m dp[i][j] = dp0 + 2 * (m + min(cnt1, cj) + min(cnt2, ci) - 1) 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 VAR LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR 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.
from itertools import accumulate n = int(input()) arr = [int(c) for c in input().split(" ")] arr_par = [(arr[i] if not i % 2 else -arr[i]) for i in range(len(arr))] acc = list(accumulate(arr_par)) ctr = 0 def d_inc(d, i): if i in d: d[i] += 1 else: d[i] = 1 def summ(i): return i * (i + 1) / 2 def corr(i): return summ(i + 1) - (i + 1) def handle(d, ths): ad = 0 for k in d: if ths < k: if d[k]: ad += corr(d[k]) d[k] = 0 return ad open_reg = 0 mn = 0 ths = 0 special = {} for i in range(n): ths += arr_par[i] if not i % 2: open_reg += arr[i] else: if arr[i] <= open_reg: ctr += arr[i] open_reg -= arr[i] else: ctr += open_reg open_reg = 0 corr_val = handle(special, ths) ctr += corr_val if ths < mn: mn = ths else: d_inc(special, ths) ad = 0 d = special for k in d: if d[k]: ad += corr(d[k] - 1) d[k] = 0 ctr += ad print(int(ctr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR 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()) L = list(map(int, input().split())) if len(L) % 2 != 0: L.pop() n = len(L) ret = 0 for i in range(n // 2): b = L[2 * i] curr = 0 low = 1 for j in range(2 * i, n): if j % 2 == 1: if curr - (b - (low - 1)) < L[j]: ret += min(L[j] - max(0, curr - (b - (low - 1))), b - (low - 1)) curr -= L[j] low = max(low, b - curr) if curr < 0: break else: curr += L[j] print(ret)
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 FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF 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()) arr = list(map(int, input().split())) count = 0 ans = 0 for i in range(n): count = 0 mini = 0 if i % 2 == 0: for j in range(i + 1, n, 1): if j % 2 == 0: count += arr[j] mini = min(mini, count) else: l_cont = arr[i] r_cont = abs(arr[j]) c_count = count if l_cont >= abs(mini): l_cont += mini c_count -= mini if r_cont >= c_count: r_cont -= c_count ans = ans + 0 if i + 1 == j else ans + 1 ans += min(r_cont, l_cont) count -= arr[j] mini = min(mini, count) 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 NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL 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.
import sys from sys import stdin n = int(stdin.readline()) c = list(map(int, stdin.readline().split())) h = [0] for i in range(n): if i % 2 == 0: h.append(h[-1] + c[i]) else: h.append(h[-1] - c[i]) ans = 0 for l in range(0, n, 2): if l == n - 1: break nmin = float("inf") la = h[l] lb = h[l + 1] for r in range(l + 1, n, 2): ra = h[r + 1] rb = h[r] a = max(la, ra) b = min(lb, rb, nmin + 1) now = max(0, b - a) ans += now nmin = min(nmin, ra) 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 LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP 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.
N = int(input()) C = list(map(int, input().split())) ans = 0 stack = [[0, 0]] now = pre = mn = 0 for i, c in enumerate(C): if i % 2 == 0: pre = now now += c else: pre = now now -= c ans += pre - max(now, mn) is_mn = False if mn > now: mn = now is_mn = True while stack and stack[-1][0] > now: _, v = stack.pop() ans += v if stack and stack[-1][0] == now: stack[-1][1] += 1 ans += stack[-1][1] - 1 elif is_mn: stack.append([now, 0]) else: stack.append([now, 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 NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN 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 NUMBER NUMBER NUMBER VAR BIN_OP 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.
n = int(input()) a = list(map(int, input().split())) s = [] answer = 0 for i in range(n): if i % 2 == 0: if len(s) == 0 or s[-1][0] != "(": s.append(["(", a[i]]) else: s[-1][1] += a[i] else: while len(s) > 0: if len(s) > 0 and s[-1][0] == "(": if s[-1][1] <= a[i]: a[i] -= s[-1][1] s[-1][0] = "()" else: s[-1][1] -= a[i] s.append(["()", a[i]]) a[i] = 0 if a[i] == 0: break accu = 0 while len(s) > 0 and s[-1][0] == "()": answer += s[-1][1] + accu s.pop() accu += 1 accu = 0 while len(s) > 0: if s[-1][0] == "()": answer += s[-1][1] + accu accu += 1 s.pop() else: accu = 0 s.pop() 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 FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR LIST STRING VAR VAR VAR NUMBER NUMBER VAR VAR WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER STRING IF VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER STRING VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST STRING VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL 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 doWork(): n = int(input()) a = map(int, input().split()) balance = 0 total = 0 stack = [] for i, x in enumerate(a): if i % 2 == 0: balance += x continue total += min(x, balance) balance = balance - x while stack and stack[-1][0] > balance: last = stack[-1] total += last[1] if last[0] == balance: last[1] += 1 break else: stack.pop() if stack and stack[-1][0] == balance: total += stack[-1][1] stack[-1][1] += 1 elif balance >= 0: stack.append([balance, 1]) balance = max(0, balance) print(total) doWork()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER 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 = list(map(int, input().split())) S = [0] for i in range(n): c = C[i] if i % 2 == 0: S.append(S[-1] + c) else: S.append(S[-1] - c) X = [] for s in S: X.append(s - 1) X.append(s) X.append(s + 1) X = sorted(set(X)) D = {X[i]: i for i in range(len(X))} Y = [[] for i in range(len(X))] now = 0 for i in range(n): c = C[i] if i % 2 == 0: for k in range(D[now], D[now + c]): Y[k].append(1) now += c else: for k in range(D[now], D[now - c], -1): Y[k].append(-1) now -= c if n % 2 == 0: Y[D[now]].append(1) else: Y[D[now]].append(-1) ANS = 0 for i in range(len(Y) - 1): count = 0 K = 0 for y in Y[i]: if y == 1: count += 1 else: K += count * (count + 1) // 2 count = 0 if count != 0: K += count * (count - 1) // 2 ANS += K * (X[i + 1] - X[i]) print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR 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.
import sys input = sys.stdin.readline def main(): t = 1 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) 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 = min(signs[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 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 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 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()) c = list(map(int, input().split())) c.insert(0, 0) ans = 0 for i in range(1, n + 1, 2): bal, lo = 0, 1 for j in range(i + 1, n + 1): if j % 2 == 0: L = lo R = max(1, bal + lo) ans += max(0, min(c[i] - L + 1, c[j] - R + 1)) bal -= c[j] else: bal += c[j] lo = max(lo, -bal) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR 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.
debug = False go_next = True while go_next: length = int(input()) values = list(map(int, input().split())) if debug: print(length, values) result = 0 for s_ix in range(0, length, 2): slackable = values[s_ix] unclosed = 0 for i in range(s_ix, length): if i % 2 == 0: unclosed += values[i] else: unclosed -= values[i] if unclosed <= slackable: if debug: print(s_ix, i, unclosed, slackable, end=" ") result += slackable - max(unclosed, 0) slackable = min(unclosed + 1, slackable) if debug: print(slackable, result) if unclosed < 0: break print(result) go_next = debug
ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR 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 IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR STRING VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN 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 = list(map(int, input().split())) ans = 0 for i in range(0, n, 2): st = [0, arr[i] - 1] curr = 0 for j in range(i + 1, n, 2): en = [curr + arr[j - 1] - arr[j], curr + arr[j - 1] - 1] curr += arr[j - 1] - arr[j] r1 = max(en[0], st[0]) r2 = min(en[1], st[1]) ans += max(r2 - r1 + 1, 0) if en[0] < 0: break st[1] = min(st[1], en[0]) 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 LIST NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER 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 inp(): return int(input()) def input_list(): return list(map(int, input().split())) def input_string(): s = input() return list(s[: len(s) - 1]) def input_int_gen(): return map(int, input().split()) tests = 1 for _ in range(tests): n = inp() a = input_list() stk = [[0, 0]] lefts = 0 ret = 0 for idx, x in enumerate(a): if idx % 2 == 0: lefts += x else: level = 0 valid = lefts if x <= lefts: level = lefts - x valid = x ret += valid while stk[-1][0] > level: ret += stk[-1][1] stk.pop() if stk[-1][0] == level: ret += stk[-1][1] stk[-1][1] += 1 else: stk.append([level, 1]) if x > lefts: stk = [[0, 0]] lefts = level print(ret)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER IF VAR VAR ASSIGN VAR LIST LIST NUMBER 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()) A = list(map(int, input().split())) if n % 2 == 1: A.pop() Ans = 0 B = [] for i in range(1, n, 2): x = A[i] - A[i - 1] z = 0 Ans += min(A[i], A[i - 1]) if x >= 0: for j in range(i - 2, -1, -2): z += A[j] w = A[j - 1] W = min(z, w) z -= W w -= W if W != 0 and z == 0: Ans += 1 Ans += min(w, x) x -= w if x < 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 NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL 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.
n = int(input()) a = [int(i) for i in input().split()] less = 0 zhan = [[0, 1]] high = 0 ans = 0 flag = True for i in a: if flag: high += i else: ans += min(high - less, i) high -= i less = min(high, less) while len(zhan) != 0 and zhan[-1][0] > high: ans += zhan.pop()[1] if len(zhan) == 0: ans -= 1 if len(zhan) == 0 or zhan[-1][0] < high: zhan.append([high, 1]) else: ans += zhan[-1][1] zhan[-1][1] += 1 if len(zhan) == 1: ans -= 1 flag = not flag 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 ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER 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.
import sys def main(N, A): ans = 0 for i in range(0, N, 2): op = 0, A[i] bottom = A[i] e = A[i] for j in range(i + 1, N, 2): cl = e - A[j], e mx = min(op[1], cl[1], max(0, bottom)) mn = max(op[0], cl[0], -min(0, bottom)) now = mx - mn if now >= 0: if j > i + 1: now += 1 ans += now bottom = min(bottom, e - A[j]) e -= A[j] if j < N - 1: e += A[j + 1] print(ans) input = sys.stdin.readline N = int(input()) (*A,) = map(int, input().split()) main(N, A)
IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR 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())) n = len(c) ans = 0 for i in range(0, n, 2): diff = 0 a = 0 for j in range(i + 1, n): if j % 2 == 0: diff += c[j] continue ans += min(max(0, c[j] - diff), c[i]) if c[j] - diff >= 0: ans += a diff -= c[j] if c[i] + diff < 0: break if diff < 0: c[i] += diff a = 1 diff = 0 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 FUNC_CALL VAR 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 NUMBER VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN 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 overlap(min1, max1, min2, max2): if max1 < min2 or max2 < min1: return 0 return max(0, min(max1, max2) - max(min1, min2)) + 1 n = int(input()) arr = list(map(int, input().split())) ans = 0 li = [] for i in range(0, n, 2): temp = 0 a = 0 b = arr[i] - 1 temp += arr[i] flag = 0 ans = 0 for j in range(i + 1, n): c, d = 0, 0 if j % 2 == 0: temp += arr[j] else: c = temp - 1 d = temp - arr[j] temp -= arr[j] ans += overlap(a, b, d, c) b = min(b, temp) if temp < 0: break li.append(ans) print(sum(li))
FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by p_{i} people for t_{i} days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it. For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd. In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. -----Input----- The first line contains integer n β€” the number of Olympiads in 2013 (1 ≀ n ≀ 100). Each of the following n lines contains four integers m_{i}, d_{i}, p_{i} and t_{i} β€” the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≀ m_{i} ≀ 12, d_{i} β‰₯ 1, 1 ≀ p_{i}, t_{i} ≀ 100), d_{i} doesn't exceed the number of days in month m_{i}. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. -----Output----- Print a single number β€” the minimum jury size. -----Examples----- Input 2 5 23 1 2 3 13 2 3 Output 2 Input 3 12 9 2 1 12 8 1 3 12 8 2 2 Output 3 Input 1 1 10 1 13 Output 1
fin = open("input.txt", "r") fout = open("output.txt", "w") mon = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] ans = [0] * 500 ba = [] for i in range(int(fin.readline())): m, d, p, t = map(int, fin.readline().split()) ba.append([m, d, p, t]) for i in ba: days = sum(mon[: i[0]]) + i[1] + 100 for j in range(1, i[3] + 1): ans[days - j] += i[2] print(max(ans), file=fout)
ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by p_{i} people for t_{i} days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it. For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd. In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. -----Input----- The first line contains integer n β€” the number of Olympiads in 2013 (1 ≀ n ≀ 100). Each of the following n lines contains four integers m_{i}, d_{i}, p_{i} and t_{i} β€” the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≀ m_{i} ≀ 12, d_{i} β‰₯ 1, 1 ≀ p_{i}, t_{i} ≀ 100), d_{i} doesn't exceed the number of days in month m_{i}. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. -----Output----- Print a single number β€” the minimum jury size. -----Examples----- Input 2 5 23 1 2 3 13 2 3 Output 2 Input 3 12 9 2 1 12 8 1 3 12 8 2 2 Output 3 Input 1 1 10 1 13 Output 1
w, r = open("output.txt", "w"), open("input.txt", "r") s, y = [0] * 466, [0, 100, 131, 159, 190, 220, 251, 281, 312, 343, 373, 404, 434] for i in range(int(r.readline())): m, d, p, t = map(int, r.readline().split()) x = y[m] + d s[x] -= p s[x - t] += p for i in range(465): s[i + 1] += s[i] w.write(str(max(s)))
ASSIGN VAR VAR FUNC_CALL VAR STRING STRING FUNC_CALL VAR STRING STRING ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by p_{i} people for t_{i} days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it. For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd. In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. -----Input----- The first line contains integer n β€” the number of Olympiads in 2013 (1 ≀ n ≀ 100). Each of the following n lines contains four integers m_{i}, d_{i}, p_{i} and t_{i} β€” the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≀ m_{i} ≀ 12, d_{i} β‰₯ 1, 1 ≀ p_{i}, t_{i} ≀ 100), d_{i} doesn't exceed the number of days in month m_{i}. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. -----Output----- Print a single number β€” the minimum jury size. -----Examples----- Input 2 5 23 1 2 3 13 2 3 Output 2 Input 3 12 9 2 1 12 8 1 3 12 8 2 2 Output 3 Input 1 1 10 1 13 Output 1
def main(): days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] month2day = {} cnt = 0 for i in range(9, 12 + 1): for j in range(1, days_in_month[i - 1] + 1): month2day[i, j, 12] = cnt cnt += 1 for i in range(1, 12 + 1): for j in range(1, days_in_month[i - 1] + 1): month2day[i, j, 13] = cnt cnt += 1 with open("input.txt") as f: test = f.readlines() n = int(test[0].strip()) days = [0] * len(month2day) for i in range(1, n + 1): m, d, p, t = map(int, test[i].strip().split()) R = month2day[m, d, 13] - 1 L = R - t + 1 days[L] += p days[R + 1] -= p acc = [days[0]] for i in range(1, len(days)): acc.append(acc[-1] + days[i]) with open("output.txt", "w") as f: f.write(str(max(acc))) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR STRING STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by p_{i} people for t_{i} days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it. For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd. In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. -----Input----- The first line contains integer n β€” the number of Olympiads in 2013 (1 ≀ n ≀ 100). Each of the following n lines contains four integers m_{i}, d_{i}, p_{i} and t_{i} β€” the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≀ m_{i} ≀ 12, d_{i} β‰₯ 1, 1 ≀ p_{i}, t_{i} ≀ 100), d_{i} doesn't exceed the number of days in month m_{i}. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. -----Output----- Print a single number β€” the minimum jury size. -----Examples----- Input 2 5 23 1 2 3 13 2 3 Output 2 Input 3 12 9 2 1 12 8 1 3 12 8 2 2 Output 3 Input 1 1 10 1 13 Output 1
import sys def getMaxJuries(juries, m, d, t, months): year = 1 res = 0 while t > 0: if d < 0: m -= 1 if m < 0: m += 12 year = 0 d = months[m] - 1 res = max(res, juries[year][m][d]) d -= 1 t -= 1 return res def setJury(juries, m, d, t, months, p): year = 1 while t > 0: if d < 0: m -= 1 if m < 0: m += 12 year = 0 d = months[m] - 1 juries[year][m][d] += p d -= 1 t -= 1 def __starting_point(): sys.stdin = open("input.txt", "r") sys.stdout = open("output.txt", "w") months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] n = int(input()) juriesNumber = 0 juries = [[[(0) for i in range(31)] for j in range(12)] for _ in range(2)] for i in range(n): m, d, p, t = [int(x) for x in input().split()] m -= 1 d -= 2 maxJuries = getMaxJuries(juries, m, d, t, months) if maxJuries + p > juriesNumber: juriesNumber += maxJuries + p - juriesNumber setJury(juries, m, d, t, months, p) print(juriesNumber) __starting_point()
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by p_{i} people for t_{i} days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it. For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd. In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. -----Input----- The first line contains integer n β€” the number of Olympiads in 2013 (1 ≀ n ≀ 100). Each of the following n lines contains four integers m_{i}, d_{i}, p_{i} and t_{i} β€” the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≀ m_{i} ≀ 12, d_{i} β‰₯ 1, 1 ≀ p_{i}, t_{i} ≀ 100), d_{i} doesn't exceed the number of days in month m_{i}. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. -----Output----- Print a single number β€” the minimum jury size. -----Examples----- Input 2 5 23 1 2 3 13 2 3 Output 2 Input 3 12 9 2 1 12 8 1 3 12 8 2 2 Output 3 Input 1 1 10 1 13 Output 1
fin = open("input.txt", "r") fout = open("output.txt", "w") day = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] table = [] for i in range(int(fin.readline())): table.append([int(i) for i in fin.readline().split()]) ans = [0] * 500 for it in table: days = sum(day[0 : it[0]]) + it[1] + 100 for i in range(1, it[3] + 1): ans[days - i] += it[2] print(max(ans), file=fout)
ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by p_{i} people for t_{i} days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it. For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd. In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. -----Input----- The first line contains integer n β€” the number of Olympiads in 2013 (1 ≀ n ≀ 100). Each of the following n lines contains four integers m_{i}, d_{i}, p_{i} and t_{i} β€” the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≀ m_{i} ≀ 12, d_{i} β‰₯ 1, 1 ≀ p_{i}, t_{i} ≀ 100), d_{i} doesn't exceed the number of days in month m_{i}. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. -----Output----- Print a single number β€” the minimum jury size. -----Examples----- Input 2 5 23 1 2 3 13 2 3 Output 2 Input 3 12 9 2 1 12 8 1 3 12 8 2 2 Output 3 Input 1 1 10 1 13 Output 1
from sys import stdin, stdout def get_time(m, d, t): month = ["dum", 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] day = 0 for i in range(1, m): day += month[i] day += d end = day start = end - t return start, end stdin = open("input.txt", "r") stdout = open("output.txt", "w") n = int(stdin.readline().strip()) time_line = [] for _ in range(n): m, d, p, t = list(map(int, stdin.readline().split())) start, end = get_time(m, d, t) time_line.append([start, p]) time_line.append([end, -p]) time_line = sorted(time_line, key=lambda x: (x[0], x[1])) ans = 0 temp = 0 for i in range(len(time_line)): temp += time_line[i][1] ans = max(ans, temp) stdout.write(str(ans))
FUNC_DEF ASSIGN VAR LIST STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by p_{i} people for t_{i} days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it. For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd. In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. -----Input----- The first line contains integer n β€” the number of Olympiads in 2013 (1 ≀ n ≀ 100). Each of the following n lines contains four integers m_{i}, d_{i}, p_{i} and t_{i} β€” the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≀ m_{i} ≀ 12, d_{i} β‰₯ 1, 1 ≀ p_{i}, t_{i} ≀ 100), d_{i} doesn't exceed the number of days in month m_{i}. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. -----Output----- Print a single number β€” the minimum jury size. -----Examples----- Input 2 5 23 1 2 3 13 2 3 Output 2 Input 3 12 9 2 1 12 8 1 3 12 8 2 2 Output 3 Input 1 1 10 1 13 Output 1
import sys sys.stdin = open("input.txt", "r") sys.stdout = open("output.txt", "w") di = {} k = 1 days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] for i in range(12): for j in range(days[i]): di[i + 1, j + 1] = k k += 1 n = int(input()) a = [0] * 512 for _ in range(n): m, d, p, t = map(int, input().split()) temp = di[m, d] for x in range(temp - t, temp): a[x] += p print(max(a))
IMPORT ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by p_{i} people for t_{i} days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it. For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd. In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. -----Input----- The first line contains integer n β€” the number of Olympiads in 2013 (1 ≀ n ≀ 100). Each of the following n lines contains four integers m_{i}, d_{i}, p_{i} and t_{i} β€” the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≀ m_{i} ≀ 12, d_{i} β‰₯ 1, 1 ≀ p_{i}, t_{i} ≀ 100), d_{i} doesn't exceed the number of days in month m_{i}. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. -----Output----- Print a single number β€” the minimum jury size. -----Examples----- Input 2 5 23 1 2 3 13 2 3 Output 2 Input 3 12 9 2 1 12 8 1 3 12 8 2 2 Output 3 Input 1 1 10 1 13 Output 1
w = open("output.txt", "w") r = open("input.txt", "r") arr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] def get_date(m, d): res = 0 for i in range(m - 1): res += arr[i] return res + d n = int(r.readline()) used = [0] * 366 for i in range(n): m, d, p, t = list(map(int, r.readline().split())) date = get_date(m, d) for i in range(max(0, date - t), date): used[i] += p w.write(str(max(used)))
ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
import sys input = sys.stdin.readline n, r = map(int, input().split()) xs = list(map(int, input().split())) points = [] for x in xs: y = r for x0, y0 in points: if abs(x - x0) <= 2 * r: y = max(y, y0 + ((2 * r) ** 2 - (x - x0) ** 2) ** 0.5) points.append((x, y)) print(" ".join("%.20f" % p[1] for p in points))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING VAR NUMBER VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
import sys input = sys.stdin.readline MOD = 1000000007 MOD2 = 998244353 ii = lambda: int(input().strip("\n")) si = lambda: input().strip("\n") dgl = lambda: list(map(int, input().strip("\n"))) f = lambda: map(int, input().strip("\n").split()) il = lambda: list(map(int, input().strip("\n").split())) ls = lambda: list(input().strip("\n")) let = "abcdefghijklmnopqrstuvwxyz" n, r = f() l = il() ans = [r] for i in range(1, n): mx = r for j in range(i): if abs(l[j] - l[i]) <= 2 * r: mx = max(mx, ans[j] + (4 * r * r - (l[i] - l[j]) ** 2) ** 0.5) ans.append(mx) print(*ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR 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 FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) a = list(map(int, input().split())) st = [] for i in a: left, right, cen = i - r, i + r, -1 for j in st: if ( not (j[0] > right or j[1] < left) and cen < ((2 * r) ** 2 - ((j[0] + j[1]) / 2 - i) ** 2) ** 0.5 + j[2] ): cen = ((2 * r) ** 2 - ((j[0] + j[1]) / 2 - i) ** 2) ** 0.5 + j[2] k = j if cen == -1: st.append((left, right, r)) c = r else: c = cen st.append((left, right, cen)) print(c, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
ip = lambda: map(int, input().split()) ipl = lambda: list(map(int, input().split())) n, r = ip() a = ipl() l = [] for i in range(n): b = [r] for j in range(i): d = abs(a[i] - a[j]) if d <= 2 * r: b.append((4 * r * r - d * d) ** 0.5 + l[j]) l.append(max(b)) print(max(b), end=" ")
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) l = list(map(int, input().split())) ds = [] for i in range(len(l)): ans = [l[i], r] x = l[i] for j in range(len(ds)): if abs(x - ds[j][0]) <= 2 * r: ans[1] = max(ans[1], ds[j][1] + (4 * r * r - (x - ds[j][0]) ** 2) ** 0.5) ds.append(ans) ls = [] for j in range(n): ls.append(ds[j][1]) print(*ls)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
import sys n, r = map(int, sys.stdin.readline().split()) x = list(map(int, sys.stdin.readline().split())) y = [r] for i in range(1, n): m = r for j in range(i): temp = 4 * r * r - (x[i] - x[j]) ** 2 if temp >= 0: m = max(m, temp**0.5 + y[j]) y.append(m) for i in range(n): print(y[i], end=" ") print()
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
num_disks, radius = list(map(int, input().strip().split())) x_locs = list(map(int, input().strip().split())) locs = [] for x_loc in x_locs: max_y = radius for x, y in locs: base = (radius * 2) ** 2 - (x_loc - x) ** 2 if base < 0: continue new_y = base**0.5 + y if new_y > max_y: max_y = new_y locs.append((x_loc, max_y)) print(max_y)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = list(map(int, input().split())) X = [int(x) for x in input().split()] Y = [] for i in range(n): forcedy = r for j in range(len(Y)): dist = abs(X[i] - X[j]) if dist <= 2 * r: y = Y[j] + ((2 * r) ** 2 - dist**2) ** 0.5 forcedy = max(y, forcedy) Y.append(forcedy) print(*Y)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) x_coord = list(map(int, input().split())) d = {} for i in x_coord: final = r for j in range(i - r, i + r + 1): check = d.get(j, [-1, -1]) if check[0] > 0: potential = check[1] + (4 * r * r - (i - check[0]) ** 2) ** 0.5 final = max(potential, final) for j in range(i - r, i + r + 1): d[j] = i, final print(final, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
def f(x1, x2, y2): t = 4 * r * r - (x1 - x2) ** 2 if t >= 0: return y2 + t**0.5 return r n, r = map(int, input().split()) print(r) (*a,) = map(int, input().split()) cor = [[a[i], r] for i in range(n)] for i in range(1, n): cor[i][1] = max(f(cor[i][0], cor[j][0], cor[j][1]) for j in range(i)) print(cor[i][1])
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = list(map(int, input().split())) p = 1 points = list(map(int, input().split())) x, y = [(0) for i in range(n)], [(0) for i in range(n)] for i in range(n): x[i] = points[i] y[i] = r for j in range(i): if abs(x[j] - x[i]) <= 2 * r: ans = 4 * r**2 d = abs(x[j] - x[i]) d = d**2 ans = ans - d ans = ans**0.5 y[i] = max(y[i], y[j] + ans) print(*y)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) x = list(map(int, input().split())) y = [r] * n for i in range(n): for j in range(i): if not abs(x[i] - x[j]) > 2 * r: y[i] = max(y[i], (4 * r**2 - (x[i] - x[j]) ** 2) ** 0.5 + y[j]) for i in y: print(i, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
read = lambda: map(int, input().split()) n, r = read() x = list(read()) y = [0] * n for i in range(n): cur = r for j in range(i): if 2 * r >= abs(x[i] - x[j]): cur = max(cur, y[j] + (4 * r * r - (x[i] - x[j]) ** 2) ** 0.5) y[i] = cur print(*y)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) x = list(map(int, input().split())) y = [] for i in range(n): s = [] for j in range(i): if abs(x[i] - x[j]) < 2 * r + 1e-07: s.append(j) max = r for j in s: if (4 * r * r - (x[i] - x[j]) ** 2) ** 0.5 + y[j] > max: max = (4 * r * r - (x[i] - x[j]) ** 2) ** 0.5 + y[j] y.append(max) for i in y: print(i, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) x = [int(i) for i in input().split()] c = [] for i in range(n): k = r for x1, j in c: d = abs(x[i] - x1) if d <= 2 * r: k = max(k, j + (4 * r**2 - d * d) ** 0.5) c.append([x[i], k]) print(k)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
import sys input = sys.stdin.readline def gcd(a, b): if a == 0: return b return gcd(b % a, a) def lcm(a, b): return a * b / gcd(a, b) def main(): n, r = map(int, input().split()) a = list(map(int, input().split())) ans = [] ans.append(r) for i in range(1, n): ymax = r for j in range(i): if abs(a[j] - a[i]) <= 2 * r: ymax = max(ymax, ans[j] + (4 * r * r - (a[i] - a[j]) ** 2) ** 0.5) ans.append(ymax) print(*ans) return main()
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) y = [] x = list(map(int, input().split())) for xi in x: yi = r for tx, ty in zip(x, y): if xi - 2 * r <= tx <= xi + 2 * r: dy = (4.0 * r**2 - (tx - xi) ** 2) ** 0.5 yi = max(yi, ty + dy) y.append(yi) print(*y)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
l = list(map(int, input().rstrip().split())) n = l[0] r = l[1] num = list(map(int, input().rstrip().split())) final = [] mahalist = [] for i in range(len(num)): mahalist = [] if i == 0: final.append(r) mahalist.append(r) else: for j in range(i): if abs(num[i] - num[j]) <= 2 * r: mahalist.append( ((2 * r) ** 2 - (num[i] - num[j]) ** 2) ** 0.5 + final[j] ) else: mahalist.append(r) final.append(max(mahalist)) for i in final: print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
a = list(map(int, input().split(" "))) y = list(map(int, input().split(" "))) circ = [] for i in range(a[0]): fy = a[1] for j in circ: delta = abs(y[i] - j[0]) if delta <= 2 * a[1]: fy = max(fy, j[1] + ((2 * a[1]) ** 2 - delta**2) ** 0.5) circ.append([y[i], fy]) for i in circ: print(i[1], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = list(map(int, input().split())) disks = list(map(int, input().split())) new_disks = [] for i in range(n): y2 = r x2 = disks[i] for j in range(i): x1, y1 = new_disks[j] a = r * r * 4 - (x2 - x1) ** 2 if a < 0: continue else: y2 = max(y2, a**0.5 + y1) new_disks.append([x2, y2]) for i in new_disks: print(i[1], end=" ")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = list(map(int, input().split())) x = list(map(int, input().split())) r2r4 = 4 * r**2 y = [] for i in range(0, n): max_y = r for j in range(i): if r2r4 >= (x[i] - x[j]) ** 2: max_y = max(max_y, y[j] + (r2r4 - (x[i] - x[j]) ** 2) ** 0.5) y.append(max_y) print(*y)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
import itertools inf = float("inf") read_ints = lambda: list(map(int, input().split())) def intersects(x1, y1, x2, y2, r): return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 <= 2 * r def get_intersection_y(x, xt, yt, r): if abs(x - xt) > 2 * r: return r return yt + (4 * r**2 - (x - xt) ** 2) ** 0.5 n, r = read_ints() r = float(r) xs = list(map(float, input().split())) ys = [r] * n for i in range(1, n): ys[i] = max( [get_intersection_y(xs[i], xt, yt, r) for xt, yt in zip(xs[:i], ys[:i])] ) print(" ".join(map(str, ys)))
IMPORT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR FUNC_DEF IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN VAR RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = [int(i) for i in input().split(" ")] x = [int(i) for i in input().split(" ")] y = [] for i in range(n): contact = -1 cury = r for j in range(i): if x[i] - r <= x[j] + r and x[i] + r >= x[j] - r: if contact == -1: contact = j cury = y[j] + (4 * r**2 - (x[i] - x[j]) ** 2) ** 0.5 else: tempy = y[j] + (4 * r**2 - (x[i] - x[j]) ** 2) ** 0.5 if tempy > cury: contact = j cury = tempy y.append(cury) for i in range(n - 1): print(y[i], end=" ") print(y[n - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) xs = list(map(float, input().split())) ys = [0.0] * n y = 0 for i in range(n): maxy = r for j in range(i): if abs(xs[j] - xs[i]) <= 2.0 * r: dx = xs[j] - xs[i] newy = (4 * r * r - dx * dx) ** 0.5 + ys[j] if maxy == -1 or maxy < newy: maxy = newy ys[i] = maxy output = " ".join(map(str, ys)) print(output)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) a = [int(i) for i in input().split()] s = [] arr = [] s.append(r) arr.append(a[0]) for i in range(1, n): an = r f = False temp = -1 for j in range(len(arr)): if arr[j] <= a[i] + 2 * r and arr[j] >= a[i] - 2 * r: an = max(an, s[j] + (pow(2 * r, 2) - pow(a[i] - arr[j], 2)) ** 0.5) f = True s.append(an) arr.append(a[i]) for j in range(len(s)): print(s[j], end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) l = list(map(int, input().split())) ans = [r] * n for i in range(n): for j in range(i): if 4 * r**2 - (max(l[i], l[j]) - min(l[i], l[j])) ** 2 >= 0: y = 4 * r**2 - (max(l[i], l[j]) - min(l[i], l[j])) ** 2 ans[i] = max(ans[i], ans[j] + y**0.5) print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
R = lambda: map(int, input().split()) n, r = R() xs = list(R()) ys = [] for i in range(n): ys.append( max( [ (((2 * r) ** 2 - abs(xs[i] - xs[j]) ** 2) ** 0.5 + ys[j]) for j in range(i) if abs(xs[i] - xs[j]) <= 2 * r ], default=r, ) ) print(*ys)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = list(map(int, input().strip().split())) x = list(map(int, input().strip().split())) fallen = [[x[0], r]] for i in range(1, len(x)): k = len(fallen) - 1 maks = r for k in range(len(fallen)): xi, yi = fallen[k] if abs(x[i] - xi) <= 2 * r + 10**-10: y1 = ((2 * r) ** 2 - (xi - x[i]) ** 2) ** 0.5 maks = max(maks, yi + y1) fallen.append((x[i], maks)) cent = [e for a, e in fallen] print(" ".join(map(str, cent)))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) x = list(map(int, input().split())) y = [0] * n y[0] = r for i in range(1, n): temp = r for j in range(i): s = 4 * r * r - abs(x[j] - x[i]) * abs(x[j] - x[i]) if s >= 0: temp = max(temp, s**0.5 + y[j]) y[i] = temp for i in range(n): y[i] = str(y[i]) print(" ".join(y))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) discs = {} s = list(map(int, input().split())) discs[s[0]] = r print(r, end=" ") b = lambda y: (4 * r**2 - y**2) ** 0.5 for i in range(1, n): xi = s[i] found = False to_app = [] for x in discs: res = b(x - xi) if type(res) != complex: to_app.append(res + discs[x]) found = True if found: prin = max(to_app) discs[xi] = prin print(prin, end=" ") else: discs[xi] = r print(r, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) s = list(map(int, input().split())) l = [r] for i in range(1, len(s)): b = r for j in range(i - 1, -1, -1): if abs(s[i] - s[j]) <= 2 * r: b = max(b, l[j] + (4 * r * r - (s[i] - s[j]) ** 2) ** 0.5) l.append(b) print(*l)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
maxN = int(100000.0 + 3) eps = 1e-10 x = [(0) for k in range(maxN)] y = [(0) for k in range(maxN)] n = 0 r = 0 d = 0 def read(): global x, n, r, d n, r = (int(k) for k in input().split()) r = float(r) d = 2 * r x = [float(k) for k in input().split()] def doit(): for i in range(n): this_y = r for j in range(i): dx = abs(x[i] - x[j]) if dx < d + eps: new_y = y[j] + (d**2 - dx**2) ** 0.5 if new_y > this_y: this_y = new_y y[i] = this_y def main(): read() doit() print(*y[:n]) main()
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
f = lambda: map(int, input().split()) n, r = f() p, d = [], 2 * r for x in f(): y = r for a, b in p: if abs(a - x) <= d: y = max(y, b + (d * d - (a - x) ** 2) ** 0.5) p.append((x, y)) for x, y in p: print(y)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR LIST BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) x = [int(i) for i in input().split()] ans_y = [r] for i in range(1, n): curr_x = x[i] stat = True pos = [] index_of_check = 0 for j in range(0, i): if x[j] - 2 * r <= curr_x <= x[j] + 2 * r: pos.append(j) stat = False if stat: ans_y.append(r) continue else: y_max = 0 for j in pos: x_c = x[j] y_c = ans_y[j] delta_x = (curr_x - x_c) ** 2 delta_y = (4 * r**2 - delta_x) ** 0.5 new_y = delta_y + y_c y_max = max(new_y, y_max) ans_y.append(y_max) for i in ans_y: print(i, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) x, y = list(map(int, input().split())), [r] * n def f(xi, xj, yj): d = abs(xi - xj) return r if d > 2 * r else yj + (4 * r**2 - d**2) ** 0.5 for i in range(1, n): y[i] = max(f(x[i], x[j], y[j]) for j in range(i)) print(" ".join(map(str, y)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP LIST VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = list(map(int, input().split())) disks = list(map(int, input().split())) atline = [[disks[0], r]] for i in range(1, n): sup = r thisd = disks[i] for disk in atline: dx = disk[0] - thisd if abs(dx) > 2 * r: continue y = disk[1] + (4 * r * r - dx * dx) ** 0.5 sup = max(sup, y) atline += [[thisd, sup]] s = "" for d in atline: s += str(d[1]) + " " print(s)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR LIST LIST VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
import time n, r = 0, 0 x = [] y = [] def calc_y(x, x1, y1): dx = abs(x - x1) if dx > 2 * r: return r dy = (4 * r**2 - dx**2) ** (1 / 2) return y1 + dy def let_fall_disk(i): global n, r, x, y return max([calc_y(x[i], x[j], y[j]) for j in range(i)] + [r]) def let_fall_disks(): global n, r, x, y for i in range(n): y += [let_fall_disk(i)] def get_values(): global n, r, x, y n, r = (int(a) for a in input().split()) x = [int(a) for a in input().split()] def main(): get_values() if len(x) != n: return let_fall_disks() print(" ".join([str(e) for e in y])) main()
IMPORT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER RETURN BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR LIST VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) a = list(map(int, input().split())) ans = [r] for i in range(1, n): maxx = -1 for j in range(i): if abs(a[i] - a[j]) <= 2 * r: c = ans[j] + (4 * r * r - (a[i] - a[j]) ** 2) ** 0.5 else: c = r maxx = max(maxx, c) ans.append(maxx) print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
n, r = map(int, input().split()) A = list(map(int, input().split())) B = [] B.append([r, A[0] - r, A[0] + r]) print(r, end=" ") for i in range(1, n): B = sorted(B, key=lambda l: l[0], reverse=True) m = 0 k = 0 for j in B: if j[1] - r <= A[i] <= j[2] + r: k = j[0] + (4 * r * r - abs(A[i] - j[1] - r) ** 2) ** 0.5 if k > m: m = k if m == 0: B.append([r, A[i] - r, A[i] + r]) print(round(r, 11), end=" ") else: B.append([round(m, 11), A[i] - r, A[i] + r]) if m % 1 == 0: print(int(m), end=" ") else: print(round(m, 11), end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
I = lambda: input().split(" ") a = I() y = I() c = [] for i in range(int(a[0])): f = int(a[1]) g = 2 * f h = int(y[i]) for j in c: d = abs(h - j[0]) if d <= g: f = max(f, j[1] + (g * g - d * d) ** 0.5) c.append([h, f]) for i in c: print(i[1])
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
k, r = map(int, input().split()) S = list(map(int, input().split())) Used = [] Answer = [] for i in S: delta = r for j in range(len(Used)): if abs(i - Used[j]) <= 2 * r: rass = abs(i - Used[j]) delta = max(delta, Answer[j] + ((2 * r) ** 2 - rass**2) ** 0.5) Answer.append(delta) Used.append(i) print(" ".join(map(str, Answer)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≀ n, r ≀ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≀ x_{i} ≀ 1 000)Β β€” the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk.
R = lambda: map(int, input().split()) R1 = lambda: map(float, input().split()) n, r = R() a = list(R1()) pos = [] t = 4.0 * r * r for i in range(n): cons = [r] for k in range(i): t1 = t - (a[k] - a[i]) ** 2 if t1 >= 0: t2 = t1 ** (1 / 2) cons.append(pos[k] + t2) pos.append(max(cons)) print(*pos, sep=" ")
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
class Solution: def numsSameConsecDiff(self, n: int, k: int) -> List[int]: result = {1, 2, 3, 4, 5, 6, 7, 8, 9} for i in range(1, n): temp = list() for r in result: if k == 0: temp.append(r * 10 + r % 10) else: back = r % 10 if back - k >= 0: temp.append(r * 10 + back - k) if back + k < 10: temp.append(r * 10 + back + k) result = temp.copy() return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR VAR VAR
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
class Solution: def numsSameConsecDiff(self, N: int, K: int) -> List[int]: def dfs(N, K, digit, rst): if not N: rst.append(int("".join(map(str, digit)))) return if digit[-1] + K <= 9: dfs(N - 1, K, digit + [digit[-1] + K], rst) if digit[-1] - K >= 0: dfs(N - 1, K, digit + [digit[-1] - K], rst) if N == 1: return list(range(10)) if K == 0: return [int(str(i) * N) for i in range(1, 10)] rst = [] for i in range(1, 10): dfs(N - 1, K, [i], rst) return rst
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN IF BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR LIST BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR LIST BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR LIST VAR VAR RETURN VAR VAR VAR
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
class Solution: def numsSameConsecDiff(self, n, k): res = [] for i in range(1, 10): neighbors = self.helper(i, k) for neighbor in neighbors: res.append([i, neighbor]) if n > 2: n -= 2 while n > 0: temp = [] for sublist in res: neighbors = self.helper(sublist[-1], k) for neighbor in neighbors: temp.append(sublist + [neighbor]) n -= 1 res = temp for i in range(len(res)): res[i] = int("".join(str(ele) for ele in res[i])) return res def helper(self, val, k): res = set() if val + k <= 9: res.add(val + k) if val - k >= 0: res.add(val - k) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
class Solution: def numsSameConsecDiff(self, n: int, k: int) -> List[int]: if n == 1: return list(range(10)) ans = [] for num in range(1, 10): self.dfs(n - 1, num, ans, k) return ans def dfs(self, n, num, ans, k): if n == 0: return ans.append(num) last_digit = num % 10 next_ds = set([last_digit + k, last_digit - k]) for digit in next_ds: if 0 <= digit < 10: tmp = num * 10 + digit self.dfs(n - 1, tmp, ans, k)
CLASS_DEF FUNC_DEF VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR VAR IF NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
class Solution: def numsSameConsecDiff(self, n: int, k: int) -> List[int]: queue = [] ans = [] for i in range(1, 10): queue.append(str(i)) while len(queue) > 0: num = queue.pop(0) if len(num) == n and num not in ans: ans.append(num) elif len(num) < n: x = int(num[-1]) if x - k >= 0: queue.append(num + str(x - k)) if x + k <= 9: queue.append(num + str(x + k)) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR VAR
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
import itertools class Solution: def step(self, n, k): d = n % 10 b = n * 10 if k == 0: yield b + d if d < d + k < 10: yield b + d + k if 0 <= d - k < d: yield b + d - k def solve(self, nums, n, k): if n == 1: return nums soln = [self.step(x, k) for x in nums] return itertools.chain.from_iterable([self.solve(x, n - 1, k) for x in soln]) def numsSameConsecDiff(self, n: int, k: int) -> List[int]: seed = [x for x in range(1, 10) if x + k < 10 or 0 <= x - k < 10] return list(self.solve(seed, n, k))
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR NUMBER EXPR BIN_OP BIN_OP VAR VAR VAR IF NUMBER BIN_OP VAR VAR VAR EXPR BIN_OP BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
class Solution: def numsSameConsecDiff(self, n: int, k: int) -> List[int]: def solve(x): s = str(x) if len(s) > 1 and abs(int(s[-1]) - int(s[-2])) != k: return if len(s) == n and abs(int(s[-1]) - int(s[-2])) == k: result.append(x) if len(s) == n: return for i in range(0, 10): solve(x * 10 + i) result = [] for i in range(1, 10): solve(i) return result
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
class Solution: def numsSameConsecDiff(self, N: int, K: int) -> List[int]: dp = list(range(10)) for _ in range(N - 1): _dp = set() for x in dp: for y in [x % 10 + K, x % 10 - K]: if x and 0 <= y <= 9: _dp.add(x * 10 + y) dp = _dp return list(dp)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR LIST BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
class Solution: def numsSameConsecDiff(self, n: int, k: int) -> List[int]: @functools.lru_cache(maxsize=128, typed=False) def check(n, k, s): rtv = [] if n == 0: return [] rtv = [] for i in range(10): if abs(i - s) == k: r1 = check(n - 1, k, i) if r1: for r in r1: rtv.append([i] + r) else: rtv.append([i]) return rtv rtv = [] for i in range(1, 10): r0 = check(n - 1, k, i) for r in r0: t = i for c in r: t = t * 10 + c rtv.append(t) return rtv
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER RETURN LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR RETURN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
class Solution: def numsSameConsecDiff(self, n: int, k: int) -> List[int]: if n == 1: return [i for i in range(10)] queue = [digit for digit in range(1, 10)] for level in range(n - 1): next_queue = [] for num in queue: tail_digit = num % 10 next_digits = set([tail_digit + k, tail_digit - k]) for next_digit in next_digits: if 0 <= next_digit < 10: new_num = num * 10 + next_digit next_queue.append(new_num) queue = next_queue return queue
CLASS_DEF FUNC_DEF VAR VAR IF VAR NUMBER RETURN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR VAR IF NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid. You may return the answer in any order. Β  Example 1: Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes. Example 2: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 3: Input: n = 2, k = 0 Output: [11,22,33,44,55,66,77,88,99] Example 4: Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98] Example 5: Input: n = 2, k = 2 Output: [13,20,24,31,35,42,46,53,57,64,68,75,79,86,97] Β  Constraints: 2 <= n <= 9 0 <= k <= 9
class Solution: def numsSameConsecDiff(self, N: int, K: int) -> List[int]: ans = [] def DFS(N, num): if N == 0: return ans.append(num) tail_digit = num % 10 next_digits = set([tail_digit + K, tail_digit - K]) for next_digit in next_digits: if 0 <= next_digit < 10: new_num = num * 10 + next_digit DFS(N - 1, new_num) for num in range(1, 10): DFS(N - 1, num) return list(ans)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR VAR IF NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
def type_(list_v, group): cnt_0 = 0 cnt_1 = 0 for v in list_v: if v in group: cnt_1 += 1 else: cnt_0 += 1 if cnt_1 == len(group): return 1 if cnt_0 == len(list_v): return 0 return 2 def is_all_type_1(ex_index, list_group, v): for i, group in list(list_group.items()): if i == ex_index: continue if type_(g[v], group) != 1: return False return True def check(v, list_group): t = None for i, group in list(list_group.items()): t = type_(g[v], group) if t == 0 or t == 2: if t == 0: if is_all_type_1(i, list_group, v) == True: group[v] = 1 else: return 2 return t return t group = {} def process(g): for v in g: if len(group) == 0: group[0] = {} group[0][v] = 1 continue t = check(v, group) if t == 2: return -1 if t == 1: if len(group) == 3: return -1 group[len(group)] = {} group[len(group) - 1][v] = 1 return group g = {} n, m = list(map(int, input().split())) for _ in range(m): u, v = list(map(int, input().split())) if u not in g: g[u] = [] if v not in g: g[v] = [] g[u].append(v) g[v].append(u) ans = process(g) if ans == -1 or len(ans) < 3: print(-1) else: pr = [0] * n cnt = 0 for k, gr in list(group.items()): for v in gr: cnt += 1 pr[v - 1] = str(k + 1) if cnt == n: print(" ".join(pr)) else: print(-1)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN VAR RETURN VAR ASSIGN VAR DICT FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER DICT ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR DICT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR DICT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
def split_group(n): for g in range(1, 4): first_node = 1 while first_node <= n and group[first_node] != 0: first_node = first_node + 1 if first_node > n: return False group[first_node] = g group_cnt[g] = group_cnt[g] + 1 group_mem[g].append(first_node) for i in range(1, n + 1): if group[i] == 0 and i not in edge[first_node]: group[i] = g group_cnt[g] = group_cnt[g] + 1 group_mem[g].append(i) def judge(): if ( group_cnt[1] * group_cnt[2] + group_cnt[2] * group_cnt[3] + group_cnt[3] * group_cnt[1] != m ): return False for g1 in range(1, 3): for g2 in range(g1 + 1, 4): for node1 in group_mem[g1]: for node2 in group_mem[g2]: if node1 not in edge[node2]: return False return True edge = dict() n, m = [int(c) for c in input().split()] group = [0] * (n + 1) group_cnt = [0] * 4 group_mem = [[], [], [], []] for i in range(n): edge[i + 1] = set() for i in range(m): u, v = [int(c) for c in input().split()] edge[u].add(v) edge[v].add(u) if split_group(n) == False: print(-1) elif judge() == False or m == 0: print(-1) else: for node_group in range(1, n + 1): print(group[node_group], end=" ")
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST LIST LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
n, m = map(int, input().split()) EE = [] if m < 3: print(-1) else: edge = [[] for i in range(n)] a, b = map(int, input().split()) edge[a - 1].append(b - 1) edge[b - 1].append(a - 1) EE.append([a - 1, b - 1]) for i in range(m - 1): x, y = map(int, input().split()) edge[x - 1].append(y - 1) edge[y - 1].append(x - 1) EE.append([x - 1, y - 1]) c = 0 for i in range(n): if a - 1 in edge[i] and b - 1 in edge[i]: c = i + 1 break if c == 0: print(-1) else: Ans = [0] * n Ans[a - 1] = 1 Ans[b - 1] = 2 Ans[c - 1] = 3 flg = True C = [1] * 3 for i in range(n): if Ans[i] != 0: continue else: E = edge[i] if a - 1 in E and b - 1 in E and c - 1 not in E: Ans[i] = 3 C[2] += 1 elif a - 1 in E and b - 1 not in E and c - 1 in E: Ans[i] = 2 C[1] += 1 elif a - 1 not in E and b - 1 in E and c - 1 in E: Ans[i] = 1 C[0] += 1 else: print(-1) flg = False break if flg: T = [0] * 3 for x, y in EE: xx, yy = Ans[x], Ans[y] if xx == yy: print(-1) flg = False break elif xx == 1 and yy == 2: T[0] += 1 elif xx == 2 and yy == 1: T[0] += 1 elif xx == 1 and yy == 3: T[1] += 1 elif xx == 3 and yy == 1: T[1] += 1 else: T[2] += 1 if flg: if T[0] == C[0] * C[1] and T[1] == C[0] * C[2] and T[2] == C[1] * C[2]: print(*Ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
import sys n, m = [int(i) for i in sys.stdin.readline().split()] group = [0] * n data = [set() for i in range(n)] gi = 0 for i in range(m): a, b = [(int(j) - 1) for j in sys.stdin.readline().split()] data[a].add(b) data[b].add(a) for i in range(n): if group[i] == 0: gi += 1 if gi == 4: print(-1) exit() nei = data[i] for j in range(n): if j not in nei: group[j] = gi cnt = [0, 0, 0, 0] cnt[1] = group.count(1) cnt[2] = group.count(2) cnt[3] = n - cnt[1] - cnt[2] for i in range(1, 4): if cnt[i] <= 0: print(-1) exit() amount = 0 for a, b in [(1, 2), (2, 3), (1, 3)]: amount += cnt[a] * cnt[b] if amount != m: print(-1) exit() for a in range(1, 4): for i in range(n): if group[i] == a: for neib in data[i]: c = group[neib] if c == a: print(-1) exit() print(*group)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
import sys input = sys.stdin.readline def r1(): return int(input()) def r2(): return map(int, input().split()) n, m = r2() d = {} for i in range(1, n + 1): d[i] = set() for i in range(m): x, y = r2() d[x].add(y) d[y].add(x) first = set(range(1, n + 1)) - d[1] if not d[1]: print(-1) else: flag = 1 for el in first: if d[el] != d[1]: flag = 0 if not flag: print(-1) else: i = 2 while i not in d[1]: i += 1 second = d[i] - first third = d[1] - second if not second or not third: flag = 0 etalon = first.union(third) for el in second: if d[el] != etalon: flag = 0 etalon = first.union(second) for el in third: if d[el] != etalon: flag = 0 if not flag: print(-1) else: for i in range(1, n + 1): if i in first: print(1, end=" ") elif i in second: print(2, end=" ") else: print(3, end=" ")
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
n, m = map(int, input().split()) neigh = [set() for i in range(n + 1)] total = set(range(1, n + 1)) for i in range(m): a, b = map(int, input().split()) neigh[a].add(str(b)) neigh[b].add(str(a)) for i in range(len(neigh)): neigh[i] = frozenset(neigh[i]) numbers = dict() count = 0 r = [] for i in neigh[1:]: if i in numbers: r.append(numbers[i]) else: if len(i) < 2: print(-1) exit() count += 1 numbers[i] = count r.append(count) if count != 3: print(-1) exit() print(*r)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
import sys n, m = [int(x) for x in input().split()] d = {i: [] for i in range(n)} for _ in range(m): ai, bi = [int(x) for x in input().split()] d[ai - 1].append(bi - 1) d[bi - 1].append(ai - 1) which_set = [None for _ in range(n)] SET_B_OR_C = 0 SET_A = 1 SET_B = 2 SET_C = 3 for x in d[0]: which_set[x] = SET_B_OR_C for x in range(n): if which_set[x] is None: which_set[x] = SET_A if not d[0]: print(-1) sys.exit(0) for x in d[d[0][0]]: if which_set[x] == SET_B_OR_C: which_set[x] = SET_B elif which_set[x] == SET_A: continue else: print(-1) sys.exit(0) for x in range(n): if which_set[x] == SET_B_OR_C: which_set[x] = SET_C num_A, num_B, num_C = 0, 0, 0 for x in which_set: if x == SET_A: num_A += 1 if x == SET_B: num_B += 1 if x == SET_C: num_C += 1 if num_A == 0 or num_B == 0 or num_C == 0: print(-1) sys.exit(0) def same_set(x, y): return which_set[x] == which_set[y] for x in d: for y in d[x]: if same_set(x, y): print(-1) sys.exit(0) expected_num_edges = (num_A * num_B + num_B * num_C + num_A * num_C) * 2 if sum(len(d[x]) for x in d) != expected_num_edges: print(-1) sys.exit(0) print(*which_set)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
import sys input = sys.stdin.readline n, m = map(int, input().split()) E = [set() for i in range(n + 1)] ECOUNT = [0] * (n + 1) for i in range(m): x, y = map(int, input().split()) E[x].add(y) E[y].add(x) ECOUNT[x] += 1 ECOUNT[y] += 1 Group = [i for i in range(n + 1)] def find(x): while Group[x] != x: x = Group[x] return x def Union(x, y): if find(x) != find(y): Group[find(y)] = Group[find(x)] = min(find(y), find(x)) SCORE = 0 for i in range(1, n + 1): if find(i) == i: SCORE += 1 if SCORE >= 4: print(-1) sys.exit() for j in range(i + 1, n + 1): if j in E[i]: continue else: Union(i, j) FD = [find(i) for i in range(n + 1)] if len(set(FD[1:])) != 3: print(-1) sys.exit() for i in range(n + 1): for j in E[i]: if FD[i] == FD[j]: print(-1) sys.exit() compression_dict = {a: ind for ind, a in enumerate(sorted(set(FD)))} ANS = [compression_dict[a] for a in FD] VFD = [0, 0, 0] EFD = [0, 0, 0] for i in range(1, n + 1): VFD[ANS[i] - 1] += 1 EFD[ANS[i] - 1] += ECOUNT[i] for i in range(3): VS = 0 for j in range(3): if i == j: continue VS += VFD[i] * VFD[j] if EFD[i] == VS: continue else: print(-1) sys.exit() print(*ANS[1:])
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
from sys import setcheckinterval, setrecursionlimit, stdin setcheckinterval(1000) setrecursionlimit(10**7) def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) def BFS(s, adj): parent = {s: None} color = [-1] * len(adj) color[s] += 1 u = [s] while u: nextu = [] for i in u: for v in adj[i]: if v not in parent: color[v] = (color[i] + 1) % 3 parent[v] = i nextu.append(v) elif color[v] == color[i]: color[v] = (color[v] + 1) % 3 u = nextu.copy() for i in range(len(adj)): color[i] += 1 return color n, m = lin() adj = [[] for i in range(n)] for _ in range(m): i, j = lin() adj[i - 1].append(j - 1) adj[j - 1].append(i - 1) sol = BFS(0, adj) c1 = [0, 0, 0] for i in sol: if i == 0: print(-1) return c1[i - 1] += 1 ch = [[(0) for i in range(3)] for j in range(3)] for v in range(n): for u in adj[v]: ch[sol[v] - 1][sol[u] - 1] += 1 if sol[v] == sol[u]: print(-1) return a12 = ch[0][1] + ch[1][0] a13 = ch[0][2] + ch[2][0] a23 = ch[1][2] + ch[2][1] if a12 == 2 * c1[0] * c1[1] and a13 == 2 * c1[0] * c1[2] and a23 == 2 * c1[1] * c1[2]: print(*sol) else: print(-1)
EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT VAR NONE ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
import sys input = sys.stdin.readline def r1(): return int(input()) def r2(): return map(int, input().split()) n, m = r2() d = {} for i in range(1, n + 1): d[i] = set() for i in range(m): x, y = r2() d[x].add(y) d[y].add(x) bc = d.get(1, []) if len(bc) == 0: print(-1) exit() ac = d.get(next(iter(bc)), []) if len(ac) == 0: print(-1) exit() c = bc & ac a = ac - c b = bc - c ab = a | b na = len(a) nb = len(b) nc = len(c) if na + nb + nc != n or not na or not nb or not nc: print(-1) else: for x in a: if d.get(x, []) != bc: print(-1) exit() for x in b: if d.get(x, []) != ac: print(-1) exit() for x in c: if d.get(x, []) != ab: print(-1) exit() r = ["" for i in range(n)] for x in a: r[x - 1] = "1" for x in b: r[x - 1] = "2" for x in c: r[x - 1] = "3" print(" ".join(r))
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR LIST VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR LIST VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR LIST VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
a = list(map(int, input().split())) n = a[0] e = a[1] g = {} for itr in range(1, n + 1): g[itr] = [] for i in range(e): a = list(map(int, input().split())) g[a[0]].append(a[1]) g[a[1]].append(a[0]) for itr in range(1, n + 1): g[itr] = frozenset(g[itr]) a = {} k = 1 res = [] for i in range(1, n + 1): if len(g[i]) == 0: k = 100 break if g[i] in a: res.append(a[g[i]]) else: a[g[i]] = k k += 1 res.append(a[g[i]]) if len(a) > 3: break if k != 4: print(-1) else: print(*res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
import sys input = lambda: sys.stdin.readline().rstrip() N, M = list(map(int, input().split())) E = [[] for _ in range(N)] for _ in range(M): a, b = list(map(int, input().split())) E[a - 1].append(b - 1) E[b - 1].append(a - 1) inf = 1 << 20 A, B, C = [], [], [] X = [0] * N for a in E[0]: X[a] = 1 A = [i for i in range(N) if X[i] == 0] b = min([i for i in range(N) if X[i] == 1] + [inf]) if b < inf: for a in E[b]: if X[a] == 1: X[a] = 2 B = [i for i in range(N) if X[i] == 1] c = min([i for i in range(N) if X[i] == 2] + [inf]) if c < inf: for a in E[c]: if X[a] == 2: X[a] = 3 C = [i for i in range(N) if X[i] == 2] if ( max(X) == 2 and len(A) * len(B) * len(C) and len(A) + len(B) + len(C) == N and len(A) * len(B) + len(B) * len(C) + len(A) * len(C) == M ): f = 0 for i in range(N): for j in E[i]: if X[i] == X[j]: f = 1 break if f: break if f: print(-1) else: print(*[(x + 1) for x in X]) else: print(-1)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR LIST LIST LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER LIST VAR IF VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER LIST VAR IF VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
n, m = map(int, input().split()) d = {} a = [["0"] for i in range(n + 1)] for i in range(m): u, v = map(int, input().split()) a[u].append(str(v) + "*") a[v].append(str(u) + "*") count = 1 for i in range(1, n + 1): if len(a[i]) == 1: print("-1") return a[i].sort() for i in range(1, n + 1): a[i] = "".join(a[i]) for i in range(1, n + 1): if a[i] not in d: d[a[i]] = count count += 1 if len(d) != 3: print("-1") return for i in range(1, n + 1): print(d[a[i]], end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL STRING VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
import sys input = sys.stdin.readline n, m = map(int, input().split()) g = [set() for i in range(n)] cnt_e = [0] * n for i in range(m): u, v = map(int, input().split()) g[u - 1].add(v - 1) g[v - 1].add(u - 1) cnt_e[u - 1] += 1 cnt_e[v - 1] += 1 group = [i for i in range(n)] def find(x): while group[x] != x: x = group[x] return x def union(x, y): if find(x) != find(y): group[find(x)] = group[find(y)] = min(find(x), find(y)) score = 0 for i in range(n): if find(i) == i: score += 1 if score >= 6: print(-1) exit() for j in range(i + 1, n): if not j in g[i]: union(i, j) fd = [find(i) for i in range(n)] if len(set(fd)) != 3: print(-1) exit() for i in range(n): for j in g[i]: if fd[i] == fd[j]: print(-1) exit() num = list(set(fd)) ans = [0] * n cnt_v = [0] * 3 cnt_zisu = [0] * 3 for i in range(n): if fd[i] == num[0]: ans[i] = 1 cnt_v[0] += 1 cnt_zisu[0] += cnt_e[i] elif fd[i] == num[1]: ans[i] = 2 cnt_v[1] += 1 cnt_zisu[1] += cnt_e[i] else: ans[i] = 3 cnt_v[2] += 1 cnt_zisu[2] += cnt_e[i] flag = True flag &= cnt_v[0] * cnt_v[1] + cnt_v[0] * cnt_v[2] == cnt_zisu[0] flag &= cnt_v[0] * cnt_v[1] + cnt_v[1] * cnt_v[2] == cnt_zisu[1] flag &= cnt_v[2] * cnt_v[1] + cnt_v[0] * cnt_v[2] == cnt_zisu[2] if flag: print(*ans) else: print(-1)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
import sys input = sys.stdin.readline n, m = map(int, input().split()) tree = [set() for i in range(n)] for i in range(m): a, b = map(int, input().split()) tree[a - 1].add(b - 1) tree[b - 1].add(a - 1) check = [-1] * n seen = [False] * n now = 1 for i in range(n): if not seen[i]: for j in range(n): if j not in tree[i]: check[j] = now seen[j] = True now += 1 if now == 5: print(-1) exit() if now == 1 or now == 2 or now == 3: print(-1) exit() setnum = [set() for i in range(n)] s = [0] * n for i in range(n): setnum[check[i] - 1].add(i) s[check[i] - 1] += 1 for i in range(n): if len(tree[i]) + s[check[i] - 1] != n: print(-1) exit() for j in tree[i]: if j in setnum[check[i] - 1]: print(-1) exit() print(" ".join(map(str, check)))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
def exitprint(): print(-1) exit(0) n, m = list(map(int, input().split())) al = [list() for _ in range(n)] for _ in range(m): u, v = list(map(int, input().split())) al[u - 1].append(v - 1) al[v - 1].append(u - 1) for i in range(n): al[i] = frozenset(al[i]) a = {} k = 1 ans = [] for i in range(n): if len(al[i]) == 0: exitprint() if al[i] in a: ans.append(a[al[i]]) else: a[al[i]] = k k += 1 ans.append(a[al[i]]) if len(a) > 3: exitprint() if len(a) < 3: exitprint() print(*ans)
FUNC_DEF EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. Create three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. -----Input----- The first line contains two integers $n$ and $m$ ($3 \le n \le 10^{5}$, $0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$)Β β€” the number of vertices and edges in the graph. The $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. -----Output----- If the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$. If there are multiple answers, print any. -----Examples----- Input 6 11 1 2 1 3 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 Output 1 2 2 3 3 3 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output -1 -----Note----- In the first example, if $v_{1} = \{ 1 \}$, $v_{2} = \{ 2, 3 \}$, and $v_{3} = \{ 4, 5, 6 \}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. [Image] In the second example, it's impossible to make such vertex sets.
from sys import stdin input = stdin.readline v, e = map(int, input().split()) d = {} for i in range(1, v + 1): d[i] = [] for ed in range(e): a, b = map(int, input().split()) d[a].append(b) d[b].append(a) for i in d: d[i].sort() sas = {} for i in d: sas[tuple(d[i])] = 0 if len(sas) != 3: print(-1) else: nbs = [] for i in sas: nbs.append(set(i)) a = nbs[0].intersection(nbs[1]) b = nbs[0].intersection(nbs[2]) c = nbs[1].intersection(nbs[2]) kol = [0] * (v + 1) for i in a: kol[i] = 1 for i in b: kol[i] = 2 for i in c: kol[i] = 3 zer = 0 for i in range(1, len(kol)): if kol[i] == 0: zer += 1 break if zer > 0: print(-1) elif len(a) + len(b) + len(c) != v: print(-1) else: print(*kol[1:])
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER