Balanced Ternary String CodeForces - 1102D (贪心+思维)
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 a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').
Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.
Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.
It is guaranteed that the answer exists.
Input
The first line of the input contains one integer nn (3≤n≤3⋅1053≤n≤3⋅105, nn is divisible by 33) — the number of characters in ss.
The second line contains the string ss consisting of exactly nn characters '0', '1' and '2'.
Output
Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements.
Because nn is divisible by 33 it is obvious that the answer exists. And it is obvious that there is only one possible answer.
Examples
3
121
021
6
000000
001122
6
211200
211200
6
120110
题目链接 :https://vjudge.net/problem/CodeForces-1102D
题意:给你一个长度为N个字符串,N是3的倍数,字符串中只包括'0','1','2'这三个字符,
题目让你修改最少数量的字符,使这个字符串的中的这三个字符的数量相等,
即如果N=6,需要含有两个‘1’,两个‘2’,两个‘0’,并且希望你输出满足条件并且字典序最小的那一个。 思路:
贪心构造。
先预处理一下每一个字符出现了多少次,
然后遍历一下字符串,当0字符出现了大于n/3的时候,
把'0'最后一个出现的位置尝试改成‘2’,如果‘2’的数量不小于n/3,那么就改成'1'。之所以这个顺序,就是因为要求字典序最小,这样构造出来的最小。
‘1’,’2’类推该怎么构造。
既然我们每一次可能用到某一个字符第一次出现的位置或者最后一个出现的位置,我们就要开一个双端队列,然后预处理的时候把这个信息就加入到对应的双端中,
注意:这里讲的最后一次出现的,是对于刚预处理完的,如果每一次把他的最后一个出现的给改成别的字符了,那么就要从双端中弹出,
次后的继位。 细节可以看代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
char s[maxn];
char ans[maxn];
int cnt[];
int num[];
deque<int> v[];
int main()
{
gbtb;
cin>>n;
cin>>s;
strcpy(ans,s);
repd(i,,n-)
{
if(s[i]=='')
{
cnt[]++;
v[].pb(i);
}else if(s[i]=='')
{
cnt[]++;
v[].pb(i);
}else
{
v[].pb(i);
cnt[]++;
}
}
int x=n/;
// for(int i=n-1;i>=0;i--)
// {
// if(s[i]=='0')
// {
// if(cnt[0]>x)
// {
// if(cnt[2]<x)
// {
// s[i]='2';
// cnt[2]++;
//
// }else
// {
// s[i]='1';
// cnt[1]++;
// }
// cnt[0]--;
// }
// }
// }
repd(i,,n-)
{
if(s[i]=='')
{
if(cnt[]>x)
{
int index=v[].back();
v[].pop_back();
// *(--v[0].end());
// v[0].erase(--v[0].end());
if(cnt[]<x)
{
ans[index]='';
cnt[]++; }else
{
ans[index]='';
cnt[]++;
}
cnt[]--;
}
}
else if(s[i]=='')
{
if(cnt[]>x)
{
if(cnt[]<x)
{
int index=v[].back();
v[].pop_back();
ans[index]='';
cnt[]++; }else
{
int index=v[].front();
v[].pop_front();
ans[index]='';
cnt[]++;
}
cnt[]--;
}
}else
{
if(cnt[]>x)
{
int index=v[].front();
v[].pop_front();
if(cnt[]<x)
{ ans[index]='';
cnt[]++; }else
{
ans[index]='';
cnt[]++;
}
cnt[]--;
}
}
}
cout<<ans<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Balanced Ternary String CodeForces - 1102D (贪心+思维)的更多相关文章
- Balanced Ternary String(贪心+思维)
题目链接:Balanced Ternary String 题目大意:给一个字符串,这个字符串只由0,1,2构成,然后让替换字符,使得在替换字符次数最少的前提下,使新获得的字符串中0,1,2 这三个字符 ...
- codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...
- Codeforces Round #531 (Div. 3) D. Balanced Ternary String (贪心)
题意:给你一个长度为\(3*n\)的字符串,要求修改最少的次数,使得字符串中\(0,1,2\)的个数相同,并且在最少次数的情况下使字典序最小. 题解:贪心,\(0\)一定放在前面,\(1\)和\(2\ ...
- D - Balanced Ternary String (贪心)
题目链接:http://codeforces.com/contest/1102/problem/D 题目大意:给你一个字符串,这个字符串是由0,1,2构成的,然后让你替换字符,使得在替换的次数最少的前 ...
- Obtain The String CodeForces - 1295C binary_search+思维
妈耶,,,被B题卡到哭,C题一发就过了... 字符串问题.首先用vector记录每个字符出现的位置,然后对字符串t的每个字符,用二分查找函数查找,注意用upper_bound查找,对于字符i,首先用变 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
- CodeForces - 1009B Minimum Ternary String
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...
随机推荐
- iframe无刷新跨域上传文件并获得返回值
原文:http://geeksun.iteye.com/blog/1070607 需求:从S平台上传文件到R平台,上传成功后R平台返回给S平台一个值,S平台是在一个页面弹出的浮窗里上传文件,所以不能用 ...
- firefox event.preventDefault(); 没有效果的解决方案
$('.sub-list-click a').click(function (event) { event.preventDefault(); var sub = $(this).parent(&qu ...
- Jolt的是使用
1:简单入门例子 其中1为输入数据,其中2为spec,也就是输出json的格式规范,3为输出数据.重点关注4和5即可: 其中4是rating.quality.value的表示,rating.quali ...
- Color the ball HDU - 1556 (非线段树做法)
题意:在1到n的气球中,在不同的区域中涂颜色,问每个气球涂几次. #include<cstdio>int num[100010];int main(){ int n, x, y;; whi ...
- clearRect清除html5画布
html5 canvas 清除可以使用clearRect() 方法 clearRect() 方法的作用是清空给定矩形内的指定像素.JavaScript 语法:context.clearRect(x,y ...
- Qt5中的lambda表达式和使用lambda来写connect
c11新特性中加入了lambda表达式,所以Qt 也支持 需在.pro文件中加入 CONFIG += c++11 例子: QString program = "C:/Windows/Syst ...
- Linux命令——df/du/time
一.df(disk free) df命令可以用来检查 linux服务器的文件系统的磁盘空间占用情况,可以知道硬盘被占用了多少空间,目前还剩下多少空间等信息. 1)命令格式 df [参数] 文件名 2) ...
- 人人都是产品经理<2.0>
之前有看过<人人都是产品经理1.0>,还认真的做了笔记,看完后不久,得知作者在第一版的内容基础上,升华性的出了第二版,即<人人都是产品经理2.0>.注:第一版和第二版跨度有6年 ...
- 2-微信小程序开发(开发界面说明,按钮点击切换显示内容)
说一个功能,大家在用微信实现控制设备的时候,是不是都在为绑定设备发愁. 我看了很多厂家的微信控制,大部分都只是可以用微信给设备配网,但是没有做用微信绑定的. 一般做绑定都是用设备的MAC地址. 这里我 ...
- java 之UDP编程
大白话:每一台电脑都有自己的ip地址,向指定的ip地址发数据,数据就发送到了指定的电脑.UDP通信只是一种通信方式而已,其特点就不多说.有了ip地址数据就能发送到指定的电脑了,但是呢!我把数据发送到电 ...