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

  • 题解:贪心,\(0\)一定放在前面,\(1\)和\(2\)放后面,首先统计\(0,1,2\)的个数,因为题目要求字典序最小,所以我们先从左边开始遍历,如果\(2\)的个数大于\(n/3\),那么再看\(0\)和\(1\)的个数情况将其替换成\(0\)或\(1\),对\(1\)也是如此,然后我们再反着遍历,首先考虑\(1\)的情况,再考虑\(0\)的情况.具体的细节看代码吧.

  • 代码:

    int n;
    char s[N];
    map<int,int> mp; int main() {
    //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    n=read();
    scanf("%s",s+1);
    int cnt=n/3;
    for(int i=1;i<=n;++i){
    if(s[i]=='0') mp[0]++;
    else if(s[i]=='1') mp[1]++;
    else mp[2]++;
    } for(int i=1;i<=n;++i){
    if(s[i]=='2'){
    if(mp[2]>cnt){
    if(mp[0]<cnt) s[i]='0',mp[0]++,mp[2]--;
    else if(mp[1]<cnt) s[i]='1',mp[1]++,mp[2]--;
    }
    }
    else if(s[i]=='1'){
    if(mp[1]>cnt){
    if(mp[0]<cnt) s[i]='0',mp[0]++,mp[1]--;
    }
    }
    } for(int i=n;i>=1;--i){
    if(s[i]=='1' && mp[1]>cnt){
    if(mp[2]<cnt) s[i]='2',mp[2]++,mp[1]--;
    }
    else if(s[i]=='0' && mp[0]>cnt){
    if(mp[2]<cnt) s[i]='2',mp[2]++,mp[0]--;
    else if(mp[1]<cnt) s[i]='1',mp[1]++,mp[0]--;
    }
    } for(int i=1;i<=n;++i){
    printf("%c",s[i]);
    } return 0;
    }

Codeforces Round #531 (Div. 3) D. Balanced Ternary String (贪心)的更多相关文章

  1. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  2. Codeforces Round #184 (Div. 2) E. Playing with String(博弈)

    题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...

  3. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  4. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

  5. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  6. Codeforces Round #877 (Div. 2) B. - Nikita and string

    题目链接:http://codeforces.com/contest/877/problem/B Nikita and string time limit per test2 seconds memo ...

  7. Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

    F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...

  8. Codeforces Round #544 (Div. 3) C. Balanced Team

    链接:https://codeforces.com/contest/1133/problem/C 题意: 给n个数, 在这n个数中选最多n个数,来组成一个队伍. 保证这n个数的最大最小差值不大于5. ...

  9. Codeforces Round #531 (Div. 3)

    A:瞎猜. #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); i ...

随机推荐

  1. Azure Table Storage(一) : 简单介绍

    Azure Table Storage是什么: Azure Table Storage是隶属于微软Azure Storage这个大服务下的一个子服务, 这个服务在Azure上算是老字号了, 个人大概在 ...

  2. 如何实现一个简易版的 Spring - 如何实现 Constructor 注入

    前言 本文是「如何实现一个简易版的 Spring」系列的第二篇,在 第一篇 介绍了如何实现一个基于 XML 的简单 Setter 注入,这篇来看看要如何去实现一个简单的 Constructor 注入功 ...

  3. 通过JS逆向ProtoBuf 反反爬思路分享

    前言 本文意在记录,在爬虫过程中,我首次遇到Protobuf时的一系列问题和解决问题的思路. 文章编写遵循当时工作的思路,优点:非常详细,缺点:文字冗长,描述不准确 protobuf用在前后端传输,在 ...

  4. 【Linux】ntp的一些坑。你肯定遇到过

    ntpdate提示 19 Jan 10:33:11 ntpdate[29616]: no server suitable for synchronization found 这种问题从下面几个点开始验 ...

  5. LeetCode543.二叉树的直径

    题目 1 class Solution { 2 public: 3 int minimum = INT_MIN; 4 vector<int>res; 5 int diameterOfBin ...

  6. Python输出有颜色的文字

    原创链接: https://www.cnblogs.com/easypython/p/9084426.html   我们在使用python运维与开发的过程中,经常需要打印显示各种信息.海量的信息堆砌在 ...

  7. layui表格前端格式化时间戳字段

    layui.use(['util','table'], function(){   var table = layui.table;   var util = layui.util;   //... ...

  8. 阅读lodash源码之旅数组方法篇-compact和concat

    鲁迅说过:只有阅读过优秀库源码的人,才能配的上是真正的勇士. compact 创建一个新数组,包含原数组中所有的非假值元素.例如false, null,0, "", undefin ...

  9. vue-cli3x4x修改本地端口port

    一.推荐方法 "scripts": { "serve": "vue-cli-service serve --port 3000", &quo ...

  10. python 编译EXE文件

    以labelme测试 标注工具labelimg和labelme 矩形标注工具:labelimg 多边形标准工具:labelme 前者官网发布了可执行文件,后者只有python源码,如果需要编译wind ...