CF1809D.Binary String Sorting

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a binary string ss 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 101210^{12} coins;
  • choose any element from the string and remove it. In order to perform this operation, you pay 1012+110^{12}+1 coins.

Your task is to calculate the minimum number of coins required to sort the string ss in non-decreasing order (i. e. transform ss so that s1≤s2≤⋯≤sms_1 \le s_2 \le \dots \le s_m, where mm is the length of the string after applying all operations). An empty string is also considered sorted in non-decreasing order.

给你一个仅由字符 0 和/或 1 组成的二进制字符串 ss。

你可以对该字符串执行若干次操作(可能为零次)。操作分为两种类型:

  • 选择两个相邻的元素并交换它们。执行该操作需花费 101210^{12} 枚硬币;
  • 从字符串中任选一个元素并将其删除。执行该操作需花费 1012+110^{12}+1 枚硬币。

你的任务是计算将字符串 ss 变为非递减序(即:经过所有操作后,设所得字符串长度为 mm,则满足 s1≤s2≤⋯≤sms_1 \le s_2 \le \dots \le s_m)所需的最少硬币数。空字符串也被视为满足非递减序。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The only line of each test case contains the string ss (1≤∣s∣≤3⋅1051 \le |s| \le 3 \cdot 10^5), consisting of only characters 0 and/or 1.

The sum of lengths of all given strings doesn't exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

每个测试用例仅有一行,包含字符串 ss(1≤∣s∣≤3⋅1051 \le |s| \le 3 \cdot 10^5),该字符串仅由字符 0 和/或 1 组成。

所有给定字符串的长度总和不超过 3⋅1053 \cdot 10^5。

输出格式

For each test case, print a single integer — the minimum number of coins required to sort the string ss in non-decreasing order.

对于每个测试用例,输出一个整数——将字符串 ss 按非递减顺序排序所需的最少硬币数量。

输入输出样例

  • 输入#1

    6
    100
    0
    0101
    00101101
    1001101
    11111

    输出#1

    1000000000001
    0
    1000000000000
    2000000000001
    2000000000002
    0

说明/提示

In the first example, you have to remove the 11-st element, so the string becomes equal to 00.

In the second example, the string is already sorted.

In the third example, you have to swap the 22-nd and the 33-rd elements, so the string becomes equal to 0011.

In the fourth example, you have to swap the 33-rd and the 44-th elements, so the string becomes equal to 00011101, and then remove the 77-th element, so the string becomes equal to 0001111.

In the fifth example, you have to remove the 11-st element, so the string becomes equal to 001101, and then remove the 55-th element, so the string becomes equal to 00111.

In the sixth example, the string is already sorted.

在第一个例子中,你需要删除第 11 个元素,因此字符串变为 00。

在第二个例子中,字符串本身已经有序。

在第三个例子中,你需要交换第 22 个和第 33 个元素,因此字符串变为 0011。

在第四个例子中,你需要交换第 33 个和第 44 个元素,因此字符串变为 00011101,然后删除第 77 个元素,字符串变为 0001111。

在第五个例子中,你需要删除第 11 个元素,因此字符串变为 001101,然后再删除第 55 个元素,字符串变为 00111。

在第六个例子中,字符串本身已经有序。

输入解题思路,AI测评打分。不知道怎么写?

首页