Solutions are presented as using the least memory and the fastest execution time. It also takes the top
10 most recent solutions from each language. If you want to limit to a specific index, click the
"Solved" button and go to that problem.
Problems
B"You are given a binary string S of length n indexed from 1 to n . You can perform the following operation any number of times (possibly zero): Choose two integers l and r ( 1 <= l <= r <= n ). Let cnt_0 be the number of times 0 occurs in S[l ldots r] and cnt_1 be the number of times 1 occurs in S[l ldots r] . You can pay |cnt_0 - cnt_1| + 1 coins and sort the S[l ldots r] . (by S[l ldots r] we mean the substring of S starting at position l and ending at position r ) For example if S = 11001, we can perform the operation on S[2 ldots 4] , paying |2 - 1| + 1 = 2 coins, and obtain S = 10011 as a new string. Find the minimum total number of coins required to sort S in increasing order. The first line contains a single integer t ( 1 <= t <= 1000 ) -- the number of test cases. The description of test cases follows. The first line of each test case contains a single integer n ( 1 <= n <= 2 cdot 10^5 ) -- the size of S . The second line of each test case contains a binary string S of n characters S_1S_2 ldots S_n . ( S_i = 0 or S_i = 1 for each 1 <= i <= n ) It is guaranteed that the sum of n over all test cases doesn't exceed 2 cdot 10^5 . For each test case, output the minimum total number of coins required to sort S in increasing order. In the first test case, S is already sorted. In the second test case, it's enough to apply the operation with l = 1, r = 2 . In the third test case, it's enough to apply the operation with l = 1, r = 2 . "... |
Tutorials
Submissions
Submission Id |
Author(s) |
Index |
Submitted |
Verdict |
Language |
Test Set |
Tests Passed |
Time taken (ms) |
Memory Consumed (bytes) |
Tags |
Rating |
160864850 |
rainboy |
F |
June 16, 2022, 3:45 p.m. |
OK |
GNU C11 |
TESTS |
86 |
31 |
1843200 |
|
3400 |
160888784 |
djq_cpp |
F |
June 16, 2022, 5:05 p.m. |
OK |
GNU C++14 |
TESTS |
86 |
46 |
6860800 |
|
3400 |
160887743 |
gisp_zjz |
F |
June 16, 2022, 5 p.m. |
OK |
GNU C++17 |
TESTS |
86 |
31 |
16998400 |
|
3400 |
160880007 |
Radewoosh |
F |
June 16, 2022, 4:22 p.m. |
OK |
GNU C++17 |
TESTS |
86 |
31 |
53452800 |
|
3400 |
160878948 |
izone |
F |
June 16, 2022, 4:19 p.m. |
OK |
GNU C++17 |
TESTS |
86 |
46 |
27033600 |
|
3400 |
160890167 |
heno239 |
F |
June 16, 2022, 5:14 p.m. |
OK |
GNU C++17 |
TESTS |
86 |
78 |
25804800 |
|
3400 |
160912177 |
csyakuoi |
F |
June 17, 2022, 1:23 a.m. |
OK |
GNU C++17 (64) |
TESTS |
86 |
31 |
2560000 |
|
3400 |
160908601 |
kal013 |
F |
June 16, 2022, 10:25 p.m. |
OK |
GNU C++17 (64) |
TESTS |
86 |
31 |
7782400 |
|
3400 |
160882783 |
Merkurev |
F |
June 16, 2022, 4:28 p.m. |
OK |
GNU C++17 (64) |
TESTS |
86 |
108 |
13926400 |
|
3400 |
160892940 |
ecnerwala |
F |
June 16, 2022, 5:37 p.m. |
OK |
GNU C++20 (64) |
TESTS |
86 |
15 |
1331200 |
|
3400 |
160893311 |
ecnerwala |
F |
June 16, 2022, 5:40 p.m. |
OK |
GNU C++20 (64) |
TESTS |
86 |
15 |
1331200 |
|
3400 |
160893308 |
brunovsky |
F |
June 16, 2022, 5:40 p.m. |
OK |
GNU C++20 (64) |
TESTS |
86 |
15 |
1331200 |
|
3400 |
160861674 |
tourist |
F |
June 16, 2022, 3:38 p.m. |
OK |
GNU C++20 (64) |
TESTS |
86 |
15 |
1331200 |
|
3400 |
160920051 |
sky123 |
F |
June 17, 2022, 4:44 a.m. |
OK |
GNU C++20 (64) |
TESTS |
86 |
15 |
2150400 |
|
3400 |
160875588 |
jiangly |
F |
June 16, 2022, 4:11 p.m. |
OK |
GNU C++20 (64) |
TESTS |
86 |
15 |
2969600 |
|
3400 |
160880972 |
Petr |
F |
June 16, 2022, 4:24 p.m. |
OK |
GNU C++20 (64) |
TESTS |
86 |
15 |
3788800 |
|
3400 |
160882743 |
ksun48 |
F |
June 16, 2022, 4:28 p.m. |
OK |
GNU C++20 (64) |
TESTS |
86 |
31 |
2150400 |
|
3400 |
160899913 |
maroonrk |
F |
June 16, 2022, 7 p.m. |
OK |
GNU C++20 (64) |
TESTS |
86 |
31 |
19968000 |
|
3400 |
160914573 |
dragonslayerintraining |
F |
June 17, 2022, 2:40 a.m. |
OK |
GNU C++20 (64) |
TESTS |
86 |
78 |
9830400 |
|
3400 |
160874960 |
qwerty787788 |
F |
June 16, 2022, 4:09 p.m. |
OK |
Rust 2021 |
TESTS |
86 |
31 |
8499200 |
|
3400 |
160878263 |
Egor |
F |
June 16, 2022, 4:17 p.m. |
OK |
Rust 2021 |
TESTS |
86 |
46 |
3788800 |
|
3400 |
remove filters
Back to search problems