CF1102D-Balanced Ternary String-(贪心)
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-(贪心)的更多相关文章
- Codeforces Round #531 (Div. 3) D. Balanced Ternary String (贪心)
题意:给你一个长度为\(3*n\)的字符串,要求修改最少的次数,使得字符串中\(0,1,2\)的个数相同,并且在最少次数的情况下使字典序最小. 题解:贪心,\(0\)一定放在前面,\(1\)和\(2\ ...
- Balanced Ternary String CodeForces - 1102D (贪心+思维)
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...
- Balanced Ternary String(贪心+思维)
题目链接:Balanced Ternary String 题目大意:给一个字符串,这个字符串只由0,1,2构成,然后让替换字符,使得在替换字符次数最少的前提下,使新获得的字符串中0,1,2 这三个字符 ...
- D - Balanced Ternary String (贪心)
题目链接:http://codeforces.com/contest/1102/problem/D 题目大意:给你一个字符串,这个字符串是由0,1,2构成的,然后让你替换字符,使得在替换的次数最少的前 ...
- 牛客多校第四场 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 ...
- CodeForces - 1009B Minimum Ternary String
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...
- 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)
链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...
- B. Minimum Ternary String (这个B有点狠)
B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input sta ...
- codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...
随机推荐
- 使用另一个版本的glibc
glibc是Linux系统的核心库,稍有不慎就会导致系统崩溃.如果在程序中必须使用另一版本的glibc,则需要小心从事.具体来言,是在编译时指定--rpath和--dynamic-linker,而在运 ...
- 配置opencv cmake
第一种使用 find_package的方法示例代码如下:# 声明要求的 cmake 最低版本cmake_minimum_required( VERSION 2.8 ) # 声明一个 cmake 工程p ...
- 最新修改Oracle10gscott用户
1.以system登录及输入自己设置口令; 2.更换sysdba身份: conn system/orcl as sysdba; 3.解锁scott用户(因装好默认是锁定的): alter user s ...
- 300iq Contest 1 简要题解
300iq Contest 1 简要题解 咕咕咕 codeforces A. Angle Beats description 有一张\(n\times m\)的方阵,每个位置上标有*,+,.中的一种. ...
- FusionInsight大数据开发---Flume应用开发
Flume应用开发 要求: 了解Flume应用开发适用场景 掌握Flume应用开发 Flume应用场景Flume的核心是把数据从数据源收集过来,在送到目的地.为了保证输送一定成功,发送到目的地之前,会 ...
- SQL Server优化之SET STATISTICS开关(转载)
一.准备工作 缓存对于某个查询的性能影响十分之大,所以优化之前要清空缓存. 清除Buffer Pool里面的所有缓存 DBCC DROPCLEANBUFFERS 清除Buffer Pool里的所有缓存 ...
- 局域网访问PHP项目网站 用IP地址进入
先在apache中的 httpd.conf中将 Allow from 127.0.0.1 修改为Allow from all 如果你的是Allow from all的话就不需要改 然后再将 Docum ...
- Redis高级功能-1、高并发基本概述
1.可能的问题 要将redis运用到工程项目中,只使用一台redis是万万不能的,原因如下: (1)从结构上,单个redis服务器会发生单点故障,并且一台服务器需要处理所有的请求负载,压力较大. (2 ...
- C#读写设置修改调整UVC摄像头画面-光圈
有时,我们需要在C#代码中对摄像头的光圈进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...
- C#读写设置修改调整UVC摄像头画面-增益
有时,我们需要在C#代码中对摄像头的增益进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...