https://codeforces.com/contest/1102/problem/D

题解:

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=300000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans,cnt,flag,temp;
int a[3];
char str[N];
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endifscanf("%d",&n);scanf("%s",str);//while(t--){}for(int i=0;i<n;i++){if(str[i]=='0')a[0]++;else if(str[i]=='1')a[1]++;else if(str[i]=='2')a[2]++;}m=n/3;if(a[0]<m){if(a[2]>m)for(int i=0;i<n;i++){if(str[i]=='2'){str[i]='0';a[0]++;a[2]--;if(a[0]==m||a[2]==m)break;}}if(a[1]>m&&a[0]<m)for(int i=0;i<n;i++){if(str[i]=='1'){str[i]='0';a[0]++;a[1]--;if(a[0]==m||a[1]==m)break;}}}if(a[2]<m){if(a[1]>m)for(int i=n-1;i>=0;i--){if(str[i]=='1'){str[i]='2';a[2]++;a[1]--;if(a[2]==m||a[1]==m)break;}}if(a[0]>m&&a[2]<m)for(int i=n-1;i>=0;i--){if(str[i]=='0'){str[i]='2';a[2]++;a[0]--;if(a[2]==m||a[0]==m)break;}}}if(a[1]<m){if(a[0]>m)for(int i=n-1;i>=0;i--){if(str[i]=='0'){str[i]='1';a[1]++;a[0]--;if(a[0]==m||a[1]==m)break;}}if(a[2]>m&&a[1]<m)for(int i=0;i<n;i++){if(str[i]=='2'){str[i]='1';a[1]++;a[2]--;if(a[1]==m||a[2]==m)break;}}}cout<<str<<endl;//cout << "Hello world!" << endl;return 0;
}

Balanced Ternary String相关推荐

  1. CSL分苹果 小埋与扫雷 Doors Breaking and Repairing Key Set Balanced Ternary String

    第一题CSL分苹果 思路:基础动态规划,设m为所有苹果的数量和,dp[x]为,容量为x时候能分的最多苹果数目,那么wavator分到的最多苹果数目就是dp[m/2],tokitsukaze分到的苹果数 ...

  2. Minimum Ternary String CodeForces - 1009B(思维)

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

  3. 牛客网暑期ACM多校训练营(第三场): A. Ternary String(欧拉降幂+递推)

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

  4. B. Ternary String

    B. Ternary String B.三元字符串 time limit per test: 2 seconds time limit per test: 2 seconds memory limit ...

  5. Ternary String

    思路 题意:求包含1,2,3的区间最小长度 做法:我用了最笨的方法,记录下距离i点最近的1,2,3的位置,一个是在i左边最近的1,2,3位置,一个是在i右边最近的1,2,3的位置,之后枚举一下每一个位 ...

  6. Ternary Search Trees 三分树

    Efficient auto-complete with a ternary search tree 分类: 算法和数据结构学习 2012-04-18 18:03  125人阅读  评论(0)  收藏 ...

  7. Educational Codeforces Round 47 (Div 2) (A~G)

    目录 Codeforces 1009 A.Game Shopping B.Minimum Ternary String C.Annoying Present D.Relatively Prime Gr ...

  8. 字符串处理 —— 模拟与暴力

    [概述] 在某些题目中,需要对字符串进行处理,不需要很复杂的算法,只需要按照题意进行模拟或暴力枚举即可得出答案. [例题] 1.简单模拟 Minimum Ternary String(CF-1009B ...

  9. 一文了解二进制和CPU工作原理

    Part 1. 原来,我们是这样记数的 " 本节内容节选自下方 参考资料 1 在讨论「二进制」和「CPU 如何工作」之前,我们先来讨论一下我们生活中最稀疏平常的 数字,我们与之频繁地打交道: ...

最新文章

  1. BW标准数据源初始化设置
  2. 前沿地带:从量子计算到量子互联网
  3. 环境图配置不存在pbr_小米11再曝光,硬件参数不存在短板,完全最高旗舰配置...
  4. 为什么python画不了图-python-为什么pygame不画一个圆?
  5. 随笔-使用时间管理有感
  6. 如何在python中对列表套列表进行排序_如何实现对Python中列表的排序?
  7. Dubbo Admin —— Spring Cloud Alibaba 2021.1 + Nacos + Dubbo Admin参考配置
  8. Linux系统正则表达式用法笔记
  9. Ajax和Jsonp实践
  10. 怎样快速画出一个正方体_图形与几何---正方体
  11. JavaWeb——Get、Post请求中文乱码问题
  12. HEVC官方代码下载及码流分析软件使用
  13. Phase2 Day23 数据库连接池DbUtils
  14. hive、hadoop面试题
  15. CCS中CMD文件详解
  16. HDU-4826(DP动态)
  17. “我一年赚100万,这个项目告诉你也无妨!”
  18. 【Android】图像滤镜框架GPUImage从配置到应用
  19. RGB转LVDS芯片
  20. 支付宝存漏洞?这10招保护个人信息赶紧保存起来!

热门文章

  1. rz安装 xshell_利用XShell上传、下载文件(使用sz与rz命令)
  2. homepod怎么设置为中文_设置 HomePod
  3. 电子测量与仪器第四版pdf_固定资产管理系统_资产分类名称(电子和通信测量分析仪器篇)...
  4. R星服务器修改声望,从《荒野大镖客2》Online更新,我们可以看出R星的新思路
  5. 坐标偏差大_控制点的坐标复核(二)
  6. android 相册选择视频和图片格式,Android获取文件类型是图片还是视频
  7. 打地鼠c语言代码_女白领在家玩打地鼠游戏,无意间学会python编程,还有教程有源码...
  8. python关联分析引擎_PowerBI x Python 之关联分析(上)
  9. Java在开发中应注意的问题_Java设计编程应该注意的几个问题
  10. 设置css3动画的顺序,CSS3 “瓷砖”顺序飘落的动画