D - Balanced Ternary String (贪心)】的更多相关文章

题意:给你一个长度为\(3*n\)的字符串,要求修改最少的次数,使得字符串中\(0,1,2\)的个数相同,并且在最少次数的情况下使字典序最小. 题解:贪心,\(0\)一定放在前面,\(1\)和\(2\)放后面,首先统计\(0,1,2\)的个数,因为题目要求字典序最小,所以我们先从左边开始遍历,如果\(2\)的个数大于\(n/3\),那么再看\(0\)和\(1\)的个数情况将其替换成\(0\)或\(1\),对\(1\)也是如此,然后我们再反着遍历,首先考虑\(1\)的情况,再考虑\(0\)的情况.…
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings. Your task is to replace minimum number of characters in this string with other characters to obtain…
题目链接:Balanced Ternary String 题目大意:给一个字符串,这个字符串只由0,1,2构成,然后让替换字符,使得在替换字符次数最少的前提下,使新获得的字符串中0,1,2 这三个字符的数目相同,并且新获得的字符串的字典序要尽可能的小: 直接数组做法:暴力遍历 /* */ # include <bits/stdc++.h> using namespace std; typedef long long ll; int n; ]; string s; int main() { io…
题目链接:http://codeforces.com/contest/1102/problem/D 题目大意:给你一个字符串,这个字符串是由0,1,2构成的,然后让你替换字符,使得在替换的次数最少的前提下,使得新获得的字符串中0,1,2这三个字 符的数目相同,并且新获得的字符串字典序要尽可能的小. 具体思路: 我们先统计出每个字符的个数,想一下,除了三个都相等的情况下,这三个中的某一个肯定是大于n/3的,我们就枚举每一个字符. 如果是2多的话,我们就用1和0从前面进行替换. 如果是1多的话,我们…
题目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a ternary string s which can self-reproduce. Every second, a digit 0 is inserted after every 1 in the string, and then a digit 1 is inserted after every…
题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a ternary string s which can self-reproduce. Every second, a digit 0 is inserted after every 1 in the str…
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace "01" with "10" or vice versa) or any two adjacent (con…
链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之后第一个数字消失.求最后为空串需要多少秒. 题解: (1)如果在消除一个 0 前经过了 n 秒,那么消掉这个 0 需要 n + 1 秒. (2)如果在消除一个 1 前经过了 n 秒,那么消掉这个 1 与其产生的所有数需要 (n + 1) * 2 秒. (3)如果在消除一个 2 前经过了 n 秒,那么…
B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). You can swap an…
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a ternary string (it is a string which consists only…