一、来源:Problem - C - Codeforces

二、题面

三、思路

  1. 先考虑一个子问题模型:我们现在有用\(m_1\)种随机字母组成的n个数,各字母个数未定,现在需要使这n个数变为\(m_2\)种类,平均每个种类为blance=n/\(m_2\)个数,问如何求得最小的改变数

    • 显然,只能由多的数向少的数改变,为此我们先尝试求出所有所有字母出现的次数并对其进行降序排序得到vector<pair<int,int>> v;\\pair的first为字母小标(由0开始),second为字母出现的个数

    • 我们考虑转换的过程,其实就是高位-1,低位+1的过程.

      • 尝试只找需要减去的位,初一看我们在前面高位用v[i].second减去blance直到v[i].second<=blance即可,然而事实上这情况讨论过的前提是\(m_1<m_2\);当\(m_1>m_2\),以数列9,3,3,3,2为例,我么将其转为为5,5,5,5,,最前面的9需要减去,后面的2也需要减去,这就比较复杂了
      • 尝试只找需要增加的位:根据上面错误的经验,我们可以得到需要增加的部分都是在m2之前的,且都可以使用表达式ans+=blance-v[i].second.经代码验证可以得到复合题意的解
    • code

      #include<bits/stdc++.h>
      
      using namespace std;
      
      bool cmp(const pair<int,int> a,pair<int,int> b){
      return a.second > b.second;
      } char toChar(int offset){
      return 'a'+offset;
      } int toInt(char c){
      return c-'a';
      } int main(){
      int t;
      cin >> t;
      while(t--){
      int n,m2; //n个数转换为m种,每种n/m个
      char str[100];
      cin >> n >> m2;
      cin >> str;
      map<int,int> mp;
      for(int i=0;i<26;i++){ //需初始化
      mp[i]=0;
      }
      int m1=0;
      for(int i=0;i<n;i++){
      if(++mp[toInt(str[i])]==1){
      m1++;
      }
      }
      vector<pair<int,int>> v(mp.begin(),mp.end());
      sort(v.begin(),v.end(),cmp);
      int ans=0,blance=n/m2;
      for(int i=0;i<m2;i++){
      if(v[i].second<blance){
      ans+=blance-v[i].second;
      }
      }
      cout << ans << endl;
      }
      return 0;
      }

    注:该问题模型其实还可以简化为:n个和为sum的数,每次加一减一,通过加减操作转化为m个相同的数,每个数为sum/m,所需要的最少加减次数

  2. 将问题分解:本题注意到小写字母是可以遍历的,我们可以遍历26种可能的情况求得变化最小的一次解的值,然后再对字符串进行变换

    • 遍历26种可能并求得变化最小的一个解
    • 模拟求变换即可(这里的代码属于是暴力了)
  3. 补充:对于toChar,toInt,最好还是封装为函数

四、代码

#include <bits/stdc++.h>
#define eleType int
#define INF 0x3f3f3f3f typedef long long ll; using namespace std; //字母遍历
//设字母总数为n,平等值为m => n<26*m且n%m==0 const int N=1e5+10;
char arr[N];
map<int,int> mp; bool cmp(const pair<int,int> a,pair<int,int> b){
return a.second > b.second;
} char toChar(int offset){
return 'a'+offset;
} int toInt(char c){
return c-'a';
} int main(){
int t;
cin >> t;
while(t--){
eleType n;
cin >> n >> arr;
for(int i=0;i<26;i++){ //需初始化
mp[i]=0;
}
int m1=0;
for(int i=0;i<n;i++){
if(mp[toInt(arr[i])]++==0){
m1++;
}
}
vector<pair<int,int>> v(mp.begin(),mp.end());
sort(v.begin(),v.end(),cmp);
eleType ans=INF,m2=1,blance;
for(int i=1;i<=26;i++){
if(n%i==0){ //i是种类,不是个数,不需要保证26*i>=n
// cout << "i:" << i << endl;
eleType temp=0,now=0;
blance=n/i;
for(int j=0;j<i;j++){
//cout << "mp[" << j << "]:" << mp[j] << endl;
if(v[j].second<blance){
temp+=blance-v[j].second;
}
}
// cout << temp << endl;
if(temp<ans){
ans=temp;
m2=i;
}
// cout << "i:" << i << " temp:" << temp << endl;
}
}
blance = n/m2;
eleType next=0;
for(int i=0;i<v.size();i++){
if(v[i].second<blance){
next=i;
break;
}
}
// cout << m2 << endl;
for(int i=0;i<n;i++){
eleType now;
for(int j=0;j<v.size();j++){
if(arr[i] == toChar(v[j].first)){ //易错漏,转换应封装为函数
now=j; //排序后的位置
}
}
if(v[now].second>blance||now>=m2){ //多余数,后者所在的种类位次超过m2
//在前面或后面
v[now].second--;
// cout << "v[next].first:" << v[next].first << endl;
arr[i]=toChar(v[next].first);
if(++v[next].second==blance){
next++;
} }
}
cout << ans << endl;
cout << arr << endl;
}
return 0;
}

