codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1430D.md (2393B)


      1 Source: https://codeforces.com/problemset/problem/1430/D
      2 
      3 D. String Deletion
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1.
     22 
     23 You can perform operations on the string. Each operation consists of two steps:
     24 
     25 1. 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$);
     26 2. 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).
     27 
     28 Note that both steps are mandatory in each operation, and their order cannot be changed.
     29 
     30 For example, if you have a string $s =$ 111010, the first operation can be one of the following:
     31 
     32 1. select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010;
     33 2. select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010;
     34 3. select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010;
     35 4. select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0;
     36 5. select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00;
     37 6. select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01.
     38 
     39 You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform?
     40 
     41 Input
     42 
     43 The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
     44 
     45 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$.
     46 
     47 The second line contains string $s$ of $n$ characters. Each character is either 0 or 1.
     48 
     49 It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
     50 
     51 Output
     52 
     53 For each test case, print a single integer — the maximum number of operations you can perform.
     54 
     55 Example
     56 
     57 Input
     58 
     59 ```
     60 5
     61 6
     62 111010
     63 1
     64 0
     65 1
     66 1
     67 2
     68 11
     69 6
     70 101010
     71 ```
     72 
     73 Output
     74 
     75 ```
     76 3
     77 1
     78 1
     79 1
     80 3
     81 ```
     82 
     83 Note
     84 
     85 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.