题意:给定一个长度为偶数的数,输出小于它的最大的美丽数。如果一个数长度为偶数,且没有前导零,并存在一种排列是回文数的数为美丽数。给定的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 (贪心)的更多相关文章

  1. Largest Beautiful Number CodeForces - 946E (贪心)

    大意: 定义一个好数为位数为偶数, 且各位数字重排后可以为回文, 对于每个询问, 求小于$x$的最大好数. 假设$x$有$n$位, 若$n$为奇数, 答案显然为$n-1$个9. 若为偶数, 我们想让答 ...

  2. 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 开始 ...

  3. Beautiful Paintings CodeForces - 651B (贪心)

    大意: 给定序列$a$, 可以任意排序, 求最大下标i的个数, 满足$a_i<a_{i+1}$. 这个贪心挺好的, 答案就为n-所有数字出现次数最大值.

  4. Codeforces 55D Beautiful Number

    Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...

  5. beautiful number 数位DP codeforces 55D

    题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...

  6. Vasya and Beautiful Arrays CodeForces - 354C (数论,枚举)

    Vasya and Beautiful Arrays CodeForces - 354C Vasya's got a birthday coming up and his mom decided to ...

  7. D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)

    D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...

  8. zoj Beautiful Number(打表)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2829 题目描述: Mike is very lucky, as ...

  9. zoj 2829 Beautiful Number

    Beautiful Number Time Limit: 2 Seconds      Memory Limit: 65536 KB Mike is very lucky, as he has two ...

随机推荐

  1. 大数据篇:MapReduce

    MapReduce MapReduce是什么? MapReduce源自于Google发表于2004年12月的MapReduce论文,是面向大数据并行处理的计算模型.框架和平台,而Hadoop MapR ...

  2. PAT T1013 Image Segmentation

    krustral算法加并查集,按题给要求维护并查集~ #include<bits/stdc++.h> using namespace std; ; const int inf=1e9; i ...

  3. DCL和DQL

    数据查询语言(DQL,Data Query Language) 主要是一些查询的sql语句. 语法 select * from test: 数据控制语言(DCL, Data Control Langu ...

  4. 解决HTML5(富文本内容)连续数字、字母不自动换行

    最近开发了一个与富文本相关的功能,大概描述一下:通过富文本编辑器添加的内容,通过input展示出来(这里用到了 Vue 的 v-html 指令). 也是巧合,编辑了一个只有数字组成的长文本,等到展示的 ...

  5. tensorflow中的Fetch、Feed(02-3)

    import tensorflow as tf #Fetch概念 在session中同时运行多个op input1=tf.constant(3.0) #constant()是常量不用进行init初始化 ...

  6. Codeforces 601A:The Two Routes 宽搜最短路径

    A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. PAT A 1020 Tree Traversals

    给出一棵二叉树的后序遍历序列和中序遍历序列,求这棵二叉树的层序遍历序列 #include<iostream> #include<cstring> #include<que ...

  8. Linux centos7 linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍、 target介绍

    一.linux任务计划cron crontab -u  -e -l -r 格式;分 时 日 月 周 user command 文件/var/spool/corn/username 分范围0-59,时范 ...

  9. 好用的log打印类

    package com.huawei.network.ott.weixin.util; import android.util.Log; public final class DebugLog { / ...

  10. 1 网页及浏览器内核&Web标准

    网页的组成: 主要由文字.图像和超链接等元素构成,还可以包含音频.视频以及flash. 浏览器内核: 浏览器内核分为两部分: 1 渲染引擎(layout engineer) 渲染引擎负责取得网页的内容 ...