Codeforces Round #844:C. Equal Frequencies的更多相关文章

  1. Codeforces Round #844 (Div.1 + Div.2) CF 1782 A~F 题解

    点我看题 A. Parallel Projection 我们其实是要在这个矩形的边界上找一个点(x,y),使得(a,b)到(x,y)的曼哈顿距离和(f,g)到(x,y)的曼哈顿距离之和最小,求出最小值 ...

  2. Codeforces Round #844 (Div. 1 + Div. 2, based on VK Cup 2022 - Elimination Round) A-D

    比赛链接 A 题意 设计一条线路要贴着6个墙面走,从 \((a,b)\) 到 \((f,g)\) ,线路长度最短. 题解 知识点:模拟. 分类取最短即可. 时间复杂度 \(O(1)\) 空间复杂度 \ ...

  3. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  4. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  5. Codeforces Round #284 (Div. 2)A B C 模拟 数学

    A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #270 1003

    Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...

  7. Codeforces Round #270 1002

    Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...

  8. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)

    Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...

  10. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

随机推荐

  1. 面向对象之trait

    面向对象之trait 场景 一个web站点,它有很多不同的类:用户(User).页面(Page).联系表单(ContactFrom)等.我们可能需要在每个类中添加一个方法的定义,但是这样的话就会造成不 ...

  2. 单片机 IAP 功能基础开发篇之APP升级(三)

    1 前言 上一篇单片机 IAP 功能基础开发篇之APP升级(二)讲到了单片机给 APP 程序升级具体的设计方案,这篇介绍的是升级进阶功能,如何在编译后在程序结束地址自动加上校验标志(可以通过脚本工具, ...

  3. 复制对象句柄DuplicateHandle(文件占坑)

    DuplicateHandle文档化解释 The DuplicateHandle function duplicates an object handle. The duplicate handle ...

  4. 《ASP.ENT Core 与 RESTful API 开发实战》-- (第4章)-- 读书笔记(下)

    第 4 章 资源操作 4.5 创建资源 由于创建资源的 Id 会在服务端生成,因此在创建资源时,不建议使用与获取数据时相同的 DTO,而要单独创建一个新的 DTO 类,并通过数据注解特性对相应 的属性 ...

  5. Linux离线安装MySQL(5.7.22)

    1.下载tar包 (1)Window PC下载(PC需要联网) MySQL官网地址:https://www.mysql.com/ MySQL社区版下载地址: https://dev.mysql.com ...

  6. CF-926(已更新:B)

    CF-926 两点睡,七点起,阎王夸我好身体-- 主要这场实在是难绷,两个小时都在C题上吊死了,也不是没想过跳题,只是后面的题我更是一点思路都没有-^- "就喜欢这种被揭穿的感觉,爽!&qu ...

  7. 玩转 CMS2

    玩转 CMS2 上篇研究了样式.请求.evn.mock,感觉对效率的提升没有太明显作用. 比如某个工作需要2天,现在1天可以干完,这就是很大的提升. 提高效率的方法有代码复用.模块化.低代码工具. 目 ...

  8. ElementUI导出表格数据为Excel文件

    功能介绍 将列表的数据导出成excel文件是管理系统中非常常见的功能.最近正好用到了ElementUI+Vue的组合做了个导出效果,拿出来分享一下,希望可以帮到大家:) 实现效果 实现步骤 1.定义导 ...

  9. centos7.5 hadoop NAT 静态IP网络环境搭建

    1 设置 VMware 网络环境 1. 选择VMNet8 并将子网IP 修改为 192.168.10.0,保证集群ip都在这个网段下 2. 选择NAT 设置,配置NAT的网关为 192.168.10. ...

  10. pikachu sql inject delete 注入

    留言板输入几条信息 出现删除按钮,点他 通过burpsuite拦截请求,请求报文如下 GET /vul/sqli/sqli_del.php?id=57 HTTP/1.1 Host: 192.168.1 ...