HDU 4352 XHXJ's LIS 数位dp lis

时间:2021-04-22 16:22:24

目录

题目链接

HDU 4352 XHXJ's LIS

题解

对于lis求的过程

对一个数列,都可以用nlogn的方法来的到它的一个可行lis

对这个logn的方法求解lis时用的数组进行装压

预处理的到这个的转移

数位dp转移的时候直接得到下一位的lis状态

代码


#include<set>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define gc getchar()
#define pc putchar
#define LL long long
inline LL read() {
LL x = 0,f = 1;
char c = gc;
while(c < '0' || c > '9') c = gc;
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc;
return x * f;
}
void print(LL x) {
if(x < 0) {
pc('-');
x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0');
}
const int maxn = (1 << 10) + 7;
int cnt[maxn];
int nxt[maxn][21]; //状态s插入j之后的最优可行方案
int get(int x,int y) {
for(int i = y;i < 10;++ i) {
if(x & (1 << i)) {
return x ^ (1 << i) | (1 << y);
}
}
return x | (1 << y);
}
void pre() {
for(int i = 0;i < (1 << 10); ++ i) {
for(int j = 0;j < 10;++ j) {
if(i & (1 << j) ) cnt[i] ++;
nxt[i][j] = get(i,j);
}
}
}
LL dp[21][maxn][21];
int limit[22];
LL dfs(int k,int len,int num,bool flag,bool zero) {
if(len < 0)
return cnt[num] == k;
if(!flag && dp[len][num][k] != -1) return dp[len][num][k];
LL ret = 0;
int lim = flag ? limit[len] : 9;
for(int i = 0;i <= lim;++ i)
ret += dfs(k,len - 1,(zero && i == 0) ? num : nxt[num][i],(flag && i == lim),(zero && i == 0));
if(!flag)
dp[len][num][k] = ret;
return ret;
}
LL solve(LL n,int k) {
int pos = 0;
while(n) {
limit[pos ++] = n % 10;
n /= 10;
}
return dfs(k,pos - 1,0,1,1);
}
int main() {
memset(dp,-1,sizeof dp);
int T = read();
pre();
for(int i = 1;i <= T;++ i) {
LL L = read(),R = read(),k = read();
printf("Case #%d: ",i);
print(solve(R,k) - solve(L - 1,k));
pc('\n');
}
return 0;
}