description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) s = input() ans = 0 cnt = 1 for i in range(1, n): if s[i] == s[i - 1]: ans += 1 ans = min(ans, cnt) else: cnt += 1 ans += (cnt - ans + 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from sys import stdin casos = int(stdin.readline().strip()) c = 1 for caso in range(casos): c += 1 n = int(stdin.readline().strip()) s = stdin.readline().strip() n = len(s) d = 0 l = 0 ans = 0 for i in range(n): ans += 1 d += 1 res = n - (l + 1) if d >= res: break y = l + 1 while y < n and s[y] == s[l] and d > 0: d -= 1 y += 1 y = l while y < n and s[y] == s[l]: y += 1 l = y if l == n: break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for case in range(t): n = int(input()) s = input() i = 0 x = [] while i < n: cnt = 1 while i + cnt < n and s[i + cnt] == s[i]: cnt += 1 x.append(cnt) i += cnt i = 0 j = 0 ops = 0 while x: f = False while i < len(x): if x[i] > 1: x[i] -= 1 ops += 1 f = True break else: i += 1 if not f: del x[-1] ops += 1 if len(x) > 0: del x[0] i = max(i - 1, 0) print(ops)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for i in range(t): n = int(input()) arr = input() delcnt = 1 actual = 0 for j in range(0, n - 1): if arr[j] == arr[j + 1]: if delcnt: actual += 1 delcnt -= 1 else: delcnt += 1 if delcnt % 2: actual += delcnt // 2 + 1 else: actual += delcnt // 2 print(actual)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) ar = list(input()) li = [] count = 1 pre = ar[0] for i in range(1, n): if ar[i] == pre: count += 1 else: li.append(count) count = 1 pre = ar[i] if count != 0: li.append(count) ans = 0 one = 0 for i in range(len(li)): if li[i] == 1: one += 1 elif li[i] > 2: ext = li[i] - 2 if ext >= one: ans += one + 1 one = 0 else: ans += ext + 1 one -= ext else: ans += 1 if one % 2 == 0: ans += one // 2 else: ans += one // 2 + 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) s = list(input()) rd = 0 id = 1 for i in range(1, n): if s[i] == s[i - 1]: rd = min(rd + 1, id) else: id += 1 print(rd + (id - rd + 1) // 2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for i in range(int(input())): x = input() s = input() a = [] last = "" k = 0 for i in s: if i == last: k += 1 else: last = i a.append(k) k = 1 if k > 0: a.append(k) if len(a) <= 2: print(1) else: i = 1 j = 1 ss = sum(a) while j < len(a) and a[j] == 1: j += 1 k = 1 if j == len(a): j -= 1 k = -1 ans = 0 while ss > 0: if k == -1: while a[j] == 0: j -= 1 a[j] -= 1 ss -= 1 if ss > 0: while a[i] == 0: i += 1 ss -= a[i] a[i] = 0 else: while j < len(a) and a[j] <= 1: j += 1 if j == len(a): j -= 1 k = -1 a[j] -= 1 ss -= 1 if ss > 0: while a[i] == 0: i += 1 ss -= a[i] a[i] = 0 ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
z = input o = [] for _ in range(int(z())): n = int(z()) a = [] p = "" for i in z().strip(): if i == p: a[-1] += 1 else: a += [1] p = i a = a[::-1] r = 0 i = len(a) - 1 while a: while i and a[i] < 2: i -= 1 if i < 1 and a[i] < 2: break a[i] -= 1 a.pop() r += 1 if i >= len(a): i -= 1 r += (len(a) + 1) // 2 o += [str(r)] print("\n".join(o))
ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR NUMBER NUMBER VAR LIST NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for t in range(int(input())): n = int(input()) s = input() dp = [(0) for i in range(n)] temp = s[0] j = cnt = 0 i = 1 if n == 1: cnt = 1 while i < n: while i < n and dp[i]: i += 1 if i >= n: cnt += 1 break if s[i] == temp: i += 1 while i < n and dp[i]: i += 1 while i < n and (s[i] == temp or dp[i]): i += 1 else: while 1: if j < i: j = i while j < n and dp[j]: j += 1 k = j + 1 while k < n and dp[k]: k += 1 if j >= n or k >= n: i = i + 1 break t1 = s[j] t2 = s[k] if t1 == t2: dp[j] = 1 j = k break j += 1 while i < n and dp[i]: i += 1 if i < n: temp = s[i] dp[i] = 1 cnt += 1 print(cnt)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for __ in range(int(input())): n = int(input()) ar = list(map(int, input())) num = 1 kek = [] for i in range(1, n): if ar[i] != ar[i - 1]: kek.append(num) num = 1 else: num += 1 kek.append(num) i = 0 j = 0 ans = len(kek) lol = len(kek) while i < lol: while j < len(kek) and kek[j] == 1: j += 1 if j < len(kek): kek[j] -= 1 else: lol -= 1 lol = max(i + 1, lol) i += 1 j = max(j, i) ans -= len(kek) - lol print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for _ in range(t): n = int(input()) s = input() if n == 1: print(1) continue ans = 0 partition = list() start = 0 for end in range(1, n): if s[end] != s[start]: size = end - start partition.append(size) start = end size = n - start partition.append(size) left = right = 0 length = len(partition) ans = 0 while left < length: while right < length and partition[right] == 1: right += 1 if right != length and partition[right] > 1: partition[right] -= 1 left += 1 if left == right + 1: right += 1 elif right == length: left += 2 ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for case in range(t): n = int(input()) s = input() groups = [s[0]] for x in s[1:]: if x == groups[-1][-1]: groups[-1] += x else: groups.append(x) groups = [len(x) for x in groups] to_use = 0 ops = 0 for i, x in enumerate(groups): while to_use < len(groups): if to_use < i: to_use += 1 continue if groups[to_use] <= 1: to_use += 1 continue break else: break groups[to_use] -= 1 groups[i] = 0 ops += 1 else: print(ops) continue size = len(groups) - i ops += (size + 1) // 2 print(ops)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for i in range(t): ls = int(input()) s = input() c = 0 l = [] cnt = 1 for j in range(1, ls): if s[j] == s[j - 1]: cnt += 1 else: l.append(cnt) cnt = 1 l.append(cnt) j = 0 while j < len(l) and l[j] == 1: j += 1 ans = 0 for x in range(len(l)): if j == len(l): ans += (len(l) - x + 1) // 2 break else: l[j] -= 1 ans += 1 if l[j] == 1 or j == x: j = max(j + 1, x + 1) while j < len(l) and l[j] == 1: j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def solution(): n = int(input()) s = input() l = [] i = 0 while i < n: j = i while i < n and s[i] == s[j]: i += 1 l.append(i - j) d = 0 pos = [] for i in range(len(l)): d += l[i] - 1 if l[i] - 1 > 0: pos.append(i) pos.reverse() ans = 0 i = 0 x = len(l) while i < x: if pos: ans += 1 if l[i] - 1 > 0: pos.pop() else: y = pos.pop() l[y] -= 1 if l[y] - 1 > 0: pos.append(y) else: ans += (x - i) // 2 + (x - i) % 2 break i += 1 print(ans) t = int(input()) for _ in range(t): solution()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for you in range(t): n = int(input()) s = input() l = [] curr = "2" for i in range(n): if s[i] != curr: l.append(1) curr = s[i] else: l[-1] += 1 z = len(l) ans = 0 curr = 0 for i in range(z): curr = min(curr + l[i] - 1, i + 1) ans = curr + (z - curr + 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
class Solution: def __init__(self, n, str): self.ans = 0 self.n = n self.str = str self.cnt = [] for idx, s in enumerate(self.str): if idx == 0 or self.str[idx - 1] != s: self.cnt.append(1) else: self.cnt[-1] += 1 def solve(self): pos = -1 for idx, l in enumerate(self.cnt): if l != 1: pos = idx break if pos == -1: return (len(self.cnt) + 1) // 2 start = 0 while pos < len(self.cnt): if self.cnt[pos] == 1: while True: pos += 1 if pos == len(self.cnt): break if self.cnt[pos] != 1: break if pos == len(self.cnt): break start += 1 self.cnt[pos] -= 1 self.ans += 1 pos = max(start, pos) return self.ans + (len(self.cnt) - start + 1) // 2 t = int(input()) for _ in range(t): n = int(input()) str = input() s = Solution(n, str) print(s.solve())
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for case in range(t): n = int(input()) s = input() arr = [] if n == 1: print(1) continue count = 0 for i in range(n): if i == 0: count += 1 elif i < n - 1: if s[i] == s[i - 1]: count += 1 else: arr.append(count) count = 1 elif s[i] == s[i - 1]: arr.append(count + 1) else: arr.append(count) arr.append(1) ans = 0 i = 0 j = 0 m = len(arr) while i < m: if arr[i] > 1: ans += 1 i += 1 else: while j < m and arr[j] <= 1: j += 1 if j == m: ans += (j - i + 1) // 2 break else: arr[j] -= 1 i += 1 ans += 1 if i > j: j = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
T = int(input()) for _ in range(0, T): n = int(input()) s = input() many = 0 c = 1 tempp = 1 st = "" k1 = 0 k2 = 0 for i in range(len(s) - 2, -1, -1): if s[i] != s[i + 1]: if tempp == 0: tempp = 1 many += c - 1 k2 += 1 elif c >= 2: many += c - 2 k1 += 1 elif many > 0: many -= 1 k1 += 1 else: k2 += 1 c = 1 else: c += 1 if c >= 2: many += c - 2 k1 += 1 elif many > 0: many -= 1 k1 += 1 else: k2 += 1 ans = k1 + k2 // 2 if k2 % 2 != 0: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in " " * int(input()): a = int(input()) z = [len(i) for i in input().replace("10", "1 0").replace("01", "0 1").split()] + [ 0 ] ans = 0 i = 0 j = len(z) - 1 f = 0 while i < j: f = max(f, i) if z[i] > 1: i += 1 else: while f < j and z[f] == 1: f += 1 if z[f] > 1: z[f] -= 1 i += 1 else: i += 2 ans += 1 print(ans)
FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING STRING STRING LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from sys import stdin, stdout t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) s = stdin.readline() s1 = [] i = 0 ind = [] while i < n: j = i + 1 while j < n and s[j] == s[i]: j += 1 s1.append([int(s[i]), j - i]) if j - i >= 3: ind.append(len(s1) - 1) i = j op = 0 for i in range(len(s1)): if s1[i][1] == 0: continue if s1[i][1] == 1: op += 1 while len(ind) != 0 and ind[0] < i: ind.pop(0) if len(ind) != 0: s1[ind[0]][1] -= 1 if s1[ind[0]][1] == 2: ind.pop(0) elif i + 1 < len(s1): s1[i + 1][1] -= 1 else: op += 1 print(op)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys input = sys.stdin.readline t = int(input()) for i in range(t): n = int(input()) s = input().rstrip() changes = 1 spare = 0 before = s[0] spare_can = 1 for j in range(n - 1): if s[j + 1] == before: if spare_can > 0: spare_can -= 1 spare += 1 else: before = s[j + 1] changes += 1 spare_can += 1 ans = 0 ans += spare changes -= spare ans += (changes + 1) // 2 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
gans = [] for _ in range(int(input())): n = int(input()) a = list(map(int, list(input()))) u = [] k = 1 for i in range(1, n): if a[i] == a[i - 1]: k += 1 else: u.append(k) k = 1 u.append(k) dop = 0 ln = len(u) for i in range(ln): dop += u[i] - 1 cur = 0 ind = 0 while ind < ln: if dop == 0: ln -= 1 else: cur += 1 dop -= 1 cnt = u[ind] - 1 if cur < cnt: dop -= cnt - cur cur = 0 else: cur -= cnt ind += 1 gans.append(ind) print("\n".join(map(str, gans)))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for _ in range(t): n = int(input()) s = input() conts = [] last = None for c in s: if c == last: conts[-1] += 1 else: conts.append(1) last = c before = 0 ops = 0 for i, cont in enumerate(conts): before += 1 if cont > 1: _ops = min(before, cont - 1) before -= _ops ops += _ops print(ops + (before + 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR VAR IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def find_next_scylla_target(scylla, n, s): for i in range(scylla + 1, n): if s[i] == s[i - 1]: return i return -1 def find_next_charybdis_target(charybdis, n, s): current = s[charybdis] for i in range(charybdis + 1, n): if s[i] != current: return i return -1 def find_last_group(n, s): for i in range(n - 2, -1, -1): if s[i] != s[i + 1]: return i return n - 1 def main(): t = int(input()) for _ in range(t): n = int(input()) s = list(input()) scylla = charybdis = operations = 0 last_group = find_last_group(n, s) scylla_l = [] scylla_z = 1 while True: operations += 1 if scylla == -1: while scylla_z <= len(scylla_l) and scylla_l[-scylla_z] == n - 1: scylla_z += 1 n -= 1 n -= 1 else: next_scylla = find_next_scylla_target(scylla, n, s) if next_scylla == -1 or next_scylla > last_group: scylla = -1 n -= 1 else: scylla_l.append(next_scylla) scylla = next_scylla next_charybdis = find_next_charybdis_target(charybdis, n, s) if next_charybdis == -1: break else: charybdis = next_charybdis if charybdis > scylla and scylla != -1: scylla = charybdis print(operations) main()
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys as _sys ZERO = "0" ONE = "1" def main(): t = int(input()) for i_t in range(t): (n,) = _read_ints() s = _read_line() result = find_max_operations_n_can_make(s) print(result) def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split()) def find_max_operations_n_can_make(s): assert isinstance(s, str) islands_lengths = [] curr_length = 1 for prev_x, curr_x in zip(s, s[1:]): if curr_x == prev_x: curr_length += 1 else: islands_lengths.append(curr_length) curr_length = 1 islands_lengths.append(curr_length) islands_n = len(islands_lengths) islands_powers = [(length - 1) for length in islands_lengths] indexed_nonzero_islands_powers_rev = [ [i, power] for i, power in enumerate(islands_powers) if power != 0 ][::-1] i_next_island_to_eat = 0 while indexed_nonzero_islands_powers_rev: indexed_nonzero_islands_powers_rev[-1][1] -= 1 if indexed_nonzero_islands_powers_rev[-1][1] == 0: indexed_nonzero_islands_powers_rev.pop() while ( indexed_nonzero_islands_powers_rev and indexed_nonzero_islands_powers_rev[-1][0] <= i_next_island_to_eat ): indexed_nonzero_islands_powers_rev.pop() i_next_island_to_eat += 1 islands_eaten = i_next_island_to_eat assert islands_eaten <= islands_n one_elem_islands_remain = islands_n - islands_eaten moves_used = islands_eaten moves_used += one_elem_islands_remain // 2 + one_elem_islands_remain % 2 return moves_used main()
IMPORT ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) s = str(input()) one, zero = 0, 0 l = [] for i in range(n): if s[i] == "1": if zero != 0: l.append(zero) zero = 0 one += 1 elif s[i] == "0": if one != 0: l.append(one) one = 0 zero += 1 if one != 0: l.append(one) if zero != 0: l.append(zero) ans = 0 loan = 0 m = len(l) for i in range(m): if l[i] >= 2: if loan: ans += min(l[i] - 2, loan) loan -= min(l[i] - 2, loan) ans += 1 elif l[i] == 1: loan += 1 print(ans + (loan + 1) // 2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) pref, last = [], "" for i in input(): if i != last: pref.append(1) last = i else: pref[-1] += 1 ans = 0 i, curr = 0, 0 while i < len(pref): curr = max(curr, i) if pref[i] < 2: while curr < len(pref) - 1 and pref[curr] == 1: curr += 1 if pref[curr] < 2: i += 1 else: pref[curr] -= 1 i += 1 ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST STRING FOR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) s = input() a = [] curr = 1 g = 0 for i in range(1, n): if s[i] == s[i - 1]: curr += 1 else: a.append(curr) if curr > 1: g += 1 curr = 1 if curr > 0: if curr > 1: g += 1 a.append(curr) j = 0 i = 0 res = 0 while i < len(a): if a[i] > 1: res += 1 i += 1 else: j = max(i + 1, j) ch = 0 while j < len(a): if a[j] > 1: a[j] -= 1 ch = 1 break j += 1 if ch == 1: i += 1 res += 1 else: i += 2 res += 1 print(res)
FOR VAR FUNC_CALL 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 NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
T = int(input()) for _ in range(0, T): n = int(input()) s = input() now = 0 dq = list() while now < n: x = s[now] cnt = 0 while now < n and s[now] == x: cnt = cnt + 1 now = now + 1 dq.append(cnt) deleted = 0 ans = 0 for cnt2 in dq: if cnt2 == 1: deleted = deleted + 1 elif cnt2 > 2: deleted = deleted - min(deleted, cnt2 - 2) ans = ans + 1 print(ans - deleted // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for t in range(int(input())): n = int(input()) s = input().strip() i = 0 l = [] while i < n: j = i while j < n - 1: if s[j] != s[j + 1]: break j += 1 l.append(j - i + 1) i = j + 1 m = len(l) i = 0 j = 0 ans = 0 while i < m: if l[i] > 1: ans += 1 i += 1 else: j = max(j, i + 1) while j < m: if l[j] > 1: break j += 1 if j == m: ans += (j - i) // 2 + (j - i) % 2 i = m break while l[j] > 1 and l[i] == 1: i += 1 l[j] -= 1 ans += 1 if i == m: break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def answer(n, A): count = 1 dp = [] for i in range(0, n - 1): if A[i] == A[i + 1]: count += 1 else: dp.append(count) count = 1 dp.append(count) i = 0 j = 0 count = 0 while i < len(dp): if dp[i] > 1: dp[i] = 0 count += 1 i += 1 else: j = max(i + 1, j) while j < len(dp) and dp[j] == 1: j += 1 if j == len(dp): count += (j - i + 1) // 2 break else: dp[j] -= 1 count += 1 i += 1 return count t = int(input()) for i in range(t): n = int(input()) A = input() print(answer(n, A))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) s = list(input()) groups = [] last = "" cnt = 0 for c in s: if c != last: if cnt: groups.append(cnt) cnt = 1 else: cnt += 1 last = c if cnt: groups.append(cnt) m = len(groups) i = 0 j = 0 ops = 0 while i < m: ops += 1 while j < i or j < m and groups[j] == 1: j += 1 if j < m: groups[j] -= 1 else: i += 1 i += 1 print(ops)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) while t > 0: t -= 1 n = int(input()) string = input() if n == 1: print(1) continue arr = [] count = 1 for i in range(n - 1): if string[i] == string[i + 1]: count += 1 else: arr.append(count) count = 1 if string[-1] == string[-2]: arr.append(count) else: arr.append(1) prefix = 0 waste = 0 sum = 0 n = len(arr) while waste < n: if arr[waste] != 1: arr[waste] -= 1 sum += 1 prefix += 1 if prefix > waste: waste = prefix else: waste += 1 while prefix < n: prefix += 2 sum += 1 print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from itertools import groupby t = int(input()) for _ in range(t): n = int(input()) s = list(input()) l, r = 0, n - 1 result = 0 runEncoding = [len(list(group)) for _, group in groupby(s)] multiIndices = [i for i in range(len(runEncoding)) if runEncoding[i] > 1] index = 0 mIndex = 0 mExists = True if len(multiIndices) > 0 else False end = len(runEncoding) - 1 while index <= end: if mExists and index == multiIndices[mIndex]: mIndex += 1 if mIndex >= len(multiIndices): mExists = False if runEncoding[index] == 1: if not mExists: end -= 1 else: runEncoding[multiIndices[mIndex]] -= 1 if runEncoding[multiIndices[mIndex]] == 1: mIndex += 1 if mIndex >= len(multiIndices): mExists = False result += 1 index += 1 print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from sys import stdin, stdout input = stdin.readline t = int(input()) for _ in range(t): n = int(input()) s = input()[:-1] arr = [] val = s[0] c = 0 for i in s: if i == val: c += 1 else: arr.append(c) c = 1 val = i if c > 0: arr.append(c) ans = 0 q = [] for i in range(len(arr)): if arr[i] != 1: q.append(i) k = 0 while 1: if arr == []: break if arr[0] > 1: ans += 1 q.pop(0) arr.pop(0) else: if len(q) > 0: arr[q[0] - k] -= 1 if arr[q[0] - k] == 1: q.pop(0) ans += 1 else: ans += len(arr) // 2 + len(arr) % 2 break arr.pop(0) k += 1 stdout.write(str(ans) + "\n")
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR LIST IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def ans(s, n): temp = 1 curr = s[0] seq = [] i = 1 while i < n: if s[i] == curr: temp += 1 else: seq.append(temp) temp = 1 curr = s[i] i += 1 seq.append(temp) answer = 0 if len(seq) == 1: return 1 j = 0 while j < len(seq) and seq[j] == 1: j += 1 if j == len(seq): if len(seq) % 2: return len(seq) // 2 + 1 else: return len(seq) // 2 i = 0 while i < len(seq) and j < len(seq): if i == j: answer += 1 i += 1 j += 1 while j < len(seq) and seq[j] == 1: j += 1 if j == len(seq): break else: answer += 1 i += 1 seq[j] -= 1 while j < len(seq) and seq[j] == 1: j += 1 if j == len(seq): break x = len(seq) - i if x % 2: answer += x // 2 + 1 else: answer += x // 2 return answer m = int(input()) for j in range(m): n = int(input()) s = input() print(ans(s, n))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
T = int(input()) for t in range(T): n = int(input()) s = input() A = [] prev = "2" count = 0 for i in range(n): if s[i] == prev: count += 1 else: if count > 0: A.append(count) prev = s[i] count = 1 A.append(count) de = 0 for i in range(len(A)): if A[i] == 1: de += 1 elif A[i] > 2: r = A[i] - 2 de = max(de - r, 0) print(len(A) - de // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys from itertools import groupby input = sys.stdin.readline def solveD(s): cnt = 0 xs = [len(list(g)) for k, g in groupby(s)] ones = 0 can_save = 0 for x in xs: if x == 1: ones += 1 elif ones == 0: cnt += 1 elif ones == 1: ones -= 1 cnt += 1 x -= 1 if x > 1: cnt += 1 else: ones += 1 else: while ones and x > 1: ones -= 1 cnt += 1 x -= 1 if x == 1: ones += 1 else: cnt += 1 cnt += (ones + 1) // 2 return cnt t = int(input()) for _ in range(t): input() s = input().strip() print(solveD(s))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for p in range(t): n = int(input()) s = input() b = [] for j in s: b.append(int(j)) ind = [] j = 0 while j < n: if b[j] == 0: if j > 0 and b[j - 1] != 0: ind.append(l) l = 1 elif j > 0 and b[j - 1] == 0: l += 1 elif j == 0: l = 1 elif j > 0 and b[j - 1] != 1: ind.append(l) l = 1 elif j > 0 and b[j - 1] == 1: l += 1 elif j == 0: l = 1 j += 1 ind.append(l) j = 0 i = 0 ans = 0 l = len(ind) while j < l: i = max(i, j) while i < l: if ind[i] > 1: break i += 1 if i == l: ans += 1 j += 2 else: ans += 1 ind[i] -= 1 j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from itertools import groupby from sys import stdin, stdout input = stdin.readline mp = lambda: map(int, input().split()) it = lambda: int(input()) test = it() for i in range(test): a = it() b = input().strip() l = [] ind = [] for k, g in groupby(b): l.append(len(list(g))) if l[-1] > 1: ind.append(len(l) - 1) st = 0 ans = 0 st_ind = 0 if ind == []: print((a + 1) // 2) continue while st < len(l): if l[st] == 1: index = None for j in range(st_ind, len(ind)): if ind[j] >= st and l[ind[j]] > 1: index = ind[j] st_ind = j break if index == None: st += 1 else: l[index] -= 1 ans += 1 st += 1 print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR LIST EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NONE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for t in range(int(input())): n = int(input()) s = input() if n < 3: print(1) else: a = [] b = [] c = 1 j = 0 for i in range(1, n): if s[i] == s[i - 1]: c += 1 else: a.append(c) if c > 1: b.append(j) c = 1 j += 1 a.append(c) if c > 1: b.append(j) la = len(a) lb = len(b) if lb == 0: print((la + 1) // 2) else: ia = ib = r = 0 while ia < la: r += 1 if a[ia] == 1: a[b[ib]] -= 1 if a[b[ib]] == 1: ib += 1 if ib >= lb: r += (la - ia) // 2 break else: ib += 1 if ib >= lb: r += (la - ia) // 2 break ia += 1 print(r)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline for _ in range(int(input())): N = int(input()) S = input().rstrip("\n") S = [int(s) for s in S[::-1]] A = [] prev = S[0] cnt = 0 for s in S: if s == prev: cnt += 1 else: A.append(cnt) cnt = 1 prev ^= 1 A.append(cnt) ans = 0 i = len(A) - 1 while A: ans += 1 if i >= 0: while True: if A[i] > 1: A[i] -= 1 break else: i -= 1 if i < 0: A.pop() break else: A.pop() if A: A.pop() if i == len(A): i -= 1 print(ans) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER IF VAR NUMBER WHILE NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n = int(input()) S = input().strip() L = [1] for i in range(1, n): if S[i] == S[i - 1]: L[-1] += 1 else: L.append(1) de = 0 i = 0 ANS = 0 LEN = len(L) flag = 0 while de < LEN: if flag == 0: i = max(i, de) while i < LEN: if L[i] > 1: break else: i += 1 if i == LEN: flag = 1 else: L[i] -= 1 if flag == 0: de += 1 ANS += 1 else: de += 2 ANS += 1 print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from itertools import groupby for _ in [0] * int(input()): n = int(input()) s = input() c = [len(list(x[1])) for x in groupby(s)] m = len(c) dup_idx = [] for i, j in enumerate(c): if j > 1: dup_idx.append(i) dup_idx.reverse() curr = 0 while dup_idx: i = dup_idx[-1] if i < curr: dup_idx.pop() continue c[i] -= 1 if c[i] == 1: dup_idx.pop() curr += 1 ans = curr + (m - curr + 1) // 2 print(ans)
FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def main(): n = int(input()) line = input() turn_take = [] prev = line[-1] can_be = 0 for i in range(n - 2, -1, -1): if line[i] == prev: can_be += 1 else: prev = line[i] turn_take.append(can_be) turn_take.append(can_be) turns = len(turn_take) taken = 0 res = 0 for i in range(1, turns + 1): if i > 1 and turn_take[-i] < turn_take[-i + 1]: taken = min(0, taken + turn_take[-i + 1] - turn_take[-i]) if turn_take[-i] > -taken: taken -= 1 res += 1 else: res += (turns - i + 1) // 2 if (turns - i + 1) % 2 != 0: res += 1 break print(res) def __starting_point(): t = int(input()) for i in range(t): main() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def parseString(string): cur = string[0] subLen = 0 ans = [] for ele in string: if ele == cur: subLen += 1 else: ans.append(subLen) subLen = 1 cur = ele ans.append(subLen) return ans def reduceNext(curIndex, lst): for i in range(curIndex, len(lst)): if lst[i] > 1: lst[i] -= 1 return i lst.pop(len(lst) - 1) return len(lst) - 1 def main(string): subList = parseString(string) reducer = 0 deleter = 0 counter = 0 while subList[-1] != 0: reducer = reduceNext(reducer, subList) counter += 1 try: subList[deleter] = 0 except IndexError: break deleter += 1 return counter tc = int(input()) for _ in range(tc): length = int(input()) string = str(input()) print(main(string))
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) s = input().rstrip() comp = [] s = [s[-i - 1] for i in range(n)] bi = s.pop() L = 1 while s: if s[-1] == bi: L += 1 s.pop() else: comp.append(L) L = 1 bi = s.pop() comp.append(L) a = 0 b = 0 res = 0 while a < len(comp): if comp[a] != 1: comp[a] = 0 res += 1 a += 1 else: while b < len(comp): if comp[b] >= 2: break else: b += 1 if b == len(comp): comp[a] = 0 if a + 1 < len(comp): comp[a + 1] = 0 res += 1 a += 2 else: res += 1 a += 1 else: comp[b] -= 1 res += 1 comp[a] = 0 a += 1 print(res)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for case in range(1, t + 1): n = int(input()) s = input() ranges = [] last_char = s[0] last_amount = 1 for i in range(1, n): if s[i] != last_char: ranges.append(last_amount) last_char = s[i] last_amount = 0 last_amount += 1 ranges.append(last_amount) moves = 0 pointer = 0 best_non_1 = 0 while pointer < len(ranges): found = False while best_non_1 < len(ranges): if ranges[best_non_1] > 1: moves += 1 ranges[best_non_1] -= 1 pointer += 1 if best_non_1 < pointer: best_non_1 = pointer found = True break best_non_1 += 1 if not found: moves += (len(ranges) - pointer + 1) // 2 break print(moves)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from sys import stdin, stdout input = stdin.readline print = lambda x: stdout.write(str(x) + "\n") for _ in range(int(input())): n = int(input()) s = input().strip() p = [] o, z = 0, 0 for i in s: if i == "1": if z: p.append(z) z = 0 o += 1 else: if o: p.append(o) o = 0 z += 1 if z: p.append(z) if o: p.append(o) ans = 0 j = 0 f = 1 i = 0 while i < len(p): while i < len(p) and p[i] == 0: i += 1 if i == len(p): break if p[i] > 1: ans += 1 i += 1 continue elif f: j = max(i + 1, j) while j < len(p) and p[j] < 2: j += 1 if j != len(p): p[j] -= 1 ans += 1 i += 1 continue f = 0 ones = 0 for j in p[i:]: if j == 1: ones += 1 ans += (ones + 1) // 2 break print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for _ in range(t): n = int(input()) s = input() paq = [] l = 1 last_seen = s[0] for i in range(1, len(s)): if s[i] == last_seen: l += 1 else: paq.append(l) l = 1 last_seen = s[i] paq.append(l) vie = 0 num = 0 reste = len(paq) next_greater = 0 while reste > 0: if next_greater < len(paq) and (next_greater < vie or paq[next_greater] <= 1): j = max(next_greater, vie) while j < len(paq) and paq[j] <= 1: j += 1 next_greater = j i = next_greater if i == len(paq): vie += 2 reste -= 2 else: paq[i] -= 1 reste -= 1 vie += 1 num += 1 print(num)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys def eprint(*args): print(*args, file=sys.stderr) zz = 1 if zz: input = sys.stdin.readline else: sys.stdin = open("input.txt", "r") sys.stdout = open("all.txt", "w") di = [[-1, 0], [1, 0], [0, 1], [0, -1]] def string(s): return "".join(s) def fori(n): return [fi() for i in range(n)] def inc(d, c, x=1): d[c] = d[c] + x if c in d else x def bo(i): return ord(i) - ord("A") def li(): return [int(xx) for xx in input().split()] def fli(): return [float(x) for x in input().split()] def comp(a, b): if a > b: return 2 return 2 if a == b else 0 def gi(): return [xx for xx in input().split()] def cil(n, m): return n // m + int(n % m > 0) def fi(): return int(input()) def pro(a): return reduce(lambda a, b: a * b, a) def swap(a, i, j): a[i], a[j] = a[j], a[i] def si(): return list(input().rstrip()) def mi(): return map(int, input().split()) def gh(): sys.stdout.flush() def isvalid(i, j): return 0 <= i < n and 0 <= j < m and a[i][j] != "." def bo(i): return ord(i) - ord("a") def graph(n, m): for i in range(m): x, y = mi() a[x].append(y) a[y].append(x) t = fi() while t > 0: t -= 1 n = fi() s = si() c = 0 a = [] no = -1 for i in range(n - 1): if s[i] != s[i + 1]: if c > 0 and no == -1: no = len(a) a.append(c + 1) c = 0 else: c += 1 a.append(c + 1) if no == -1 and c > 0: no = len(a) - 1 op = i = 0 while i < len(a): if a[i] == 1: if no < i: no = i while no < len(a) and a[no] == 1: no += 1 if no != len(a) and no >= i: a[no] -= 1 if a[no] == 1: while no < len(a) and a[no] == 1: no += 1 i += 1 op += 1 else: i += 2 op += 1 else: i += 1 op += 1 print(op)
IMPORT FUNC_DEF EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL STRING VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR STRING FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
n = int(input()) for i in range(n): f = int(input()) s = str(input()) ans = 0 bor = 0 cnt = 1 for j in range(1, f): if s[j - 1] == s[j]: bor += 1 bor = min(bor, cnt) else: cnt += 1 print(bor + (cnt - bor + 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def find_next_index(s): for pos in range(1, len(s)): if s[pos] == s[pos - 1]: return pos return len(s) - 1 def get_prefix_len(s): assert len(s) > 0 if len(s) == 1: return 1 pos = 1 while pos < len(s) and s[pos] == s[0]: pos += 1 return pos def count_max_actions_old(s): count = 0 while len(s) > 0: count += 1 j = find_next_index(s) s = s[0:j] + s[j + 1 :] if len(s) == 0: break prefix_len = get_prefix_len(s) s = s[prefix_len:] return count def find_next_index_rle(i0, max_i, rle): for i in range(i0, max_i + 1): if rle[i] > 1: return i return max_i def get_rle(s): assert len(s) > 0 if len(s) == 1: return [1] rle = [] prev_pos = 0 pos = 1 while pos < len(s): if s[pos] != s[prev_pos]: rle.append(pos - prev_pos) prev_pos = pos pos += 1 rle.append(pos - prev_pos) return rle def count_max_actions(s): count = 0 rle = get_rle(s) start_index = 0 end_index = len(rle) - 1 dec_index = 0 while end_index >= start_index: count += 1 dec_index = find_next_index_rle(dec_index, end_index, rle) rle[dec_index] -= 1 if rle[dec_index] == 0: end_index = end_index - 1 dec_index -= 1 if end_index < start_index: break start_index += 1 if dec_index < start_index: dec_index = start_index return count n = int(input()) for t in range(n): m = int(input()) s = input() assert len(s) == m result = count_max_actions(s) print(result)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN LIST NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys input = sys.stdin.buffer.readline t = int(input()) for _ in range(t): n = int(input()) s = input() a = [1] for i in range(1, n): if s[i] == s[i - 1]: a[-1] += 1 else: a.append(1) la = len(a) ans = 0 for i in range(la): ans = min(ans + a[i] - 1, i + 1) ans = ans + (la - ans + 1) // 2 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): siz = int(input()) st = input() diff = 0 q = [] for x in range(1, siz): if st[x] != st[x - 1]: diff += 1 if st[x] == st[x - 1]: q.append(diff) q.reverse() rem, ans = 0, 0 for x in range(siz): if not q: break q.pop(-1) rem += 1 ans += 1 while q and q[-1] == x: q.pop(-1) rem += 1 rem += 1 print(ans + (siz - rem + 1) // 2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def string_deletion(): test_cases = int(input()) for t in range(test_cases): n = int(input()) string = input() dup_count = 0 dup = [0] op_count = 1 op = [1] for x in range(1, len(string)): remaining = len(string) - len(op) dup_available = op[x - 1] - dup[x - 1] if remaining <= dup_available: break if string[x] != string[x - 1]: op_count += 1 else: dup_count += 1 dup_count = min(dup_count, op_count) op += [op_count] dup += [dup_count] print(op[len(op) - 1]) string_deletion()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR LIST VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from sys import stdin stdin.readline def mp(): return list(map(int, stdin.readline().strip().split())) def it(): return int(stdin.readline().strip()) for _ in range(it()): n = it() s = input() cur, v = 1, [] for i in range(1, n): if s[i - 1] == s[i]: cur += 1 else: v.append(cur) cur = 1 if cur > 0: v.append(cur) i, j, ans = 0, 0, 0 while i < len(v): if v[i] > 1: ans += 1 i += 1 else: j = max(i + 1, j) ch = 0 while j < len(v): if v[j] > 1: v[j] -= 1 ch = 1 break j += 1 if ch == 1: i += 1 ans += 1 else: i += 2 ans += 1 print(ans)
EXPR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def ss(n, s): if n == 1: return 1 d = {} i = 0 j = 0 ans = 0 while i < n: while j + 1 < n and s[j] != s[j + 1]: j += 1 if j >= n - 1: i += 1 while i in d: i += 1 i += 1 while i in d: i += 1 else: d[j] = 1 x = s[i] i += 1 while i in d or i < n and s[i] == x: i += 1 ans += 1 j = max(i, j + 1) return ans for _ in range(int(input())): n = int(input()) s = str(input()) print(ss(n, s))
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
n_inputs = int(input()) for _ in range(n_inputs): str_len = int(input()) str_input = input() arr_of_lens = [] i = 0 while i < str_len: curr_len = 0 current_char = str_input[i] while i < str_len and current_char == str_input[i]: curr_len += 1 i += 1 arr_of_lens += [curr_len] n_lens = len(arr_of_lens) first_step_ind = 0 second_step_ind = 0 iters = 0 while first_step_ind < n_lens: while first_step_ind < n_lens and arr_of_lens[first_step_ind] <= 1: first_step_ind += 1 if first_step_ind < n_lens: arr_of_lens[first_step_ind] -= 1 arr_of_lens[second_step_ind] = 0 second_step_ind += 1 iters += 1 else: iters += (first_step_ind - second_step_ind + 1) // 2 print(iters)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from itertools import groupby t = int(input()) for _ in range(0, t): n = int(input()) ss = [i for i in input()] sg = [len(list(g)) for s, g in groupby(ss)] i, j, ans = 0, 0, 0 while i < len(sg): j = max(j, i) if sg[i] > 1: i += 1 else: while j < len(sg) and sg[j] == 1: j += 1 if j < len(sg) and sg[j] > 1: sg[j] -= 1 i += 1 else: i += 2 ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) s = input() blocks = [] size = 1 for i in range(n - 1): if s[i] == s[i + 1]: size += 1 else: blocks.append(size) size = 1 blocks.append(size) lost = 0 free_moves = 0 for block_size in blocks: if block_size > 2: free_moves_used = min(free_moves, block_size - 2) free_moves -= free_moves_used lost += block_size - 2 - free_moves_used elif block_size == 1: free_moves += 1 l = n - lost print(l // 2 + l % 2)
FOR VAR FUNC_CALL 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for i in range(int(input())): n = int(input()) s = input() arr = [] curr = 1 for i in range(1, n): if s[i] == s[i - 1]: curr += 1 else: arr.append(curr) curr = 1 arr.append(curr) i = 0 j = 0 steps = 0 while i < len(arr): if arr[i] == 1: while j < len(arr) and arr[j] == 1: j += 1 if j < len(arr): arr[j] -= 1 i += 1 else: steps += (len(arr) - i) // 2 + (len(arr) - i) % 2 break else: i += 1 if j < i: j = i steps += 1 print(steps)
FOR VAR FUNC_CALL 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 FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def solve(s): cur = 0 i = 0 last = i ans = 0 power = 0 while i <= len(s): if i == len(s) or s[i] != s[last]: while power and cur > 2: power -= 1 cur -= 1 ans += 1 if cur == 1: ans += 1 power += 1 if cur >= 2: ans += 2 cur = 0 last = i cur += 1 i += 1 return (ans + 1) // 2 for _ in range(int(input())): _ = input() s = input() print(solve(s))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
ts = int(input()) for t in range(ts): n = int(input()) l = input() a = [] curr = l[0] count = 1 for i in l[1:]: if i == curr: count += 1 else: a.append(count) count = 1 curr = i a.append(count) p1 = 0 p2 = 0 op = 0 la = len(a) while p1 < la and a[p1] == 1: p1 += 1 while p1 < la and p2 < la: a[p1] -= 1 p2 += 1 op += 1 if p1 < p2: p1 += 1 while p1 < la and a[p1] == 1: p1 += 1 if p1 == la: if (la - p2) % 2: op += (la - p2) // 2 + 1 else: op += (la - p2) // 2 print(op)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from sys import stdin t = int(stdin.readline()) for _ in range(t): n = stdin.readline() stri = stdin.readline() lst = [] count = 0 char = stri[0] for i in range(len(stri) - 1): if char == stri[i]: count += 1 else: lst.append(count) char = stri[i] count = 1 lst.append(count) current = 0 ans = 0 to_delete = 0 while to_delete < len(lst): current = max(to_delete, current) while lst[current] <= 1: current += 1 if current == len(lst): lst.pop(-1) current -= 2 ans += 1 break else: lst[current] -= 1 ans += 1 if len(lst) > to_delete and lst[to_delete] > 0: lst[to_delete] = 0 else: break to_delete += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import itertools t = int(input()) for _ in range(t): n = int(input()) s = input() groups = [len(list(g)) for k, g in itertools.groupby(s)] large_pos = 0 cur_pos = 0 count = 0 while cur_pos < len(groups): while large_pos < len(groups) and groups[large_pos] == 1: large_pos += 1 if large_pos < len(groups) and groups[large_pos] > 1: groups[large_pos] -= 1 else: cur_pos += 1 cur_pos += 1 large_pos = max(large_pos, cur_pos) count += 1 print(count)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
T = int(input()) for _ in range(T): N = int(input()) S = input() arr = [] seq = 1 for a, b in zip(S, S[1:]): if a == b: seq += 1 else: arr.append(seq) seq = 1 arr.append(seq) hist = [] arr.reverse() for i, a in enumerate(arr): if a == 1: continue hist.append([i, a]) ans = 0 while len(arr): if len(hist): hist[-1][1] -= 1 if hist[-1][1] == 1: hist.pop() elif len(arr): arr.pop() else: break ans += 1 if len(arr): arr.pop() if len(hist) and hist[-1][0] == len(arr): hist.pop() print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def prom(a, s): ans = 0 k0 = 0 k1 = 0 pred = "2" for c in s: if c != pred and pred != "2": a.append(k0 + k1) k0 = 0 k1 = 0 if c == "0": k0 += 1 else: k1 += 1 pred = c if pred != "2": a.append(k0 + k1) t = int(input()) for ti in range(t): n = int(input()) s = input() a = [] prom(a, s) l = len(a) bi = 0 b2 = 0 ans = 0 while bi < l: for i in range(max(b2, bi), l): b2 = i if a[i] > 1: a[i] -= 1 bi += 1 break else: bi += 2 ans += 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) s = input() count = [] c = 0 last = s[0] for i in range(n): if s[i] == last: c += 1 else: last = s[i] count.append(c) c = 1 if c != 0: count.append(c) tot = [] for i in range(len(count)): if count[i] > 1: tot.append(i) j = 0 ans = 0 i = 0 while i < len(count): if j < len(tot): count[tot[j]] -= 1 if count[tot[j]] == 1: j += 1 elif i == tot[j]: j += 1 else: ans += (len(count) - i + 1) // 2 break i += 1 ans += 1 print(ans)
FOR VAR FUNC_CALL 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 VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) s = input() blocks = [[s[0], 1]] for i in range(1, n): if s[i] == blocks[-1][0]: blocks[-1][1] += 1 else: blocks += [[s[i], 1]] ind = -1 for i in range(n): if blocks[i][1] > 1: ind = i break ans = 0 for i in range(len(blocks)): if ind == -1: break ans += 1 blocks[ind][1] -= 1 blocks[i][1] = 0 while ind < len(blocks) and blocks[ind][1] <= 1: ind += 1 if ind == len(blocks): ind = -1 cnt = 0 for i in range(len(blocks)): cnt += blocks[i][1] ans += (cnt + cnt % 2) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR LIST LIST VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for j in range(t): n = int(input()) s = input() i, op, future = 0, 0, 0 left = n while left > 0: op += 1 if i == n - 1: break symbol = s[i] flag = True left -= 1 i += 1 while left != 0 and symbol == s[i]: if future > 0: future -= 1 left += 1 else: flag = False left -= 1 i += 1 if flag: future += 1 left -= 1 print(op)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
n = int(input()) for _ in range(n): k = int(input()) s = input() s = s[0] + s + str(int(s[-1]) ^ 1) m = [] prev = 1 for i in range(1, k + 2): if s[i] != s[i - 1]: m.append(i - prev) prev = i ans = 0 start = 0 end = len(m) first = 0 while start < end: if m[start] > 1: start += 1 first = max(first, start) else: while first < end and m[first] == 1: first += 1 if first >= end: end -= 1 else: m[first] -= 1 start += 1 ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def find_cnt_seg(string, start): l = len(string) ind = start while ind < l - 1: if string[ind] == string[ind + 1]: return ind ind += 1 if ind >= l - 1: return -1 t = int(input()) for ii in range(t): n = int(input()) s = input() m = [False] * len(s) cnt = 0 i = 0 ind = find_cnt_seg(s, 0) if ind == i: i += 1 if ind == -1: print(n // 2 + n % 2) continue while i < n: cnt += 1 m[ind] = True start = s[i] while i < n: if s[i] == start: m[i] = True i += 1 elif m[i] == True: i += 1 else: break j = max(i, ind) while j < n: if m[j] == True: j += 1 else: break ind = find_cnt_seg(s, j) if ind == -1: break int_ = 0 for i in m: if i == False: int_ += 1 print(cnt + int_ // 2 + int_ % 2)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def main(): n = int(input()) s = input() l, o = 0, 0 cnt, groups = 0, [] while l < n: c = s[l] r = l while r + 1 < n and s[r + 1] == c: r += 1 pre_len = r - l + 1 groups.append(pre_len) l = r + 1 p, n = 0, len(groups) i = 0 while i < n: if groups[i] > 1: o += 1 p += 1 i += 1 else: while p < n and groups[p] == 1: p += 1 if p == n: i += 2 o += 1 else: groups[p] -= 1 o += 1 i += 1 print(o) for _ in range(int(input())): main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for _ in range(t): n = int(input()) s = input() l = [] counter = 1 for i in range(1, len(s)): if s[i] == s[i - 1]: counter += 1 else: l.append(counter) counter = 1 if counter != 0: l.append(counter) ans = 0 x = [] for i in range(len(l) - 1, -1, -1): if l[i] > 1: x.append([i, l[i]]) for i in range(len(l)): if l[i] > 1: ans += 1 x.pop() elif len(x) > 0: x[-1][1] -= 1 l[x[-1][0]] -= 1 if x[-1][1] == 1: x.pop() ans += 1 else: ans += (len(l) - i + 1) // 2 break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for _ in range(t): n = int(input()) s = input() si = [] mi = [] deleted = set() prev = "a" for i in range(n - 1, -1, -1): c = s[i] if c == prev: mi.append(i) else: si.append(i) prev = c mi.reverse() si.reverse() midx = 0 sidx = 0 opCnts = 0 i = 0 while i < n: while midx < len(mi) and mi[midx] < i: midx += 1 if midx < len(mi): deleted.add(mi[midx]) midx += 1 else: while sidx < len(si) and si[sidx] in deleted: sidx += 1 deleted.add(si[sidx]) sidx += 1 while i < n and (i in deleted or i + 1 < n and s[i] == s[i + 1]): i += 1 deleted.add(i) i += 1 opCnts += 1 print(opCnts)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for _ in range(t): n = int(input()) s = input() i, c = 0, 0 visit = [(-1) for _ in range(n)] flag, last = 0, -1 while i < n: if visit[i] == 1: i += 1 continue if i + 1 < n and s[i] == s[i + 1]: c += 1 while i + 1 < n and s[i] == s[i + 1]: i += 1 i += 1 elif i == n - 1: i += 1 c += 1 else: c += 1 if flag == 1: c1 = 1 j = i + 1 while j < n: if visit[j] == -1: visit[j] = 1 c1 -= 1 break j += 1 i = j + 1 continue if last == -1: j = i + 1 else: j = max(i + 1, last + 1) while j < n and visit[j] == 1 or j + 1 < n and s[j] != s[j + 1]: j += 1 if j < n: visit[j] = 1 i += 1 last = j else: flag = 1 c1 = 1 j = i + 1 while j < n: if visit[j] == -1: visit[j] = 1 c1 -= 1 break j += 1 i = j + 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) while t != 0: n = int(input()) s = input() arr = [] i = 0 while i < n: p = s[i] c = 0 while i < n and s[i] == p: c += 1 i += 1 arr.append(c) ans = 0 temp = 0 for i in range(len(arr)): if arr[i] != 1: p = arr[i] - 2 if temp > 0 and p > 0: temp = max(0, temp - p) ans += 1 else: temp += 1 ans += 1 print(ans - temp // 2) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
T = int(input()) for t in range(T): N = int(input()) s = input() groups = [] current_c = s[0] current_count = 0 for c in s: if c == current_c: current_count += 1 continue if current_c is not None: groups.append(current_count) current_count = 1 current_c = c groups.append(current_count) i = 0 im = 0 moves = 0 while i < len(groups): while im < len(groups) and groups[im] == 1: im += 1 if im >= len(groups): left_groups = len(groups) - i moves += (left_groups + 1) // 2 break groups[im] -= 1 i += 1 im = max(im, i) moves += 1 print(moves)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from itertools import groupby def main(): N = int(input()) S = input() C = [len(list(x[1])) for x in groupby(S)] M = len(C) dup_idx = [] for i, c in enumerate(C): if c > 1: dup_idx.append(i) dup_idx.reverse() curr = 0 while dup_idx: i = dup_idx[-1] if i < curr: dup_idx.pop() continue C[i] -= 1 if C[i] == 1: dup_idx.pop() curr += 1 ans = curr + (M - curr + 1) // 2 print(ans) def __starting_point(): for __ in [0] * int(input()): main() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) s = str(input()) j = 0 ll = [] count = 1 tt = 0 while j < n - 1: if s[j + 1] == s[j]: count += 1 if j == n - 2: ll.append(count) tt += count j += 1 else: j += 1 ll.append(count) tt += count count = 1 if n - tt > 0: ll.append(n - tt) zz = 0 ans = 0 qq = 0 count = 0 i = 0 j = i + 1 ttt = len(ll) while i < ttt: if j == ttt: break if ll[i] != 1: ans += 1 i += 1 j = max(i + 1, j) elif ll[j] > 1: ll[j] -= 1 ans += 1 i += 1 j = max(i + 1, j) else: j += 1 ans += (ttt - i) // 2 + (ttt - i) % 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def testcase(): n = int(input()) s = input() fs = [] last = -1 for i in range(n - 1): if s[i] != s[i + 1]: fs.append(i - last) last = i if last != n - 1: fs.append(n - 1 - last) m = len(fs) ans = 0 j = 1 i = 0 while i < m: while j < m and (j <= i or fs[j] <= 1): j += 1 if fs[i] > 1: ans += 1 i += 1 elif j < m: ans += 1 fs[j] -= 1 i += 1 while j < m and fs[j] <= 1: j += 1 else: ans += 1 i += 2 print(ans) return t = int(input()) for _ in range(t): testcase()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
from sys import stdin for _ in range(int(stdin.readline())): length = int(stdin.readline()) string = stdin.readline() if length == 1: print(1) continue lists = [] temp = str(string[0]) ans = 0 for i in range(1, length): if string[i] != string[i - 1]: lists.append(temp) temp = "" temp += string[i] else: temp += string[i] lists.append(temp) total1 = 0 for x in range(len(lists)): i = lists[x] if len(i) == 1: total1 += 1 else: a = len(i) - 1 if total1 >= a: total1 -= a ans += a total1 += 1 else: ans += total1 + 1 total1 = 0 ans += (total1 + 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys T = int(sys.stdin.readline().strip()) for t in range(0, T): n = int(sys.stdin.readline().strip()) s = sys.stdin.readline().strip() L = [1] for i in range(1, n): if s[i] == s[i - 1]: L[-1] = L[-1] + 1 else: L.append(1) L.reverse() i = n - 1 ans = 0 while len(L) > 0: ans = ans + 1 v = True i = min(i, len(L) - 1) while i >= 0 and v == True: if L[i] == 1: i = i - 1 if i == -1: v = False else: v = False if i == -1: L.pop() else: L[i] = L[i] - 1 if len(L) > 0: L.pop() print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def ceil(a, b): return (a + b - 1) // b for _ in range(int(input())): n = int(input()) s = input() streak = 1 prev = s[0] res = [] for i in range(1, n): if s[i] == prev: streak += 1 else: prev = s[i] res.append(streak) streak = 1 res.append(streak) ans = 0 next_max = 0 t = len(res) for i in range(t): next_max = max(next_max, i) while next_max < t and res[next_max] == 1: next_max += 1 if next_max != t: res[next_max] -= 1 ans += 1 ans += 1 print(ceil(ans, 2))
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): input() s = input() n = len(s) ans = 0 i = 0 j = 0 no = [False] * n while i < n: if i + 1 == n: ans += 1 break if s[i] != s[i + 1]: j = max(j, i + 1) while j + 1 < n: if s[j] == s[j + 1]: no[j] = True j += 1 break j += 1 else: ans += (n - i - sum(no[i:]) + 1) // 2 break c = s[i] i += 1 while i < n and (s[i] == c or no[i]): i += 1 ans += 1 print(ans, flush=False)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
class SegTree: def __init__(self, lists, function, basement): self.n = len(lists) self.K = (self.n - 1).bit_length() self.f = function self.b = basement self.seg = [basement] * (2 ** (self.K + 1) + 1) X = 2**self.K for i, v in enumerate(lists): self.seg[i + X] = v for i in range(X - 1, 0, -1): self.seg[i] = self.f(self.seg[i << 1], self.seg[i << 1 | 1]) def update(self, k, value): X = 2**self.K k += X self.seg[k] += value while k: k = k >> 1 self.seg[k] = self.f(self.seg[k << 1], self.seg[k << 1 | 1]) def query(self, L, R): num = 2**self.K L += num R += num vL = self.b vR = self.b while L < R: if L & 1: vL = self.f(vL, self.seg[L]) L += 1 if R & 1: R -= 1 vR = self.f(self.seg[R], vR) L >>= 1 R >>= 1 return self.f(vL, vR) def find_min_index(self, L, R, X): return self.fmi(L, R, X, 1, 0, 2**self.K) def fMi(self, a, b, x, k, l, r): if self.seg[k] < x or r <= a or b <= l: return -1 elif k >= 2**self.K: return k - 2**self.K else: vr = self.fMi(a, b, x, k << 1 | 1, (l + r) // 2, r) if vr != -1: return vr return self.fMi(a, b, x, k << 1, l, (l + r) // 2) def fmi(self, a, b, x, k, l, r): if self.seg[k] < x or r <= a or b <= l: return -1 elif k >= 2**self.K: return k - 2**self.K else: vl = self.fmi(a, b, x, k << 1, l, (l + r) // 2) if vl != -1: return vl return self.fmi(a, b, x, k << 1 | 1, (l + r) // 2, r) def solve(): n = int(input()) s = input() + "#" cnt = 0 que = [] for i in range(n): cnt += 1 if s[i] != s[i + 1]: que.append(cnt) cnt = 0 m = len(que) seg = SegTree(que, max, 0) res = m for i in range(m): V = seg.query(i, m) if V == 1: res -= (m - i) // 2 break else: p = seg.find_min_index(i, m, 2) seg.update(p, -1) print(res) return def main(): t = int(input()) for i in range(t): solve() return main()
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys input = lambda: sys.stdin.readline().rstrip() t = int(input()) for _ in range(t): n = int(input()) s = input() se = set() for i in range(n - 1): if s[i] == s[i + 1]: se.add(i) now = 0 idx = 0 ans = 0 deleted = set() while now < n: while idx < n: if idx < now or idx not in se: idx += 1 continue deleted.add(idx) idx += 1 break else: now += 1 while now in deleted: now += 1 while now < n - 1: nxt = now + 1 while nxt in deleted: nxt += 1 if s[now] == s[nxt]: now = nxt continue break now += 1 while now in deleted: now += 1 ans += 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n = mint() s = minp() have = [True] * n i = 0 ans = 0 j = 0 k = 0 ks = 0 kp = "z" left = n while True: while i < n and not have[i]: i += 1 if i == n: break j = max(i, j) c = s[i] while j < n: if have[j] and s[j] != c: break j += 1 if k <= i: ks = 0 k = i kp = "z" while k < n: if have[k]: if s[k] != kp: ks = 0 kp = s[k] k += 1 else: ks += 1 break else: k += 1 if k == n: ans += (left + 1) // 2 break ans += 1 if have[k]: left -= 1 have[k] = False while i < n: if have[i] and s[i] != c: break if have[i]: left -= 1 have[i] = False i += 1 print(ans) def solve1(): n = mint() s = minp() i = 0 res = 0 while i < n: j = i c = s[i] while j < n and c == s[j]: j += 1 res += 1 i = j for i in range(mint()): 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 VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR WHILE NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING WHILE VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for _ in range(t): n = int(input()) s = list(input()) if n == 1: print(1) continue renzoku = [] ren = 1 for i in range(1, n): if s[i - 1] == s[i]: ren += 1 else: renzoku.append(ren) ren = 1 if i == n - 1: renzoku.append(ren) pena = 0 count = 0 l = len(renzoku) for i in range(l): count += renzoku[i] - 1 if count > i + 1: pena += count - (i + 1) count = i + 1 ans = (n - pena + 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
cases = int(input()) for t in range(cases): n = int(input()) s = input() + "!" c = [] v = 1 l = 0 for i in range(1, n + 1): if s[i] == s[i - 1]: v += 1 else: c.append(v) l += 1 v = 1 i, out, dl = 0, 0, 0 while i < l: out += 1 while dl < l and c[dl] <= 1: dl += 1 if dl < l and c[dl] > 1: c[dl] -= 1 else: i += 1 i += 1 dl = max(i, dl) print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
def solve(n, s): last = s[0] cur = 1 patch = [] for c in s[1:]: if c == last: cur += 1 else: patch.append(cur) cur = 1 last = c patch.append(cur) lp = len(patch) ans = 0 j = 0 while j < lp and patch[j] == 1: j += 1 for i in range(lp): if j < lp: patch[j] -= 1 ans += 2 else: ans += 1 j = max(j, i + 1) while j < lp and patch[j] == 1: j += 1 print(ans // 2 + ans % 2) T = int(input()) for t in range(T): n = int(input()) s = input() solve(n, s)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
TC = int(input()) for tc in range(TC): N = int(input()) S = input() ln = [] cs = S[0] c = 0 for s in S: if s == cs: c += 1 else: ln.append(c) c = 1 cs = s ln.append(c) result = 0 pos = 0 nxt = pos while pos < len(ln): if nxt == pos: while nxt < len(ln) and ln[nxt] == 1: nxt += 1 if ln[pos] == 1: if nxt < len(ln) and nxt != pos: ln[nxt] -= 1 while nxt < len(ln) and ln[nxt] == 1: nxt += 1 else: pos += 1 else: nxt += 1 pos += 1 result += 1 print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() def RLE(s): cc = [] ww = [] pc = s[0] w = 0 for c in s: if c == pc: w += 1 else: cc.append(pc) ww.append(w) w = 1 pc = c cc.append(pc) ww.append(w) return cc, ww for _ in range(II()): n = II() s = SI() cc, ww = RLE(s) ans = 0 j = 0 back = False for i in range(len(ww)): w = ww[i] if w == 0: break if back: j -= 1 ww[j] = 0 else: if j < i: j = i while j < len(ww) and ww[j] == 1: j += 1 if j == len(ww): back = True j -= 1 ww[j] = 0 else: ww[j] -= 1 ans += 1 print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for _ in range(t): n = int(input()) s = input() s = list(s) ans = [] cnt = 1 for i in range(1, n): if s[i] == s[i - 1]: cnt += 1 else: ans.append(cnt) cnt = 1 ans.append(cnt) one = 0 gre = 0 two = 0 i = 0 cnt = 0 while i < len(ans): if ans[i] == 1: one += 1 elif ans[i] == 2: cnt += 1 elif one >= 1: x = min(ans[i] - 1, one) cnt += x one -= x ans[i] -= x i -= 1 else: cnt += 1 i += 1 if one >= 1: if one % 2 == 0: cnt += one // 2 else: cnt += one // 2 + 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) def constructPartitions(partition): start = 0 for end in range(1, n): if s[end] != s[start]: size = end - start partition.append(size) start = end size = n - start partition.append(size) def solve(partition): prefixPtr = greaterThanOnePtr = 0 length = len(partition) ans = 0 while prefixPtr < length: while greaterThanOnePtr < length and partition[greaterThanOnePtr] == 1: greaterThanOnePtr += 1 if greaterThanOnePtr != length and partition[greaterThanOnePtr] > 1: partition[greaterThanOnePtr] -= 1 prefixPtr += 1 if prefixPtr == greaterThanOnePtr + 1: greaterThanOnePtr += 1 elif greaterThanOnePtr == length: prefixPtr += 2 ans += 1 return ans for _ in range(t): n = int(input()) s = input() if n == 1: print(1) continue partition = list() constructPartitions(partition) ans = solve(partition) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
for _ in range(int(input())): n = int(input()) s = input() curr = 1 a = [] for i in range(1, n): if s[i] != s[i - 1]: a.append(curr) curr = 1 else: curr += 1 a.append(curr) l = 0 r = l ans = 0 while l < len(a): if a[l] == 0: l += 1 continue if a[l] > 1: ans += 1 a[l] = 0 else: while r < len(a) and a[r] <= 1: r += 1 if l < r < len(a): a[r] -= 1 a[l] = 0 ans += 1 else: ans += (len(a) - l) // 2 + (len(a) - l) % 2 break l += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for i in range(t): n = int(input()) s = input() s1 = [1] for j in range(1, n): if s[j] == s[j - 1]: s1[len(s1) - 1] += 1 else: s1.append(1) c = 0 p = 0 for j in range(len(s1)): if s1[j] > 1: if j == 0: c += 1 elif s1[j] > p: c += p if s1[j] > p + 1: p = 0 c += 1 else: p = 1 else: c += s1[j] - 1 p -= s1[j] - 1 p += 1 else: p += 1 c += (p + 1) // 2 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys from itertools import groupby input = sys.stdin.readline def HalfDead(): n = int(input()) s = input().strip() a = [len(list(g)) for _, g in groupby(s)] cnt = 0 for i, v in enumerate(a): if v == 1: cnt += 1 else: r = v - 2 cnt = max(0, cnt - r) print(len(a) - cnt // 2) for _ in range(int(input())): HalfDead()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
import sys input = sys.stdin.buffer.readline T = int(input()) for testcase in range(T): n = int(input()) s = list(input()) d = [] cnt = 1 for i in range(1, n): if s[i] == s[i - 1]: cnt += 1 else: d.append(cnt) cnt = 1 d.append(cnt) n = len(d) res = 0 pidx = 0 i = 0 while i < n: if d[i] >= 2: res += 1 i += 1 else: flag = True if pidx < i: pidx = i while pidx < n: if d[pidx] >= 2: d[pidx] -= 1 flag = False break else: pidx += 1 if flag: res += 1 i += 2 else: res += 1 i += 1 print(res)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR