传送门

题目大意:

求[l, r]中数位的最长上升序列恰好为k的数的个数。

题目分析:

首先要理解\(o(nlogn)\)求LIS问题的思路,每次寻找第一个大于等于的数将其更改。

设dp[pos][sta][k]表示第pos位,sta见后,加入k是为了初始化减少后面的时间。

sta表示前pos位的LIS,是一个2进制数,110位表示09是否被选,比如现在状态是0010100000,就是说LIS是2,4,分两种情况:

  • 现在加入一个数字3,用LIS的方法找到第一个大于等于3的数----4,将其置为0,并把3置为1,sat变为0011000000.
  • 若加入6,没有大于等于它的数,就直接将6置为1,sat变为0010101000

    需要先预处理to[i][j]表示i状态加入数字j后转移到的状态来加速。 我卡了半天T所以必须初始化。

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll; struct ioSys{
inline ll read(){
ll i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar())
i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wr(int x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
inline void operator >> (ll &x){
x = read();
}
inline void operator << (int x){
wr(x);
}
}IO; ll T;
ll l, r, k, len;
typedef long long ll;
ll dp[25][1050][15], s[25], to[1050][15], oneNum[1050]; inline void convert(ll x){
len = 0;
memset(s, 0, sizeof s);
while(x) s[++len] = x % 10, x /= 10;
} inline ll count(ll x){
int ret = 0;
while(x) ret++, x = x & (x - 1);
return ret;
} inline void init(){
memset(dp, -1, sizeof dp);
for(int i = 0; i <= (1 << 10) - 1; i++){
for(int j = 0; j <= 9; j++){
int tmp = 0, p;
for(p = j; p < 10; p++)
if(i & (1 << p)) break;
if(p == 10) tmp = i | (1 << j);
else tmp = i ^ (1 << p) | (1 << j);
to[i][j] = tmp;
}
}
for(int i = 0; i <= (1 << 10) - 1; i++)
oneNum[i] = count(i);
} inline ll DP(int pos, int sta, bool limit, bool lead, int K){
if(!limit && !lead && dp[pos][sta][K] != -1) return dp[pos][sta][K];
if(pos == 0) return oneNum[sta] == k || (lead && K == 1);
int high = limit ? s[pos] : 9;
ll ret = 0;
for(int i = 0; i <= high; i++){
if(i == 0 && lead) ret += DP(pos - 1, 0, limit && (i == high), true, K);
else
ret += DP(pos - 1, to[sta][i], limit && (i == high), false, K);
}
if(!limit && !lead) dp[pos][sta][K] = ret;
return ret;
} int main(){
T = IO.read();
init();
for(int t = 1; t <= T; t++){
IO >> l, IO >> r, IO >> k;
convert(r);
ll ret1 = DP(len, 0, 1, 1, k);
convert(l - 1);
ll ret2 = DP(len, 0, 1, 1, k);
printf("Case #%d: %I64d\n", t, ret1 - ret2);
}
return 0;
}

HDU 4352 XHXJ's LIS - 状压dp + LIS的更多相关文章

  1. hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)

    传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...

  2. HDU 6149 Valley Numer II 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...

  3. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  4. HDU 1074 Doing Homework(状压DP)

    第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...

  5. HDU 4906 Our happy ending (状压DP)

    HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...

  6. hdu4352-XHXJ's LIS状压DP+数位DP

    (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 题意:传送门  原题目描述在最下面.  在区间内把整数看成一个阿拉伯数字的集合,此集合中最长严格上升子序列的长度为k的个数. 思路: ...

  7. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  8. HDU 4568 Hunter 最短路+状压DP

    题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...

  9. HDU 1074 Doing Homework【状压DP】

    Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...

随机推荐

  1. 给已有数据的oracle表建立外键关系

    PS:这里是给自己做个备忘,下次遇到同类问题的时候,方便查找: 客户在有主外键关系的2张表进行页面删除时报错已有子记录,运维后台处理的时候应该找出相应的数据,先删除子记录,在删主表记录:但客户要的急, ...

  2. Docker---(2)为什么要用Docker

    原文:Docker---(2)为什么要用Docker 版权声明:欢迎转载,请标明出处,如有问题,欢迎指正!谢谢!微信:w1186355422 https://blog.csdn.net/weixin_ ...

  3. NYOJ 552 小数阶乘

    小数阶乘 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描写叙述 编写一个程序,求一个数m的阶乘. 输入 有多组測试数据,以EOF结束. 每组測试数据有1个整数m. 输出 每 ...

  4. 区分json与jsonp

    JSON(JavaScript Object Notation)和JSONP(JSON with Padding)虽然只有一个字母的差别,但其实他们根本不是一回事儿,下边简单区分概括一下: JSON是 ...

  5. css页面滚动条出现时防止页面跳动的方法

    大家写页面时应该都遇到过一个问题,尤其是写单页面应用的时候, 在有滚动条页面和没有滚动条页面之间相互跳转时, 你页面的主体内容会向左或者向右抖一下,让强迫症看了很不舒服. 现在就来解救一下强迫症: 方 ...

  6. Leetcode之Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  7. Android 用SQLite 使用 CursorLoader 中的数据填充列表视图

    我做了简单的测试应用程序基于此示例.有一个按钮,插入到数据库和列表视图的数据.都是在 MainActivity 中.在原来的代码是restartLoader() 仅从调用 onResume() ,但它 ...

  8. gdbserver远程调试嵌入式linux应用程序方法

    此处所讲的是基于gdb和gdbsever的远程调试方法.环境为:PC机:win7.虚拟机:10.04.下位机:飞嵌TE2440开发板. 嵌入式linux应用程序的开发一般都是在linux里面编写好代码 ...

  9. SoC中的IP模块学习

    SoC中的IP模块学习 理解IP Spec-->register定义,理解原理+架构框图 查看testcase+model(看已有的测试例程),分析操作/使用模块的流程,寄存器的配置方法 运行仿 ...

  10. phpstorm 2018本地激活

    这是简书上找到的,害怕以后找不到,记录一下网址为https://www.jianshu.com/p/133af2e4fe3f