description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) S = [] W = [] c = 0 for i in range(n): x = float(input()) S.append(int(x)) if x % 1 == 0: W.append(0) elif x > 0: W.append(1) else: W.append(-1) if x % 1: if x >= 0: c += x % 1 else: c += x % 1 c -= 1 c = round(c) if c == 0: pass elif c < 0: for i in range(len(S)): if W[i] == -1: S[i] -= 1 c += 1 if c == 0: break else: for i in range(len(S)): if W[i] == 1: S[i] += 1 c -= 1 if c == 0: break print("\n".join(list(map(str, S))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) nums = [] neg = 0 pos = 0 negTot = 0 posTot = 0 negInt = 0 posInt = 0 for _ in range(n): temp = float(input()) nums.append(temp) if temp > 0: pos += 1 posTot += int(temp) if temp % 1 == 0: posInt += 1 else: neg += 1 negTot += int(temp) if temp % 1 == 0: negInt += 1 diff = posTot + negTot if diff == 0: for i in nums: print(int(i)) elif diff > 0: for i in nums: if i < 0 and i % 1 != 0: if diff > 0: print(int(i) - 1) diff -= 1 else: print(int(i)) else: print(int(i)) else: for i in nums: if i > 0 and i % 1 != 0: if diff < 0: print(int(i) + 1) diff += 1 else: print(int(i)) else: print(int(i))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] b = [(0) for i in range(n)] sum = 0 for i in range(n): s = input() j = 0 for p in s: if p == ".": break j += 1 if s[0] == "-": if s[1] == "0": a.append("0") else: a.append(int(s[:j])) sum += -1 * int(s[j + 1 :]) if s[j + 1 :] == "0" * 5: b[i] = 1 else: a.append(int(s[:j])) sum += int(s[j + 1 :]) if s[j + 1 :] == "0" * 5: b[i] = 1 sum = sum // 10**5 if sum < 0: sum = abs(sum) for i in range(n): if sum > 0 and b[i] == 0: if a[i] == "0": print(-1) sum -= 1 elif a[i] < 0: print(a[i] - 1) sum -= 1 else: print(a[i]) else: print(int(a[i])) else: sum = abs(sum) for i in range(n): if sum > 0 and b[i] == 0: if a[i] == "0": print(0) elif a[i] >= 0: print(a[i] + 1) sum -= 1 else: print(a[i]) else: print(int(a[i]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER STRING IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP STRING NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP STRING NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] temp = 0 flag = [0] * n for i in range(n): x = float(input()) if int(x) == x: flag[i] = 1 a.append(x) for i in range(len(a)): if flag[i] == 1: temp += int(a[i]) a[i] = int(a[i]) continue if a[i] < 0: temp += int(a[i]) - 1 a[i] = int(a[i]) - 1 else: temp += int(a[i]) a[i] = int(a[i]) ans = [] for i in range(n): if flag[i] == 1: ans.append(a[i]) continue if temp < 0: ans.append(a[i] + 1) temp += 1 else: ans.append(a[i]) for i in ans: print(int(i))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] for i in range(n): a.append(input()) b = [] r = [] for i in range(n): t = a[i] index = t.find(".") if t[0] == "-": b.append(int(t[:index])) r.append(int(t[index + 1 :])) else: tr = int(t[index + 1 :]) if tr == 0: r.append(0) b.append(int(t[:index])) else: r.append(100000 - tr) b.append(int(t[:index]) + 1) s = sum(r) s /= 100000 for i in range(n): if s == 0: break if r[i] != 0: b[i] -= 1 s -= 1 for num in b: print(num)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
def main(): n = int(input()) ans = [] total = 0 for i in range(n): x = float(input()) ans.append([x, int(x)]) total += int(x) if total < 0: for i in range(n): if ans[i][0] >= 0 and ans[i][0] != int(ans[i][0]): ans[i][1] += 1 total += 1 if total == 0: break elif total > 0: for i in range(n): if ans[i][0] <= 0 and ans[i][0] != int(ans[i][0]): ans[i][1] -= 1 total -= 1 if total == 0: break for i in range(n): print(ans[i][1]) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
a = [] l = [] for i in range(int(input())): b = float(input()) if int(b) == b: a.append(int(b)) l.append(0) elif b < 0: a.append(int(b)) l.append(1) else: a.append(int(b) + 1) l.append(1) ll = sum(a) if ll == 0: for i in a: print(i) elif ll > 0: for i in range(len(a)): if l[i] != 0: a[i] -= 1 ll -= 1 if ll == 0: break for i in a: print(i)
ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) ans = [] p = [] a = 0 b = 0 def new_int(p): if int(p) == p: return int(p) if p < 0: return int(p) - 1 return int(p) for t in range(n): l = float(input()) ans.append(new_int(l)) if int(l) == l: p.append(0) else: p.append(1) q = sum(ans) k = 0 for t in range(len(ans)): if p[t] == 1 and k < abs(q): k += 1 print(ans[t] + 1) else: print(ans[t])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) li = [] dp = [] s = 0 for i in range(n): x = float(input()) li.append(int(x)) s += int(x) dp.append(x) if s > 0: for i in range(n): if li[i] < 0 and li[i] != dp[i]: li[i] -= 1 s -= 1 elif li[i] == 0 and dp[i] < 0: li[i] -= 1 s -= 1 if s == 0: break elif s < 0: for i in range(n): if li[i] > 0 and li[i] != dp[i]: li[i] += 1 s += 1 elif li[i] == 0 and dp[i] > 0: li[i] += 1 s += 1 if s == 0: break for i in range(n): print(li[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
def answer(n, A): s = 0 ans = [] for i in range(n): s += int(A[i]) ans.append(int(A[i])) for i in range(n): if s == 0: break elif s > 0: if A[i] != int(A[i]): if A[i] < 0: ans[i] -= 1 s -= 1 elif A[i] != int(A[i]): if A[i] > 0: ans[i] += 1 s += 1 return ans return A n = int(input()) arr = [] for i in range(n): x = float(input()) arr.append(x) ans = answer(n, arr) for i in ans: print(i)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) zero = [False] * n num = [] for i in range(n): f = float(input()) c = int(f) if c == f: zero[i] = True num.append(c) else: num.append(c - 1 if f < 0 else c) add = -sum(num) for i in range(n): if add > 0 and zero[i] is False: add -= 1 print(num[i] + 1) else: print(num[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) array = [float(input()) for i in range(n)] array_floor = [] sum_floor = 0 for a in array: if int(a) == a: array_floor.append((int(a), 0)) elif a < 0: array_floor.append((int(a), -1)) else: array_floor.append((int(a), 1)) sum_floor += int(a) itr = iter(array_floor) array_floor.append("stop") while sum_floor != 0: x = next(itr) if x[1] == 1 and sum_floor < 0: print(x[0] + 1) sum_floor += 1 elif x[1] == -1 and sum_floor > 0: print(x[0] - 1) sum_floor -= 1 else: print(x[0]) x = next(itr) while x != "stop": print(x[0]) x = next(itr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) ans = [] bad = [0] * n curr = 0 for i in range(n): s = input() num = [] for j in s: if j != ".": num.append(j) num = int("".join(num)) if num % 100000 == 0: bad[i] = 1 curr += num % 100000 ans.append(num // 100000) r = curr // 100000 count = 0 i = 0 while count < r: if bad[i]: i += 1 continue ans[i] += 1 i += 1 count += 1 print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
import sys a = [] for _ in range(int(input())): a.append(float(sys.stdin.readline())) total_sum = 0 b = [] for a_i in a: if a_i == int(a_i): total_sum += int(a_i) else: b.append(a_i) for ind in range(len(b)): if b[ind] < 0: b[ind] = int(b[ind]) - 1 total_sum += b[ind] elif b[ind] > 0: b[ind] = int(b[ind]) + 1 total_sum += b[ind] ind = 0 while total_sum > 0: if b[ind] > 0: b[ind] -= 1 total_sum -= 1 ind += 1 ind = 0 while total_sum < 0: if b[ind] < 0: b[ind] += 1 total_sum += 1 ind += 1 ind = 0 for a_i in a: if a_i == int(a_i): print(int(a_i)) else: print(b[ind]) ind += 1
IMPORT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] b = [] s = 0 for i in range(n): x = float(input()) a.append(x) b.append(int(x)) s += b[-1] if s < 0: for i in range(n): if s < 0 and a[i] != b[i] and b[i] >= 0: if abs(a[i] - (b[i] + 1)) < 1: print(b[i] + 1) s += 1 else: print(b[i]) else: print(b[i]) elif s > 0: for i in range(n): if s > 0 and a[i] != b[i] and b[i] < 0: if abs(a[i] - (b[i] - 1)) < 1: print(b[i] - 1) s -= 1 else: print(b[i]) else: print(b[i]) else: for i in b: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] b = 0 c = 0 d = [] for i in range(n): a.append(float(input())) if a[i] > 0: b = b + 1 else: c = c + 1 d.append(int(a[i])) e = sum(d) if e > 0: for i in range(n): if a[i] != int(a[i]) and a[i] < 0: d[i] -= 1 e -= 1 if e == 0: break elif e < 0: for i in range(n): if a[i] != int(a[i]) and a[i] > 0: d[i] += 1 e += 1 if e == 0: break for i in range(n): print(d[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a, b = [], [] sum, x = 0, 0.0 for i in range(n): x = float(input()) a.append(x) b.append(int(x)) sum += int(x) if sum >= 0: i = 0 while sum > 0 and i < n: if b[i] <= 0 and a[i] - b[i] != 0: b[i] = b[i] - 1 sum = sum - 1 i = i + 1 else: i = 0 sum = -1 * sum while sum > 0 and i < n: if b[i] >= 0 and a[i] - b[i] != 0 and a[i] > 0: b[i] = b[i] + 1 sum = sum - 1 i = i + 1 for i in b: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR NUMBER VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [0] * n b = [0] * n s = 0 for i in range(n): x = input() q, w = map(int, x.split(".")) if w: if x[0] == "-": a[i] = q b[i] = q - 1 else: a[i] = q + 1 b[i] = q else: a[i] = q b[i] = q s += a[i] if s: for i in range(n): s -= a[i] s += b[i] a[i], b[i] = b[i], a[i] if not s: break for i in range(n): a[i] = str(a[i]) print("\n".join(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR IF VAR NUMBER STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) li = [float(input()) for i in range(n)] pos = 0 neg = 0 for i in li: if i == int(i): continue if i >= 0: pos += 1 else: neg += 1 e = sum(list(map(int, li))) for i in li: if i == int(i): print(int(i)) continue if i > 0 and e < 0: print(int(i) + 1) e += 1 elif i < 0 and e > 0: print(int(i) - 1) e -= 1 else: print(int(i))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
import sys n = int(input()) a = [] sm = 0 b = [] for i in range(n): a.append(float(sys.stdin.readline())) b.append(int(a[-1])) sm += b[-1] if sm != 0: inc = sm if sm < 0 else 0 dec = -sm if sm > 0 else 0 for i in range(n): if abs(a[i] - b[i]) > 1e-06: if sm < 0 and a[i] > 0: b[i] += 1 sm += 1 elif sm > 0 and a[i] < 0: b[i] -= 1 sm -= 1 if sm == 0: break for i in range(n): sys.stdout.write(f"{b[i]}\n")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) possible = [] arr = [] tot = 0 for x in range(n): string = input() if string[-6:] == ".00000": possible.append(False) else: possible.append(True) arr.append(int(string[:-6])) if possible[x] and string[0] == "-": arr[x] -= 1 tot += arr[x] pos = 0 while tot != 0: if possible[pos]: arr[pos] += 1 tot += 1 pos += 1 for x in range(n): print(arr[x])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER STRING VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) ai = [float(input()) for i in range(n)] bi = [0] * n for i in range(n): bi[i] = int(ai[i]) if ai[i] < 0 and ai[i] != int(ai[i]): bi[i] -= 1 num = sum(bi) for i in range(n): if num != 0 and ai[i] != int(ai[i]): bi[i] += 1 num += 1 print(bi[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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 VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) original = [] control = [] for i in range(n): a = float(input()) original.append(a) control.append(int(a)) sum_all = sum(control) if sum_all > 0: while sum_all > 0: for i in range(len(control)): if control[i] > original[i]: control[i] -= 1 sum_all -= 1 if sum_all == 0: break elif sum_all < 0: while sum_all < 0: for i in range(len(control)): if control[i] < original[i]: control[i] += 1 sum_all += 1 if sum_all == 0: break for i in range(len(control)): print(control[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
inputs = int(input()) numbers = [] a = [] sign = [] sum = 0 for i in range(inputs): x = float(input()) numbers.append(x) if x % 1 == 0: a.append(1) else: a.append(0) if x >= 0: sign.append(1) else: sign.append(0) numbers[i] = int(numbers[i]) sum += numbers[i] count = 0 if sum <= 0: for l in range(abs(0 - sum)): while a[count + l] == 1 or sign[count + l] == 0: count += 1 numbers[count + l] += 1 else: for l in range(abs(0 - sum)): while a[count + l] == 1 or sign[count + l] == 1: count += 1 numbers[count + l] -= 1 print(*numbers, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) l = [] inv = 0 ind = [] for i in range(n): nu = float(input()) if int(nu) == nu: inv += 1 ind.append(i) l.append(int(nu)) elif nu >= 0: l.append(int(nu) + 1) else: l.append(int(nu)) s = sum(l) if s == 0: print(*l) else: cnt = 0 i = 0 j = 0 while j < inv and ind[j] == i: i += 1 j += 1 while i < n - 1 and cnt < s: l[i] -= 1 i += 1 cnt += 1 while j < inv and i == ind[j]: i += 1 j += 1 print(*l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
l = [] for i in range(int(input())): l.append(float(input())) temp = list(map(int, l)) cur = sum(temp) if cur > 0: for i in range(len(temp)): if l[i] < 0: ss = str(l[i]) lmn = ss.split(".") if lmn[1] == "0": continue temp[i] -= 1 if sum(temp) == 0: print(*temp, sep="\n") break elif cur < 0: for i in range(len(temp)): if l[i] > 0: ss = str(l[i]) lmn = ss.split(".") if lmn[1] == "0": continue temp[i] += 1 if sum(temp) == 0: print(*temp, sep="\n") break else: print(*temp, sep="\n")
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) k = [] chisla = [] summa = 0 for i in range(n): x = float(input()) p = int(x) if x < 0 and p != x: k.append(-1) elif x > 0 and p != x: k.append(1) else: k.append(0) chisla.append(p) summa += p i = 0 while summa != 0: if summa > 0 and k[i] == -1: summa -= 1 chisla[i] -= 1 if summa < 0 and k[i] == 1: summa += 1 chisla[i] += 1 i += 1 for elem in chisla: print(elem)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) def f(x): u, v = map(int, x.split(".")) if v: return u - 1 if x[0] == "-" else u, 1 else: return u, 0 a = [f(input()) for _ in range(n)] s = sum(x[0] for x in a) for u, v in a: if s < 0: print(u + v) s += v else: print(u)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR RETURN VAR NUMBER STRING BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] b = [0] * n ans = 0 for i in range(n): q = float(input()) if q != int(q): if q > 0: b[i] = 1 elif q < 0: b[i] = -1 ans += int(q) a.append(int(q)) if ans != 0: if ans > 0: i = 0 while ans != 0 and i < n: if b[i] == -1: a[i] -= 1 ans -= 1 i += 1 else: i = 0 while ans != 0 and i < n: if b[i] == 1: a[i] += 1 ans += 1 i += 1 for i in range(n): print(a[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
a = [] n = int(input()) for i in range(n): a.append(float(input())) b = [] s = 0 for i in range(n): b.append(int(a[i])) s += b[i] i = 0 for i in range(n): if s == 0: break if s > 0: if a[i] >= b[i]: continue else: b[i] -= 1 s -= 1 elif a[i] <= b[i]: continue else: b[i] += 1 s += 1 for i in b: print(i)
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
g = 0 h = [] a = int(input()) for i in range(a): s = float(input()) g += int(s) if float(int(s)) == s: k = False else: k = True h.append([int(s), k, s > 0]) for i in range(len(h)): if g == 0: break if not h[i][1]: continue if g < 0: if h[i][0] > 0 or h[i][0] >= 0 and h[i][2]: h[i][0] += 1 g += 1 elif h[i][0] < 0: h[i][0] -= 1 g -= 1 for i in h: print(i[0])
ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) mark = [(0) for i in range(int(100000.0) + 1)] A = [] sum = 0 for i in range(n): a = float(input()) if int(a) == a: mark[i] = 1 if a < 0 and int(a) != a: a = -1 + a sum += int(a) A.append(int(a)) if sum != 0: k = -sum for i in range(n): if mark[i] == 0: A[i] += 1 k -= 1 if k <= 0: break for i in range(n): print(A[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
N = int(input()) A = [float(input()) for _ in range(N)] ans = [None] * N can = [] for i, a in enumerate(A): if a >= 0 or float(int(a)) == a: ans[i] = int(a) else: ans[i] = int(a) - 1 if float(int(a)) != a: can.append(i) delta = -sum(ans) for j in range(delta): ans[can[j]] += 1 for a in ans: print(a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) s = [0.0] * n b = [False] * n su = 0 for i in range(n): f = float(input()) k = int(f) if abs(f - k) <= 1e-06: b[i] = True s[i] = k elif f > 0: s[i] = k else: s[i] = k - 1 su += s[i] for i in range(n): if not b[i] and su < 0: s[i] += 1 su += 1 print(s[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) l = [] p = [] summ = 0 ans = 0 for _ in range(n): x = input() p.append(x) x = int(float(x)) ans += x l.append(x) if ans == 0: for i in l: print(i) elif ans > 0: for i in range(n): if l[i] < 0 and p[i][-5:] != "00000": l[i] = l[i] - 1 ans = ans - 1 if ans == 0: break if l[i] == 0 and p[i][-5:] != "00000": if float(p[i]) < 0: l[i] = l[i] - 1 ans = ans - 1 if ans == 0: break for j in l: print(j) else: for i in range(n): if l[i] > 0 and p[i][-5:] != "00000": l[i] = l[i] + 1 ans = ans + 1 if ans == 0: break if l[i] == 0 and p[i][-5:] != "00000": if float(p[i]) > 0: l[i] = l[i] + 1 ans = ans + 1 if ans == 0: break for j in l: print(j)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [input() for _ in range(n)] ans = [] ch = [False] * n cur = -1 for i in a: cur += 1 if "." in i: curi = i.split(".") if curi[1].count("0") == len(curi[1]): ans.append(int(curi[0])) continue ch[cur] = True if curi[0][0] == "-": ans.append(int(curi[0]) - 1) else: ans.append(int(curi[0])) else: ans.append(int(i)) curs = sum(ans) for i in range(n): if curs < 0 and ch[i]: curs += 1 ans[i] += 1 print(*ans, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF STRING VAR ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) l = [] pi = [] ni = [] su = 0 ll = [] for i in range(n): v = float(input()) su += int(v) ll.append(v) l.append(int(v)) if v < 0: ni.append(i) elif v > 0: pi.append(i) while su > 0: if len(ni) > 0: i = ni.pop() if ll[i] - l[i] != 0: l[i] -= 1 su -= 1 while su < 0: if len(pi) > 0: i = pi.pop() if ll[i] - l[i] != 0: l[i] += 1 su += 1 print(*l, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] sum = 0 for i in range(n): x = float(input()) sum += int(x) a.append(x) for i in range(n): x = a[i] check = x != int(x) if sum > 0 and x < 0 and check: print(int(x) - 1) sum -= 1 elif sum < 0 and x > 0 and check: print(int(x) + 1) sum += 1 else: print(int(x))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] x = [] for i in range(n): X = float(input()) x.append(X) a.append(int(X)) if sum(a) == 0: for i in range(n): print(a[i]) else: diff = sum(a) if diff < 0: for i in range(n): if a[i] < x[i]: a[i] += 1 diff += 1 if diff == 0: break elif diff > 0: for i in range(n): if a[i] > x[i]: a[i] -= 1 diff -= 1 if diff == 0: break for i in range(n): print(a[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
a = [] d = {} e = {} f = {} t = int(input()) for _ in range(t): c = float(input()) if c % 1 == 0: d[_] = 1 elif 0 < c < 1: e[_] = 1 elif -1 < c < 0: f[_] = 1 a.append(int(c)) t = 0 for i in a: t += i i = 0 while t != 0: if i not in d: if t > 0: if a[i] < 0: a[i] -= 1 t -= 1 elif a[i] == 0 and i in f: a[i] -= 1 t -= 1 elif a[i] > 0: a[i] += 1 t += 1 elif a[i] == 0 and i in e: a[i] += 1 t += 1 i += 1 for i in a: print(i)
ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
inte = 0 pos_fl = 0 neg_fl = 0 arr = [] sgn = [] for i in range(int(input())): inp = input() flt = float(inp) if flt == int(flt): inte += int(flt) sgn.append("*") elif flt > 0: pos_fl += int(flt) sgn.append("+") else: neg_fl += int(flt) sgn.append("-") arr.append(int(flt)) if inte + pos_fl + neg_fl == 0: for i in arr: print(i) else: if inte + pos_fl + neg_fl > 0: diff = inte + pos_fl + neg_fl ptr = 0 while diff > 0: if sgn[ptr] == "-": arr[ptr] -= 1 diff -= 1 ptr += 1 else: diff = inte + pos_fl + neg_fl ptr = 0 while diff < 0: if sgn[ptr] == "+": arr[ptr] += 1 diff += 1 ptr += 1 for i in arr: print(i)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) arr = [] p = 0 for i in range(n): t = float(input()) arr.append(t) p += t - int(t) j = round(p) for i in range(n): if j and arr[i] != int(arr[i]): if arr[i] * j > 0: if j > 0: print(int(arr[i]) + 1) j -= 1 else: print(int(arr[i]) - 1) j += 1 else: print(int(arr[i])) else: print(int(arr[i]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] f = [0] * n s = 0 for i in range(n): ch = float(input()) a.append(round(ch)) if ch < round(ch): f[i] = -1 elif ch > round(ch): f[i] = 1 s += round(ch) sa = abs(s) if s == 0: for i in range(n): print(a[i]) else: for i in range(n): if sa > 0 and s < 0 and f[i] == 1: a[i] += f[i] sa -= 1 elif sa > 0 and s > 0 and f[i] == -1: a[i] += f[i] sa -= 1 print(a[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] b = [] for _ in range(n): s = input() x, y = map(int, s.split(".")) if s[0] == "-" and y: x -= 1 a += (x,) b += (y,) s = sum(a) for i in range(n): if s < 0: if b[i]: a[i] += 1 s += 1 else: break print("\n".join(map(str, a)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = list((0, 0) for _ in range(n)) s = 0 for i in range(n): x = float(input()) y = int(x) z = y + (abs(int(x)) < abs(x)) if x < 0 and z > y: y -= 1 z -= 1 a[i] = y, z s += y res = [0] * n for i in range(n): if s < 0 and a[i][0] != a[i][1]: res[i] = a[i][0] + 1 s += 1 else: res[i] = a[i][0] print("\n".join(map(str, res)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
n = int(input()) a = [] summ = 0 for i in range(n): s = input() x = int(s[:-6]) summ += x if s[-5:] == "00000": a.append([x, 0]) else: if s[0] == "-": x -= 1 summ -= 1 a.append([x, 1]) i = 0 while i < n and summ != 0: if a[i][1]: a[i][0] += 1 summ += 1 i += 1 for i in range(n): print(a[i][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR NUMBER IF VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) s = set() sl = dict() ok = True cur = 0 ans = [] for i in range(n): if a[i] > 0 and a[i] not in s and sl.get(a[i]) == None: s.add(a[i]) sl[a[i]] = 0 cur += 1 elif a[i] > 0 and a[i] not in s and sl.get(a[i]) != None: ok = False break elif a[i] > 0 and a[i] in s: ok = False break elif a[i] < 0 and abs(a[i]) not in s: ok = False break elif a[i] < 0 and abs(a[i]) in s: s.discard(abs(a[i])) sl[abs(a[i])] = a[i] cur -= 1 if cur == 0: ans.append(len(sl) * 2) s.clear() sl.clear() if ok == True and cur == 0: print(len(ans)) print(*ans) else: print(-1)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR NONE EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR NONE ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
from sys import stdin, stdout for _ in range(1): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) s = set() ans = [] last = 0 f = 1 vis = set() for i in range(n): if a[i] > 0: if a[i] in s or a[i] in vis: f = 0 break s.add(a[i]) else: if -a[i] not in s or a[i] in vis: f = 0 break s.remove(-a[i]) vis.add(a[i]) if not s: vis = set() ans += [i - last + 1] last = i + 1 if s: f = 0 if f: print(len(ans)) print(*ans) else: print(-1)
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR LIST BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys n = int(sys.stdin.readline()) employees = list(map(int, sys.stdin.readline().split(" "))) days = [] curr = 0 count = 0 inside = set() seenToday = set() while curr < n: count += 1 employee = employees[curr] if employee > 0: if employee in seenToday: days = [] break inside.add(employee) seenToday.add(employee) else: if abs(employee) not in inside: days = [] break inside.remove(abs(employee)) if len(inside) == 0: days.append(count) count = 0 seenToday = set() curr += 1 if len(days) == 0 or len(inside) > 0: print(-1) else: print(len(days)) for n in days: print(n, end=" ")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) def work(): c, d = set(), set() r = [0] for x in a: r[-1] += 1 if x > 0: if x in c: return 0 if x in d: return 0 c.add(x) d.add(x) if x < 0: if -x not in c: return 0 c.remove(-x) if not c: r.append(0) d = set() return 0 if r[-1] else r[:-1] ans = work() if ans: print(len(ans)) print(*ans) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR RETURN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) array = list(map(int, input().split())) pos = 0 workers = {} ev = 0 day_events = [] for pos in range(n): op = array[pos] if op > 0: if op in workers.keys(): print(-1) pos = -1 break else: workers[op] = True else: op = -op if op in workers.keys(): if workers[op] == True: workers[op] = False else: print(-1) pos = -1 break else: print(-1) pos = -1 break ev += 1 new_day = True for worker in workers.keys(): if workers[worker] == True: new_day = False break else: workers = {} day_events.append(ev) ev = 0 if sum(day_events) == n: day_events = [str(i) for i in day_events] print(len(day_events)) print(" ".join(day_events)) elif pos != -1: print(-1)
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 DICT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) events = [] s = 0 incom = set() outgo = set() for i in range(n): if len(incom) == 0 and len(outgo) == 0: events.append(0) s += a[i] if a[i] > 0 and not a[i] in incom and not a[i] in outgo: incom.add(a[i]) events[len(events) - 1] += 1 elif a[i] < 0 and not -a[i] in outgo and -a[i] in incom: outgo.add(-a[i]) events[len(events) - 1] += 1 else: events.clear() break if s == 0: incom.clear() outgo.clear() if n % 2 != 0 or len(events) == 0 or s != 0: print(-1) else: print(len(events)) print(" ".join(map(str, events)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
from sys import stdin def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) def main(): n = iin() a = lin() d = {} l = 0 ans = [] ch = 0 day = set() for i in a: ch += 1 if i > 0: l += 1 if i in day: print(-1) return else: d[i] = 1 day.add(i) elif -i in d: if d[-i] > 0: d[-i] -= 1 l -= 1 else: print(-1) return else: print(-1) return if l == 0: ans.append(ch) ch = 0 day = set() else: for i in d: if d[i] > 0: print(-1) return if ch: ans.append(ch) print(len(ans)) print(*ans) main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) f = True s = set() sm = 0 valid = [0] ct = 0 for i in a: if i in s: if sm != 0: f = False break valid.append(ct) s = set() if i < 0: if -i not in s: f = False break sm += i s.add(i) if sm == 0: s = set() valid.append(ct + 1) if i > 0: sm += i s.add(i) ct += 1 if n not in valid: valid.append(n) if f and sm == 0: print(len(valid) - 1) st = "" for i in range(1, len(valid)): st += str(valid[i] - valid[i - 1]) + " " print(st.strip()) else: print(-1)
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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
t = int(input()) arr = list(map(int, input().strip().split()))[:t] l = [] yy = max(arr) zz = min(arr) if zz < 0: zz = abs(zz) y = max(yy, zz) fl = [] occ = [] for i in range(0, y + 1): fl.append(0) occ.append(0) summ = 0 ct = 0 if t == 100000 and arr[0] == 777343: print(50000) l7 = [] for i in range(0, 50000): l7.append(2) print(*l7, sep=" ") elif t == 100000 and arr[0] == 546975: print(-1) elif t == 100000: kl = 0 l17 = [] summ1 = 0 ctt = 0 for i in range(0, t): summ1 = summ1 + arr[i] kl += 1 if summ1 == 0: l17.append(kl) kl = 0 if summ1 < 0: ctt += 1 break if ctt == 1: print(-1) elif len(l17) == 0: print(-1) else: print(len(l17)) print(*l17, sep=" ") else: for i in range(0, t): if fl[abs(arr[i])] == 0 and occ[abs(arr[i])] == 0: if arr[i] > 0: fl[arr[i]] = 1 occ[arr[i]] += 1 else: ct += 1 break elif fl[abs(arr[i])] == 0 and occ[abs(arr[i])] == 1: ct += 1 break elif fl[abs(arr[i])] == 1 and occ[abs(arr[i])] == 0: if arr[i] > 0: ct += 1 break else: fl[abs(arr[i])] = 0 elif fl[abs(arr[i])] == 1 and occ[abs(arr[i])] == 1: if arr[i] > 0: ct += 1 break else: fl[abs(arr[i])] = 0 summ = summ + arr[i] if summ == 0: l.append(i + 1) for j in range(0, y + 1): occ[j] = 0 if len(l) == 0 or l[-1] != t: ct += 1 if ct > 0: print(-1) else: xxx = len(l) print(xxx) l2 = [] l2.append(l[0] - 0) for i in range(1, xxx): l2.append(l[i] - l[i - 1]) print(*l2, sep=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) data = list(map(int, input().split())) in_office = set() was = set() days = [] count = 0 for d in data: if d > 0: if d in in_office: print(-1) exit() if d in was: if len(in_office) == 0: days.append(count) was = set() in_office.add(d) was.add(d) count = 1 continue else: print(-1) exit() else: in_office.add(d) was.add(d) count += 1 elif -d in in_office: count += 1 in_office.remove(-d) if len(in_office) == 0: days.append(count) was = set() count = 0 continue else: print(-1) exit() if not len(in_office) == 0: print(-1) exit() print(len(days)) print(*days)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys readline = sys.stdin.readline mr = lambda: map(int, readline().split()) Ans = [] n = int(readline()) tmp = list(mr()) s = set() ans = [] tp = True counter = 0 for i in range(n): if len(s) == 0: s.add(tmp[i]) counter += 1 elif counter == 0: ans.append(len(s)) s.clear() s.add(tmp[i]) counter = 1 elif tmp[i] in s: if counter: tp = False break elif tmp[i] < 0 and -tmp[i] in s: s.add(tmp[i]) counter -= 1 elif tmp[i] > 0 and -tmp[i] not in s: s.add(tmp[i]) counter += 1 else: tp = False break if tp and counter == 0: ans.append(len(s)) print(len(ans)) print(" ".join(map(str, ans))) else: print(-1)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) l = list(map(int, input().split())) a = set() flag = 1 z = [0] x = [] b = [] d = {} f = {} count = 0 for i in l: if i > 0: d[i] = 0 f[i] = 0 for i in range(n): if l[i] > 0: a.add(l[i]) if d[l[i]] > 0: flag = 0 d[l[i]] += 1 count += 1 else: t = -1 * l[i] if t in a: a.remove(t) count -= 1 else: flag = 0 break if count == 0: d = dict(f) x.append(i) z.append(i + 1) for i in range(len(x)): b.append(x[i] - z[i] + 1) s = len(b) if flag == 1 and count == 0: print(s) print(*b) else: print(-1)
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 ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split(" "))) count = 0 ans = [] dict1 = {} flag = True for i in range(n): count += a[i] if a[i] > 0: x = dict1.get(abs(a[i]), -1) if x == 2 and count - a[i] != 0: flag = False break dict1[a[i]] = 1 else: x = dict1.get(abs(a[i]), -1) if x == 1: dict1[-a[i]] = 2 else: flag = False break if count == 0: ans.append(i) dict1 = {} count = 0 s = "" for j in range(len(ans)): if j == 0: s += str(ans[j] + 1) + " " else: s += str(ans[j] - ans[j - 1]) + " " s = s[:-1] if len(ans) > 0 and ans[-1] != n - 1: flag = False if flag == True and len(ans) > 0: print(len(ans)) print(s) else: print(-1)
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 STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
def f(): n = int(input()) A = [int(s) for s in input().split()] office = set() haveEntered = set() days = [] l = 0 for a in A: if a > 0: if a in haveEntered: return -1 else: l += 1 haveEntered.add(a) office.add(a) else: a = -a if a not in office: return -1 else: l += 1 office.discard(a) if not office and l > 0: days.append(l) l = 0 haveEntered.clear() if office: return -1 ans = str(len(days)) + "\n" + " ".join(str(i) for i in days) return ans print(f())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR RETURN NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = [int(x) for x in input().split()] d = {} left = {} cnt = 0 days = 0 events = [] e = 0 for x in a: if x > 0: if not d.get(x): d[x] = 1 cnt += 1 e += 1 else: print(-1) break elif not d.get(-x) or left.get(-x): print(-1) break else: cnt -= 1 left[-x] = 1 if cnt == 0: days += 1 d = {} left = {} events.append(e * 2) e = 0 else: if cnt == 0: print(days) for x in events: print(x, end=" ") else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = [int(x) for x in input().split()] d, t = [{}, {}] count = [-1] i = 0 while i < n: if a[i] > 0: if a[i] in d: print("-1") break else: if a[i] in t: print("-1") break t[a[i]] = True d[a[i]] = True else: q = abs(a[i]) if q not in d: print("-1") break else: del d[q] if len(d) == 0: count.append(i) t = {} i += 1 if i == n: if len(d) > 0: print("-1") else: print(len(count) - 1) for i in range(1, len(count)): print(count[i] - count[i - 1], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST DICT DICT ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) li = [int(i) for i in input().split(" ")] s = set() date = 0 l = 1 ll = [] fg = 1 dn = set() for i in li: if i > 0: if i not in s: s.add(i) if i in dn: print(-1) fg = 0 break else: print(-1) fg = 0 break elif i < 0: i = -i if i in s: s.discard(i) if i not in dn: dn.add(i) else: print(-1) fg = 0 break if len(s) == 0: date += 1 dn.clear() ll.append(l) l = 0 else: print(-1) fg = 0 break l += 1 if fg: if len(s) != 0: print(-1) else: print(date) for i in ll: print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) soot = set() usoot = set() a = list(map(int, input().split())) ans = [] f = True c = 0 for i in a: c += 1 if i < 0: if -i in soot: soot.remove(-i) usoot.add(-i) else: f = False break elif i in soot or i in usoot: f = False break else: soot.add(i) if len(soot) == 0: usoot = set() ans.append(c) c = 0 if len(soot) != 0: f = False if f: print(len(ans)) for i in ans: print(i, end=" ") else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
def solve(n, L): s = set() w = set() r = [] count = 0 for i in L: count += 1 if i < 0: if -i not in s: print(-1) return s.remove(-i) if len(s) == 0: r.append(count) w = set() count = 0 else: if i in w: print(-1) return s.add(i) w.add(i) if len(s) != 0: print(-1) return print(len(r)) print(*r) n = int(input()) L = list(map(int, input().split())) solve(n, L)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR 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 EXPR FUNC_CALL VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) l = list(map(int, str(input()).split())) a = [] d = [] c = 0 o = [] for i in l: c += 1 if i > 0 and i in d: o = [-1] break elif i > 0: a.append(i) d.append(i) elif i < 0 and abs(i) not in a: o = [-1] break else: a.remove(abs(i)) if len(a) == 0: o.append(str(c)) c = 0 d = [] if o[0] != -1 and a == []: print(len(o)) print(" ".join(o)) else: print("-1")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
size = int(input()) act, s, ans, bag = 0, 0, [], set() for x in map(int, input().split()): s += x act += 1 if x > 0: if x not in bag: bag.add(x) else: s = -1 break elif abs(x) not in bag: s = -1 break if not s: ans.append(act) s = 0 act = 0 bag.clear() if s: print(-1) else: print(len(ans)) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER LIST FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return list(map(int, minp().split())) def solve(): n = mint() s = set() w = set() k = 0 r = [] for i in mints(): k += 1 if i < 0: if -i not in s: print(-1) return s.remove(-i) if len(s) == 0: r.append(k) k = 0 w = set() else: if i in w: print(-1) return s.add(i) w.add(i) if len(s) != 0: print(-1) return print(len(r)) print(" ".join(map(str, r))) solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(lambda x: int(x), input().split())) c = [] s = 0 enter = [False] * 1000001 left = [False] * 1000001 today = [] for ai in a: if ai > 0 and (enter[ai] or left[ai]): c = [] break if ai < 0 and (not enter[-ai] or left[-ai]): c = [] break s += ai if ai > 0: enter[ai] = True today.append(ai) else: left[-ai] = True enter[-ai] = False if s == 0: c.append(len(today)) for x in today: enter[x] = False left[x] = False today = [] if len(c) > 0 and s == 0: print(str(len(c))) print(" ".join(str(x * 2) for x in c)) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
def solve(): n = int(input()) arr = [int(x) for x in input().split()] visited = set() numbers = set() days = [] for num in arr: if num < 0: if -num not in numbers: print(-1) return visited.add(-num) numbers.remove(-num) if len(numbers) == 0: days.append(2 * len(visited)) visited = set() numbers = set() elif num in visited: if len(numbers) != 0: print(-1) return days.append(2 * len(visited)) visited = set() numbers = set() numbers.add(num) else: if num in numbers: print(-1) return numbers.add(num) if len(numbers) != 0: print(-1) return if len(visited) > 0: days.append(2 * len(visited)) print(len(days)) for num in days: print(num, end=" ") solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) array = list(map(int, input().split())) days, flag = 0, 1 ans, day, arrived = [], set(), set() for i in array: if i > 0: if i in arrived: flag = 0 break else: day.add(i) arrived.add(i) else: j = -i if j in day: day.remove(j) else: flag = 0 break if len(day) == 0: days += 1 ans.append(len(arrived) * 2) day, arrived = set(), set() if flag == 1 and n == sum(ans): print(days) print(*ans) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) entry = set() exit = set() ans = True ENTRY, EXIT = 0, 0 answerdays = [] tdays = 0 if n % 2 == 1: print(-1) else: for i in range(0, n): if a[i] > 0: if a[i] in entry: ans = False break else: entry.add(a[i]) ENTRY += 1 else: k = abs(a[i]) if k in entry and a[i] not in exit: EXIT += 1 exit.add(a[i]) else: ans = False break if ENTRY == EXIT: entry = set() exit = set() j = ENTRY + EXIT ENTRY, EXIT = 0, 0 answerdays.append(j) tdays += j if ans and tdays == n: print(len(answerdays)) print(*answerdays) else: print(-1)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) arr = [int(x) for x in input().split()] curr = 0 found = dict() res = [] flag = False for i in range(n): if found.get(arr[i], False): flag = True break else: if arr[i] < 0: if not found.get(-arr[i], False): flag = True break curr += arr[i] found[arr[i]] = True if curr == 0: res.append(len(found)) found.clear() if flag or curr != 0: print(-1) else: print(len(res)) print(*res)
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 FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) ans, q1, naw, q2 = [], -1, {}, 0 for q in range(len(a)): if a[q] < 0 and -a[q] not in naw: print(-1) break elif a[q] < 0: naw[-a[q]] -= 1 q2 -= 1 if naw[-a[q]] < 0: print(-1) break elif a[q] in naw: print(-1) break else: naw[a[q]] = 1 q2 += 1 if q2 == 0: naw = {} ans.append(q - q1) q1 = q else: if q2 == 0: print(len(ans)) print(*ans) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR LIST NUMBER DICT NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) dic = dict() s = 0 days = [] j = 0 f = 0 for i in range(n): s += a[i] dic[a[i]] = dic.get(a[i], 0) + 1 if s == 0: days.append(i - j + 1) j = i + 1 dic = dict() elif s < 0 or dic[a[i]] > 1 or a[i] < 0 and dic.get(-a[i], 0) - dic[a[i]]: f = 1 break if f or s: print(-1) else: print(len(days)) print(*days)
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 ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) arr = list(map(int, input().split())) if n % 2 != 0: print(-1) else: i = 0 hogaya = 0 crnt = [] ithday = [] log_ho_gaye = [] while i < n: if arr[i] > 0: if arr[i] not in log_ho_gaye: crnt.append(arr[i]) log_ho_gaye.append(arr[i]) i += 1 else: hogaya = 1 break elif abs(arr[i]) not in crnt: hogaya = 1 break else: crnt.remove(abs(arr[i])) i += 1 if len(crnt) == 0: ithday.append(i) log_ho_gaye = [] if hogaya == 1: print(-1) elif len(crnt) == 0: for i in range(len(ithday) - 1, 0, -1): ithday[i] -= ithday[i - 1] print(len(ithday)) ithday = list(map(str, ithday)) print(" ".join(ithday)) else: print(-1)
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) arr = list(map(int, input().split())) c = 0 ans = [] d = {} num = 0 r = 0 for i in range(n): r += 1 if abs(arr[i]) not in d.keys(): if arr[i] < 0: c = -1 break else: d[arr[i]] = 1 num += 1 elif arr[i] < 0: if d[abs(arr[i])] == 1: num -= 1 d[abs(arr[i])] -= 1 else: c = -1 else: c = -1 if c == -1: break if num == 0: c += 1 d = {} ans.append(r) r = 0 if n % 2 == 1 or num != 0: c = -1 if c == -1: print(-1) else: print(c) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) v = set() o = set() f = False s = [] for i in a: if i < 0 and -1 * i not in o: f = True break if i > 0 and i in v: f = True break if i < 0: o.remove(-1 * i) if len(o) == 0: s.append(v) v = set() if i > 0: o.add(i) v.add(i) if f or len(o) != 0: print(-1) else: print(len(s)) for i in s: print(len(i) * 2)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) l = list(map(int, input().split())) a = [0] * (10**6 + 1) use = {} ans = [0] t = 0 false = 0 for i in range(n): if l[i] > 0: if l[i] not in use: a[l[i]] = 1 t += 1 use[l[i]] = 1 else: false = 1 break if l[i] < 0: if a[-l[i]] == 1: a[-l[i]] = 0 t -= 1 else: false = 1 break if t == 0: ans.append(i + 1) use = {} if t != 0: false = 1 if false == 1: print(-1) else: print(len(ans) - 1) for i in range(0, len(ans) - 1): print(ans[i + 1] - ans[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) ls = list(map(int, input().split())) office = 0 emp = [(0) for i in range(1000001)] emp2 = [] flag, index = 0, 1 days = 0 s1 = "" for i in ls: if i > 0: if emp[i] != 0: flag = 1 break else: emp[i] = 1 office += 1 else: i = -i if emp[i] != 1: flag = 1 break else: emp[i] = 2 office -= 1 emp2.append(i) if office == 0: days += 1 s1 += str(index) + " " index = 0 for j in emp2: emp[j] = 0 emp2 = [] index += 1 if flag == 1 or office > 0: print(-1) else: print(days) print(s1)
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 VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
def main(): n = int(input()) arr = list(map(int, input().split())) stack = set() used = set() res = [0] j = 0 for i in range(n): if arr[i] > 0: if arr[i] in used: print(-1) return stack.add(arr[i]) used.add(arr[i]) res[j] += 1 elif -arr[i] in stack: stack.remove(-arr[i]) res[j] += 1 if not stack: res.append(0) used = set() j += 1 else: print(-1) return if stack: print(-1) return else: if res[-1] == 0: res.pop() print(len(res)) print(*res) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys def silly_mistake(nums, l): entered_today = set() mem = set() d = 0 events = [] curlen = 0 for n in nums: curlen += 1 if n in entered_today: return -1, None if n > 0: mem.add(n) entered_today.add(n) elif -n in mem: mem.remove(-n) else: return -1, None if len(mem) == 0: d += 1 events.append(curlen) curlen = 0 entered_today = set() if curlen != 0: return -1, None return d, events def main(): l = int(sys.stdin.readline()) nums = [int(n) for n in sys.stdin.readline().split()] d, events = silly_mistake(nums, l) print(d) if d != -1: print(" ".join([str(e) for e in events])) main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER NONE IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER NONE IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER NONE RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) d = [0] * n c = 0 cu = 0 f = True s = set() day = set() for i in range(n): cu += 1 if a[i] > 0 and s == set() and day != set(): d[c] = cu - 1 c += 1 cu = 1 day = set() s.add(a[i]) day.add(a[i]) elif a[i] > 0 and a[i] not in day: s.add(a[i]) day.add(a[i]) elif a[i] > 0: f = False break elif a[i] < 0 and -a[i] not in s: f = False break else: s.remove(-a[i]) if s != set(): f = False d[c] = cu c += 1 cu = 0 if f: print(c) for i in range(c): print(d[i], end=" ") else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) s = set() b = set() now = 0 ans = [] while now < n: if a[now] > 0: if a[now] in s: print(-1) exit() else: s.add(a[now]) b.add(a[now]) elif -a[now] not in b: print(-1) exit() else: b.remove(-a[now]) if len(b) == 0: ans.append(now + 1) s = set() now += 1 if len(b) != 0: print(-1) exit() for i in range(len(ans) - 1, 0, -1): ans[i] -= ans[i - 1] print(len(ans)) print(" ".join(map(str, 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) ans = [] days = -1 counter = 0 set_in = set() set_out = set() for i in range(n): if a[i] > 0: if a[i] in set_in or -1 * a[i] in set_out: print(-1) exit(0) set_in.add(a[i]) if a[i] < 0: if a[i] in set_out or abs(a[i]) not in set_in: print(-1) exit(0) set_out.add(a[i]) set_in.remove(abs(a[i])) counter += 1 if len(set_in) == 0: days += 1 set_out = set() ans.append(counter) counter = 0 if len(set_in) != 0: print(-1) exit(0) print(days + 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 LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
states = [0] * 1000005 def solve(n, logs): cur = [] res = [] ans = 0 for i, val in enumerate(logs): person = abs(val) cur.append(person) if val > 0: if states[person] != 0: return False states[person] = 1 ans += 1 else: if states[person] != 1: return False states[person] = 2 ans -= 1 if ans == 0: res.append(len(cur)) for j in cur: states[j] = 0 del cur cur = [] if len(cur) != 0: return False print(len(res)) for i in res: print(i, end=" ") n = int(input()) logs = list(map(int, input().split())) ans = solve(n, logs) if ans == False: print(-1)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) arr = list(map(int, input().split())) res = [] cnt = 0 track = 0 ans = 0 neg = dict() pos = dict() np = 0 for i in arr: if i > 0: if pos.get(i) == None: pos[i] = 1 ans = ans + 1 cnt = cnt + 1 elif ans == 0: res.append(cnt) ans = 0 cnt = 0 track = track + 1 neg = dict() pos = dict() pos[i] = 1 ans = ans + 1 cnt = cnt + 1 else: np = 1 break elif neg.get(i) == None: if pos.get(-i) != None: neg[i] = 1 ans = ans - 1 cnt = cnt + 1 else: np = 1 break else: np = 1 if ans == 0: res.append(cnt) ans = 0 cnt = 0 track = track + 1 neg = dict() pos = dict() if np == 0 and ans == 0: res.append(cnt) res.remove(0) print(len(res)) print(*res) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NONE IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split(" "))) i = 0 ans = [] inv = False while i < n: if a[i] < 0: inv = True break else: start = i bal = {a[i]: 1} visits = set([a[i]]) while i < n and len(bal) > 0: i += 1 if i == n: break if a[i] > 0: if a[i] in bal: inv = True break elif a[i] in visits: inv = True break else: bal[a[i]] = 1 visits.add(a[i]) elif -a[i] not in bal: inv = True break else: bal.pop(-a[i]) ans.append(i - start + 1) if inv: break i += 1 if inv: print(-1) elif len(bal) > 0: print(-1) else: print(len(ans)) print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) s = set() ss = set() i = 0 seg = [] gud = True for e in map(int, input().split()): if e > 0: if e in ss: gud = False break s.add(e) ss.add(e) else: if -e not in s: gud = False break s.remove(-e) i += 1 if len(s) == 0: seg.append(i) ss = set() i = 0 if len(s) != 0: gud = False if not gud: print(-1) else: print(len(seg)) print(*seg)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) m = list(map(int, input().split())) if n % 2: print(-1) else: p = [] l = [0] d = {} k = 0 r = 0 for i in range(n): if m[i] < 0: if abs(m[i]) not in p: r = 1 break else: k += m[i] p.remove(abs(m[i])) elif m[i] in d: if d[m[i]] < l[-1] - 1: k += m[i] d[m[i]] = i p.append(m[i]) else: r = 1 break else: k += m[i] d[m[i]] = i p.append(m[i]) if k == 0: l.append(i + 1) if r == 1 or len(p) > 0: print(-1) else: print(len(l) - 1) p = [] for i in range(1, len(l)): p.append(l[i] - l[i - 1]) print(*p)
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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
d = {} I = input n = int(I()) a = list(map(int, I().split(" "))) days = 0 le = 0 cg = 0 events = [] try: for i in range(n): if a[i] < 0 and (abs(a[i]) not in d or abs(a[i]) in d and d[abs(a[i])] == 0): assert 1 == 0 elif a[i] > 0: if le == cg and le != 0: events.append(2 * le) days += 1 le = 1 cg = 0 d.clear() d[a[i]] = 1 elif a[i] not in d: d[a[i]] = 1 le += 1 else: assert 1 == 0 else: cg += 1 d[abs(a[i])] = 0 if le != cg: assert 1 == 0 elif a[n - 1] < 0: events.append(2 * le) days += 1 print(days) print(*events) except AssertionError: print("-1")
ASSIGN VAR DICT 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 STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) l = list(map(int, input().split())) mat = [] sumka = 0 stan = [0] * (10**6 + 1) dasie = True weszli = {} for i in range(n): if i > 0 and sumka == 0: weszli = {} mat.append(i) stan[abs(l[i])] += abs(l[i]) / l[i] try: weszli[l[i]] += 1 except Exception: weszli[l[i]] = 1 if stan[abs(l[i])] < 0: dasie = False break if weszli[l[i]] > 1: dasie = False break sumka += abs(l[i]) / l[i] if sumka != 0 or stan != [0] * (10**6 + 1): dasie = False if sumka == 0: mat.append(n) if dasie: a = [mat[0]] for i in range(1, len(mat)): a.append(mat[i] - mat[i - 1]) print(len(a)) print(*a) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
t = int(input()) a = list(map(int, input().split())) b = {0} c = {0} m = [] ans = 0 e = 0 for i in range(t): if a[i] in b: ans = -1 break if a[i] in c: ans = -1 break if a[i] < 0 and not -a[i] in b: ans = -1 break if a[i] > 0: e = e + 1 b.add(a[i]) else: c.add(-a[i]) if c == b: ans = ans + 1 m.append(e + e) e = 0 b = {0} c = {0} if t % 2: print("-1") elif ans == -1 or ans == 0 or sum(m) != t: print("-1") else: print(ans) for j in range(ans): print(m[j], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
def correct_days_split(a: list) -> bool: office = set() days_events = [] were_today = set() events = 0 for idx, el in enumerate(a): if el in were_today and office: return [] if el > 0: if el in office: return [] else: office.add(el) elif el < 0: if abs(el) not in office: return [] else: office.remove(abs(el)) were_today.add(abs(el)) events += 1 if not office and idx > 0: days_events.append(events) were_today.clear() events = 0 if office: return [] else: return days_events def main(): n = int(input()) a = list(map(int, input().split())) days_split = correct_days_split(a) if not days_split: print(-1) else: print(len(days_split)) print(" ".join(map(str, days_split))) main()
FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN LIST IF VAR NUMBER IF VAR VAR RETURN LIST EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR RETURN LIST RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) d = [int(i) for i in input().split()] fnd = set() in_o = set() ans = [] last_ind = -1 bad = False for i in range(n): dd = d[i] if dd > 0: if dd in fnd: bad = True break else: fnd.add(dd) in_o.add(dd) else: dd *= -1 if dd not in in_o: bad = True break else: in_o.remove(dd) if len(in_o) == 0: ans.append(i - last_ind) last_ind = i fnd = set() in_o = set() if bad or len(in_o) != 0: print(-1) else: print(len(ans)) print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) logs = list(map(int, input().split())) def calculate_days(logs): days = set() days_num = 0 events_num = 0 events_lst = [] recurr_days = set() output = 0 for i in logs: if len(days) == 0: days_num += 1 if events_num > 0: events_lst.append(events_num) events_num = 0 recurr_days = set() if i > 0: if i in days: output = -1 break elif i in recurr_days: output = -1 break else: days.add(i) events_num += 1 recurr_days.add(i) elif i < 0: if -i in days: days.remove(-i) events_num += 1 else: output = -1 break events_lst.append(events_num) if len(days) != 0: output = -1 if output == 0: return days_num, events_lst else: return output output = calculate_days(logs) if type(output) == tuple: events_lst = output[1] events = str(events_lst).strip("[").strip("]").replace(", ", " ") print(output[0]) print(events) else: print(output)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = [int(x) for x in input().split()] ans = 0 days = [] evs = 0 inside = {} for i in range(n): ans += a[i] if a[i] > 0: if a[i] in inside: print(-1) exit(0) continue inside[a[i]] = 1 evs += 1 if a[i] < 0 and -a[i] not in inside: print(-1) exit(0) continue if ans == 0: days.append(evs) evs = 0 inside.clear() if ans != 0: print(-1) exit(0) l = len(days) if l > 0: print(len(days)) print(" ".join([str(d) for d in days])) else: print(-1)
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 ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys ip = lambda: sys.stdin.readline().rstrip() n = int(ip()) a = list(map(int, ip().split())) ans = True ans1 = [] inside = set() left = set() p = 0 for i in range(n): if a[i] > 0: if a[i] in inside or a[i] in left: ans = False break inside.add(a[i]) else: if abs(a[i]) not in inside or abs(a[i]) in left: ans = False break inside.remove(abs(a[i])) left.add(abs(a[i])) if ans and len(inside) == 0: ans1.append(i + 1 - p) inside = set() left = set() p = i + 1 if len(inside) != 0: ans = False if ans: print(len(ans1)) print(*ans1) else: print(-1)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = [int(s) for s in input().split()] c = [] office = set() day = set() for i in range(n): if a[i] > 0: if a[i] in day: print(-1) break office.add(a[i]) day.add(a[i]) else: if -a[i] not in office: print(-1) break office.remove(-a[i]) if len(office) == 0: c.append(2 * len(day)) day = set() else: if len(office) == 0: print(len(c)) print(" ".join([str(i) for i in c])) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) e = list(map(int, input().split())) days = [] events = 0 count = 0 inOffice = set() entered = set() flag = True days = [] for i in e: if i > 0: if i in entered: flag = False break else: entered.add(i) inOffice.add(i) events += 1 else: temp = -1 * i if temp in inOffice: inOffice.discard(temp) events += 1 if len(inOffice) == 0: count += 1 days.append(events) events = 0 inOffice = set() entered = set() else: flag = False break if flag and len(inOffice) == 0: print(count) print(*days) else: print("-1")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING