大意: 定义一个好数为位数为偶数, 且各位数字重排后可以为回文, 对于每个询问, 求小于$x$的最大好数.

假设$x$有$n$位, 若$n$为奇数, 答案显然为$n-1$个9. 若为偶数, 我们想让答案尽量大, 那么就要尽量调整$x$的低位数, 从低位到高位遍历, 假设当前处理到第$i$位, 对于判断重排后回文可以用一个长为10的二进制数来判断, 假设$[i+1,n]$状态为$t$, 那么问题就转化为求最大的小于$t$且二进制状态为$s$的数, 贪心填数即可, 若对于每一位都无解答案就为$n-2$个9.

第一次写这种涉及数位的贪心模拟, 感觉还是挺烦的, 进位要多注意, 贪心时的因为要多次进行可行性判断, 一定要写成函数.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif char s[N];
int n, f[N], cnt[N], num[N];
int pos[N], p[15]; int chk(int i, int sta, int flag) {
if (i>n) return 1;
int sz = 0;
REP(i,0,9) if (sta>>i&1) p[++sz]=i;
if (sz>n-i+1||(sz&1)!=(n-i+1&1)) return 0;
if (flag) return 1;
if (i+pos[i]-1+sz>n) return 0;
if (i+pos[i]-1+sz==n) {
int ok = 0;
REP(j,i+pos[i],n) {
if (s[j]<p[j-i-pos[i]+1]) return 0;
if (s[j]>p[j-i-pos[i]+1]) return 1;
}
return 0;
}
return 1;
} void work() {
scanf("%s", s+1);
n = strlen(s+1);
if (n&1) {
REP(i,1,n-1) putchar('9');hr;
return;
}
REP(i,1,n) s[i]-='0';
REP(i,1,n) f[i]=f[i-1]^(1<<s[i]);
pos[n+1]=0;
PER(i,1,n) pos[i]=s[i]?0:pos[i+1]+1;
PER(i,1,n) {
int sta = f[i-1];
if (!chk(i,sta,0)) continue;
int flag = 0;
REP(j,i,n-1) {
if (!flag) {
if (chk(j+1,sta^1<<s[j],flag)) {
sta ^= s[j];
continue;
}
flag = 1;
PER(k,0,s[j]-1) if (chk(j+1,sta^1<<k,flag)) {
s[j] = k, sta ^= 1<<k;
break;
}
}
else {
PER(k,0,9) if (chk(j+1,sta^1<<k,1)) {
s[j] = k, sta ^= 1<<k;
}
}
}
REP(j,0,9) if (sta>>j&1) s[n]=j;
if (!s[1]) break;
REP(j,1,n) printf("%d", s[j]);hr;
return;
}
REP(i,1,n-2) putchar('9');hr;
} int main() {
int t;
scanf("%d", &t);
while (t--) work();
}

Largest Beautiful Number CodeForces - 946E (贪心)的更多相关文章

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

    题意:给定一个长度为偶数的数,输出小于它的最大的美丽数.如果一个数长度为偶数,且没有前导零,并存在一种排列是回文数的数为美丽数.给定的t个数长度总和不超过200000. 分析: 1.存在一种排列为回文 ...

  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. android 小音频频繁播放

    android中多媒体文件(音乐和视频)的播放是用MediaPlayer方式是大家比较熟悉的,但是现在要来说一下另外一种音乐文件播放的方式SoundPool,相比较而言,用MediaPlayer来播放 ...

  2. Apicloud_(模板)登陆注册功能模板

    项目已托管到Github上 传送门 不需要使用任何图片资源,需要用到SHA1.js库文件, Apicloud_(接口验证)用户注册头部信息X-APICloud-AppKey生成 传送门 项目全代码放到 ...

  3. R_Studio(决策树算法)鸢尾花卉数据集Iris是一类多重变量分析的数据集【精】

    鸢尾花卉数据集Iris是一类多重变量分析的数据集 通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa,Versicolour,Virginica)三个种类中的哪一类 针对 ...

  4. [CSP-S模拟测试]:硬币(博弈论+DP+拓展域并查集)

    题目传送门(内部题135) 输入格式 第一行包含一个整数$T$,表示数据组数. 对于每组数据,第一行两个整数$h,w$,表示棋盘大小. 接下来$h$行,每行一个长度为$w$的字符串,每个位置由为$o, ...

  5. BootStrap之X-editable插件使用

    项目背景 刚加入公司的新项目,主要在做开发工作.由于是新手,本周的工作是配合另外一个同事写前台页面.前台框架是Bootstrap,本文主要介绍一下项目需求的一个功能——表格行内编辑事件. 使用X-ed ...

  6. 套接字之recv系统调用

    recv系统调用对sys_recvfrom进行了简单的封装,只是其中不包含地址信息,其只需要从建立连接的另一端接收信息: /* * Receive a datagram from a socket. ...

  7. vmx转换ofv模板,导入esxi

    使用VMware Workstation安装目录下\OVFTool文件的ovftool.exe工具: 转换示例: 首先进入OVFTool根目录.然后执行 ovftool.exe "D:\ce ...

  8. GetRGB下载

    下载地址:https://pan.baidu.com/s/11EyUPa2WxhIgdsTRZtj_eg 07年6-8月做的,用于屏幕取色. 2019年8月30日13点50分

  9. leetcode 166分数到小数

    手动排除特殊情况: 对于一般情况,使用位运算和加减法来计算除法,使用sign记录结果符号:(这部分为leetcode 29题的答案) 使用hashmap来记录循环体出现的开始位置(如果有的话),使用f ...

  10. latexdiff中的大坑:字符编码问题

    最近用latex写文章,要用到修订模式,于是采用latexdiff命令生成修订版pdf.这原本是一个非常简单方便的方法,却隐藏着字符编码的问题,初次用可能会遇到意想不到的问题,让人很烦,比如,生成出来 ...