Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

D . Binary String Sorting time limit per test 2 seconds memory limit per test: 2 5 6 megabytes input: standard input output: standard output

D. Binary String Sorting
time limit per test 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
You are given a binary string s consisting of only characters 0 and/or 1.
You can perform several operations on this string (possibly zero). There are two types of operations:
choose two consecutive elements and swap them. In order to perform this operation, you pay 1012 coins;
choose any element from the string and remove it. In order to perform this operation, you pay 1012+1 coins.
Your task is to calculate the minimum number of coins required to sort the string s in non-decreasing order (i. e. transform s so that
s1s2cdotssm, where m is the length of the string after applying all operations). An empty string is also considered sorted in non-
decreasing order.
Input
The first line contains a single integer t(1t104)- the number of test cases.
The only line of each test case contains the string s(1|s|3*105), consisting of only characters 0 and/or 1.
The sum of lengths of all given strings doesn't exceed 3*105.
Output
For each test case, print a single integer the minimum number of coins required to sort the string s in non-decreasing order.
Example
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions