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'). ...
随机推荐
- SQL Server2008 18456错误
1.以windows验证模式进入数据库管理器. 第二步:右击sa,选择属性: 在常规选项卡中,重新填写密码和确认密码(改成个好记的).把强制实施密码策略去掉. 第三步:点击状态选项卡:勾选 ...
- pb中遍历查询数据库数据问题(数据库为 sql server)
指针可以实现但是不推荐 例如:(部分代码) for ll_a = 1 to ll_count ll_b = ll_i + ll_a //插入行行号先下移一位 dw_main.inser ...
- Linux下编辑、编译、调试命令总结——gcc和gdb描述
GCC gcc是linux系统集成的编译器.在linux环境下编辑程序,首先需要克服的便是没有集成开发环境的一键式操作所带来的麻烦.这其中涉及命令行操作.编译选项的设定.文件依赖关系的书写(makef ...
- 转://Linux下区分物理CPU、逻辑CPU和CPU核数
㈠ 概念 ① 物理CPU 实际Server中插槽上的CPU个数 物理cpu数量,可以数不重复的 p ...
- metamask源码学习-controller-transaction
()metamask-extension/app/scripts/controllers/transactions Transaction Controller is an aggregate of ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- Linux中运行SpringBoot项目,永久运行
将写好的springboot项目打成jar包: 项目右键 -- Run As -- Maven build... ---此时出现下图 1.Goals 中填写:install 2.Skip Tests复 ...
- Python基础(8)——常见模块
模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configparser has ...
- 深入浅出的webpack4构建工具---比mock模拟数据更简单的方式(二十一)
如果想要了解mock模拟数据的话,请看这篇文章(https://www.cnblogs.com/tugenhua0707/p/9813122.html) 在实际应用场景中,总感觉mock数据比较麻烦, ...
- 【Codeforces Round 1114】Codeforces #538 (Div. 2)
Codeforces Round 1114 这场比赛做了\(A\).\(C\).\(D\).\(E\),排名\(134\). \(B\)题做了很长时间,好不容易最后一分钟\(Pretest\ Pass ...