http://codeforces.com/problemset/problem/1102/D

题意:

有n个字符,只能为012,现要通过变换让012的数量相等,并且使字典序最小。

解题:

由样例可以看出不能打乱原来的位置,按001122这样输出,只能一个一个替换,贪心。

1)只有0少了:优先把多余的2换成0,再把多余的1换成0。

2)只有1少了:优先把多余的2换成1,等0够了再把多余的0换成1。

3)只有2少了:等0和1够了,优先把多余的1换成2,再把多余的0换成2。

4)0和1都少了:2优先换成0,再换成1,再输出自己的。

5)0和2都少了:1优先换成0,1够了再换成2。

6)1和2都少了:0够了再把多余的0换成1,再换成2。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; int n;
string s; int main()
{
while(cin>>n)
{
cin>>s;
int x=n/;
int zero=,one=,two=;
for(int i=;i<n;i++)
{
if( s[i]=='' )
zero++;
else if(s[i]=='')
one++;
else
two++;
}
zero-=x;
one-=x;
two-=x;
if( zero< && one>= && two>= )
{
for(int i=;i<n;i++)
{
if( s[i]=='' && zero< && two> )
s[i]='',zero++,two--;
else if( s[i]=='' && zero< && one> )
s[i]='',zero++,one--;
}
}
else if( one< && zero>= && two>= )
{
int now0=;
for(int i=;i<n;i++)
{
if(s[i]=='' && now0<x)
now0++;
else if( s[i]=='' && one< && two> )
s[i]='',one++,two--;
else if( s[i]=='' && one< && zero> && now0==x )
s[i]='',one++,zero--;
}
}
else if( two< && zero>= && one>= )
{
int now0=,now1=;
for(int i=;i<n;i++)
{
if(s[i]=='' && now0<x)
now0++;
else if( s[i]=='' && now1<x )
now1++;
else if( s[i]=='' && one> && now1==x && two< )
s[i]='',one--,two++;
else if( s[i]=='' && zero> && now0==x && two< )
s[i]='',zero--,two++; }
}
else if( zero< && one< && two> )
{
for(int i=;i<n;i++)
if( s[i]=='' && two> && zero< )
s[i]='',two--,zero++;
else if( s[i]=='' && two> && one< )
s[i]='',two--,one++;
}
else if( zero< && two< && one> )
{
int now1=;
for(int i=;i<n;i++)
{
if( s[i]=='' && zero== && now1<x )
now1++;
else if( s[i]=='' && zero< && one> )
s[i]='',zero++,one--;
else if( s[i]=='' && now1==x && two< && one> )
s[i]='',two++,one--; }
}
else if( one< && two< && zero> )
{
int now0=;
for(int i=;i<n;i++)
{
if( s[i]=='' && now0<x )
now0++;
else if( s[i]=='' && one< && now0==x && zero> )
s[i]='',one++,zero--;
else if( s[i]=='' && two< && now0==x && zero> )
s[i]='',two++,zero--;
}
}
cout<<s<<endl;
}
return ;
}

CF1102D

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

  1. Codeforces Round #531 (Div. 3) D. Balanced Ternary String (贪心)

    题意:给你一个长度为\(3*n\)的字符串,要求修改最少的次数,使得字符串中\(0,1,2\)的个数相同,并且在最少次数的情况下使字典序最小. 题解:贪心,\(0\)一定放在前面,\(1\)和\(2\ ...

  2. Balanced Ternary String CodeForces - 1102D (贪心+思维)

    You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...

  3. Balanced Ternary String(贪心+思维)

    题目链接:Balanced Ternary String 题目大意:给一个字符串,这个字符串只由0,1,2构成,然后让替换字符,使得在替换字符次数最少的前提下,使新获得的字符串中0,1,2 这三个字符 ...

  4. D - Balanced Ternary String (贪心)

    题目链接:http://codeforces.com/contest/1102/problem/D 题目大意:给你一个字符串,这个字符串是由0,1,2构成的,然后让你替换字符,使得在替换的次数最少的前 ...

  5. 牛客多校第四场 A Ternary String

    题目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a t ...

  6. 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]

    题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...

  7. CodeForces - 1009B Minimum Ternary String

    You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...

  8. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  9. B. Minimum Ternary String (这个B有点狠)

    B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input sta ...

  10. codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题

    http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...

随机推荐

  1. Python3+Robot Framework+RIDE安装使用教程

    一.说明 Python3----网上很多文章都是用Python2,Robot Framework的部分文档没更新也直接写着不支持Python3(如RIDE does not yet support P ...

  2. How to let your website login with domain account when using IIS to deploy it?

    如何让你的网站以域账号登录 Select your website in IIS Manager, open Authentication, enable Windows Authentication ...

  3. MarkDown的常规用法

    MarkDown的常规用法 标题 # 一级标题 ## 二级标题 ... ###### 六级标题 列表 第二级 - 和 空格 + 和 空额 * 和 空格 第三级 代码块 多行代码块 3个` 回车 单行代 ...

  4. 【题解】逆序排列 [51nod1020]

    [题解]逆序排列 [51nod1020] 传送门:逆序排列 \([51nod1020]\) [题目描述] 共 \(T\) 组测试点,每一组给出 \(2\) 个整数 \(n\) 和 \(k\),在 \( ...

  5. linux学习-防火墙指令

    Redhat7之前的版本(iptables) 开启关闭防火墙 放行端口 RedHat7防火墙相关的指令(firewall-cmd) 安装firewall 本文内容适用于 redhat 和 centos ...

  6. winform datagridview控件使用

    最近做项目时,显示查询结果总需要绑定到datagridview控件上显示,总结了给datagridview绑定数据的方式,以及导出datagridview数据到excel表格,如有错误请多指教 1.直 ...

  7. react 使用的小建议

    使用pureRender,setState和Immutable.js来操作state Immutable 中文意思不可变. 不能直接修改state的值,要用setState 和Immutable re ...

  8. linux环境:FTP服务器搭建

    转载及参考至:https://www.linuxprobe.com/chapter-11.html https://www.cnblogs.com/lxwphp/p/8916664.html 感谢原作 ...

  9. XML简述

    XML简述 本文主要内容都是在中国大学MOOC上学习的,这里做个记录. 课程:Java核心技术(进阶),华东师范大学 陈良育老师 感谢陈良育老师,在他的慕课上受益匪浅. XML基本概念 XML(eXt ...

  10. GitHub Python项目推荐|瓦力Devops开源项目代码部署平台持续部署

    GitHub Python项目推荐|walle - 瓦力 Devops开源项目代码部署平台 项目热度 标星(star):8418 (很不错的实用项目,大神作品,建议关注) 标星趋势 关注(watch) ...