Largest Beautiful Number CodeForces - 946E (贪心)
题意:给定一个长度为偶数的数,输出小于它的最大的美丽数。如果一个数长度为偶数,且没有前导零,并存在一种排列是回文数的数为美丽数。给定的t个数长度总和不超过200000.
分析:
1、存在一种排列为回文数,即这个数包含的数字都是偶数个。
2、预处理出每个数字的个数。
3、从最右边一位依次往左枚举,当逐渐减小第i位时,统计目前的数中,第i位之前有多少个数字是奇数个,记为cnt。
4、因为第i位之后的数字可以随便填,所以如果第i位之后数字的个数大于等于cnt,那么则可以得出答案,大于的部分先用9填充,再从大到小依次用奇数个的数字填充即可。
5、如果枚举到最左边也没有答案,则直接输出数字长度-2个9即可。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200000 + 10;
char s[MAXN];
map<int, int> mp;
int len;
vector<int> v;
bool judge(){
for(int i = len - 1; i >= 0; --i){
int t;
if(i == 0) t = 1;
else t = 0;
--mp[s[i] - '0'];
for(int j = s[i] - '0' - 1; j >= t; --j){
++mp[j];
v.clear();
for(int k = 0; k < 10; ++k){
if(mp[k] & 1){
v.push_back(k);
}
}
int l = v.size();
if(l <= len - i - 1){
for(int k = 0; k < i; ++k){
printf("%c", s[k]);
}
printf("%d", j);
for(int k = 0; k < len - i - 1 - l; ++k){
printf("9");
}
sort(v.begin(), v.end());
for(int k = l - 1; k >= 0; --k){
printf("%d", v[k]);
}
printf("\n");
return true;
}
--mp[j];
}
}
return false;
}
int main(){
int t;
scanf("%d", &t);
while(t--){
mp.clear();
scanf("%s", s);
len = strlen(s);
for(int i = 0; i < len; ++i){
++mp[s[i] - '0'];
}
if(!judge()){
for(int i = 0; i < len - 2; ++i){
printf("9");
}
printf("\n");
}
}
return 0;
}
Largest Beautiful Number CodeForces - 946E (贪心)的更多相关文章
- Largest Beautiful Number CodeForces - 946E (贪心)
大意: 定义一个好数为位数为偶数, 且各位数字重排后可以为回文, 对于每个询问, 求小于$x$的最大好数. 假设$x$有$n$位, 若$n$为奇数, 答案显然为$n-1$个9. 若为偶数, 我们想让答 ...
- Educational Codeforces Round 39 (Rated for Div. 2) 946E E. Largest Beautiful Number
题: OvO http://codeforces.com/contest/946/problem/E CF 946E 解: 记读入串为 s ,答案串为 ans,记读入串长度为 len,下标从 1 开始 ...
- Beautiful Paintings CodeForces - 651B (贪心)
大意: 给定序列$a$, 可以任意排序, 求最大下标i的个数, 满足$a_i<a_{i+1}$. 这个贪心挺好的, 答案就为n-所有数字出现次数最大值.
- Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...
- beautiful number 数位DP codeforces 55D
题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...
- Vasya and Beautiful Arrays CodeForces - 354C (数论,枚举)
Vasya and Beautiful Arrays CodeForces - 354C Vasya's got a birthday coming up and his mom decided to ...
- D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)
D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...
- zoj Beautiful Number(打表)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2829 题目描述: Mike is very lucky, as ...
- zoj 2829 Beautiful Number
Beautiful Number Time Limit: 2 Seconds Memory Limit: 65536 KB Mike is very lucky, as he has two ...
随机推荐
- CSS背景透明设置
style="margin-top:300px;background:rgba(255,255,255,这里设置小于1比如0.6这样); color:black;" style=& ...
- 使用IDEA导入一个Maven风格的SSM项目
转自: 方法一: (我用的这种,导入的方法 File->New->Project from existing sources)(同理,important也是一样的) https://how ...
- 「JSOI2014」序列维护
「JSOI2014」序列维护 传送门 其实这题就是luogu的模板线段树2,之所以要发题解就是因为学到了一种比较NB的 \(\text{update}\) 的方式.(参见这题) 我们可以把修改操作统一 ...
- Navigating to current location ("/") is not allowed
main.js import Router from 'vue-router' // 这个是为了避免一个报错 const originalPush = Router.prototype.push; R ...
- PTA的Python练习题(五)
昨天耽搁了一天,今天继续 从 第3章-5 字符转换 开始 1. a=input() b="" for i in a: ': b=b+iprint(int(b))#(写成b=i+b ...
- lua叠代器
注意:叠待值遇到nil就退出 叠代器,是符合for遍历框架,需要满足条件 1-叠代函数,常量,控制变量 2-叠代函数可以接受二个参数,当然也可以忽略处理(利用闭包封装参数作为控制变量和状态变量) 无状 ...
- Kali Linux RPi0w Nexmon操作笔记
Kali Linux RPi0w Nexmon镜像下载地址:https://www.offensive-security.com/kali-linux-arm-images/ 1.烧录系统 可参考:树 ...
- Spring学习(一)
搭建环境 1.创建普通的Java工程 2.添加相应的jar包,下载链接:https://files.cnblogs.com/files/AmyZheng/lib.rar,此外,为了打印信息,我们还需要 ...
- 【转载】手把手教你使用Git(简单,实用)
手把手教你使用Git(简单,实用) 标签: git 2016年04月21日 20:51:45 1328人阅读 评论(0) 收藏 举报 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. ...
- python学习第一课
第一课: 1.不要使用来路不明的软件 2.下载杀毒软件 3.不懂技术的人在技术人面前会显得愈发无知 4.python无所不能 需要掌握的知识: 1.python基本语法 2.文件处理 3.函数 4.模 ...