大意: 定义一个好数为位数为偶数, 且各位数字重排后可以为回文, 对于每个询问, 求小于$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. 使用Qt Designer进行布局

    在使用Form之前,需要将Form上的对象放置到布局中.这确保在应用程序中预览或使用Form时,对象将正确显示.在布局中放置对象还可以确保在调整窗体大小时它们也能正确调整大小. 应用和打断布局    ...

  2. python3基础: 元组tuple、 列表list、 字典dict、集合set。 迭代器、生成器

    一.元组: tuple Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组 tup2 = (111, 22, 33, ...

  3. JS框架_(Esign.js)仿信用卡电子签名特效

    百度云盘 传送门 密码:l60w 电子签名特效效果: <!DOCTYPE html> <html> <head lang="en"> <m ...

  4. win7系统安装Mysql5.7

    1.下载mysql的zip包 把zip包解压到指定的目录,目录不能有中文和空格 2.进入%mysql%目录(注:%mysql%是解压目录,我的是:D:\JavaTool\mysql) 在%mysql% ...

  5. 套接字之select系统调用

    select是IO多路复用的一种方式,用来等待一个列表中的多个描述符的可读可写状态: SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_ ...

  6. 一键发布shell脚本

    1.配置集群间免密登录 (1)配置host:vim /etc/hosts (2)生成公钥 :ssh-keygen -t rsa -P '' 这时会提示生成的公钥的存放地址,直接回车,公钥生成成功 (3 ...

  7. 三十六:数据库之SQLAlchemy外建之一对一关系

    relationship()的uselist参数默认为True,即一对多,如果要一对一,则需让uselist=False 准备工作 from sqlalchemy import create_engi ...

  8. Docker 资源汇总

    Docker 资源汇总 Docker官方英文资源 Docker官网:http://www.docker.com Docker Windows 入门:https://docs.docker.com/do ...

  9. 描述什么是springboot

    Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作<Expert One-On-One J2EE Developme ...

  10. Leetcode基础篇30天30题系列之数组:模拟计算法

    作者:丁宋涛 数组:加一 题干: 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整 ...