B. Minimum Ternary String (这个B有点狠)
1 second
256 megabytes
standard input
standard output
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 (consecutive) characters '1' and '2' (i.e. replace "12" with "21" or vice versa).
For example, for string "010210" we can perform the following moves:
- "010210" →→ "100210";
- "010210" →→ "001210";
- "010210" →→ "010120";
- "010210" →→ "010201".
Note than you cannot swap "02" →→ "20" and vice versa. You cannot perform any other operations with the given string excluding described above.
You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).
String aa is lexicographically less than string bb (if strings aa and bb have the same length) if there exists some position ii (1≤i≤|a|1≤i≤|a|, where|s||s| is the length of the string ss) such that for every j<ij<i holds aj=bjaj=bj, and ai<biai<bi.
The first line of the input contains the string ss consisting only of characters '0', '1' and '2', its length is between 11 and 105105 (inclusive).
Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).
100210
001120
11222121
11112222
20
20
这场CF abcd题 我就觉得B题最难
这个题目卡了我好久好久 ,全是写BUG
代码有点毒瘤
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
const int INF = 0x3fffffff;
typedef long long LL;
string s;
int sum0[maxn], sum1[maxn], sum2[maxn], vis[maxn];
vector<int>a;
int main() {
cin >> s;
if (s[] == '') sum0[] = ;
if (s[] == '') sum1[] = ;
if (s[] == '') sum2[] = ;
for (int i = ; i < s.size() ; i++) {
if (s[i] == '') sum0[i + ] = sum0[i] + , sum1[i + ] = sum1[i], sum2[i + ] = sum2[i];
if (s[i] == '') sum1[i + ] = sum1[i] + , sum0[i + ] = sum0[i], sum2[i + ] = sum2[i];
if (s[i] == '') sum2[i + ] = sum2[i] + , sum0[i + ] = sum0[i], sum1[i + ] = sum1[i];
}
int cnt = ;
int len = s.size();
for (int i = ; i < len ; i++) {
if (s[i] == '') {
for (int j = ; j < sum0[i]; j++) a.push_back();
for (int j = ; j < sum1[len] ; j++) a.push_back();
for (int j = i ; j < s.size(); j++) {
if (s[j] == '') continue;
if (s[j] == '') a.push_back();
if (s[j] == '')a.push_back();
}
cnt = ;
break;
}
}
if (cnt == ) {
for (int i = ; i < sum0[len] ; i++) printf("");
for (int i = ; i < sum1[len] ; i++) printf("");
} else {
for (int i = ; i < a.size() ; i++) printf("%d", a[i]);
}
return ;
}
B. Minimum Ternary String (这个B有点狠)的更多相关文章
- codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...
- CF1009B Minimum Ternary String 思维
Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CodeForces - 1009B Minimum Ternary String
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...
- Codeforces ~ 1009B ~ Minimum Ternary String (思维)
题意 给你一个只含有0,1,2的字符串,你可以将"01"变为"10","10"变为"01","12" ...
- Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String
题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...
- Balanced Ternary String CodeForces - 1102D (贪心+思维)
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...
- 牛客多校第四场 A Ternary String
题目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a t ...
- 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]
题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...
- 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)
链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...
随机推荐
- Python系列5之模块
模块 1. 模块的分类 模块,又称构件,是能够单独命名并独立地完成一定功能的程序语句的集合(即程序代码和数据结构的集合体). (1)自定义模块 自己定义的一些可以独立完成某个功能的一段程序语句,可以是 ...
- centos7上部署新版 jumpserver 跳板机服务
CentOS 7 建议在一个纯净的 centos7上进行下面的安装部署 关闭 selinux 和防火墙 [root@jumpserver ~]# setenforce 0 [root@jumpserv ...
- go学习笔记-包处理
包处理 package是go管理代码的重要工具,用于组织 Go 源代码,提供了更好的可重用性与可读性. 可见性 变量或函数名的首字母大写时,其就是可导出的,小写时则是不可导出的. 函数和变量的可访问性 ...
- 教你如何更改xshell中的转发规则
使用不同的类型转发,与之对应的端口,所以如果想要使用不同类型的转发就要更改端口使其与之一一对应.本集xshell专栏文章将为大家讲解如何更改转发规则. 更改转发规则操作如下: 1.打开会话对话框. 2 ...
- bootstrap重新设计checkbox样式
文章采集于: https://www.cnblogs.com/GumpYan/p/7845445.html#undefined 在原文基础上修改了勾勾的内容,直接采用bootstrap字体库.修改了横 ...
- P1332 血色先锋队
P1332 血色先锋队 题目描述 巫妖王的天灾军团终于卷土重来,血色十字军组织了一支先锋军前往诺森德大陆对抗天灾军团,以及一切沾有亡灵气息的生物.孤立于联盟和部落的血色先锋军很快就遭到了天灾军团的重重 ...
- nginx error_page
1. error_page语法 语法: error_page code [ code... ] [ = | =answer-code ] uri | @named_location 默认值: no 使 ...
- 使用Entity Framework时,序列化出错
在使用Entity Framework时,如果数据库中有两个表是一对多或者是多对多的关系,那么生成的实体类中就有一个导航属性.这个导航属性前面都加上了一个virtual关键字.这个v ...
- 『AngularJS』Service
理解Angular 服务 什么是Angular Service Angular 服务是为web应用执行特定任务的单例对象或方法. 注:如果组件是为了内容呈现的功能复用,那么服务就是为组件进行功能复用. ...
- 失败的尝试,使用继承扩展数组,以及ES6的必要性
我们都知道直接在原生对象上扩展对象是很不好的.所以prototype这样的库广受非议. 一些库,比如lodash采用了工具包形式的扩展方式,绕开了对象的继承. 由于es6的class的出现,我尝试以A ...