HDU 4352 XHXJ's LIS - 状压dp + LIS
题目大意:
求[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的更多相关文章
- hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)
传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...
- HDU 6149 Valley Numer II 状压DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
- HDU 1074 Doing Homework(状压DP)
第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...
- HDU 4906 Our happy ending (状压DP)
HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...
- hdu4352-XHXJ's LIS状压DP+数位DP
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 题意:传送门 原题目描述在最下面. 在区间内把整数看成一个阿拉伯数字的集合,此集合中最长严格上升子序列的长度为k的个数. 思路: ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
- HDU 4568 Hunter 最短路+状压DP
题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...
- HDU 1074 Doing Homework【状压DP】
Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...
随机推荐
- 二叉树的递归插入【Java实现】
C++中由于有指针的存在,可以让二叉树节点指针的指针作为插入函数的实参,在函数体内通过*操作实现对真实节点指针.节点左孩子指针.节点右孩子指针的改变,这样很容易使用递归将大树问题转化到小树问题.但在J ...
- 基于mpvue的小程序项目搭建的步骤
mpvue 是美团开源的一套语法与vue.js一致的.快速开发小程序的前端框架,按官网说可以达到小程序与H5界面使用一套代码.使用此框架,开发者将得到完整的 Vue.js 开发体验,同时为 H5 和小 ...
- 【习题 6-4 UVA-439】Knight Moves
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] bfs模板题 [代码] /* 1.Shoud it use long long ? 2.Have you ever test sev ...
- Hadoop ecosystem 生态圈
Cascading: hadoop上面的workflow Sqoop(发音:skup)是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行 ...
- oled stm32的spi
其实各种协议是很重要的,这篇文章就当做我对spi协议的一个整理吧. 必要的spi简介: https://www.cnblogs.com/zengsf/p/7221207.html?utm_source ...
- Day1:If else流程判断
一.if...else语句 if 条件成立: 执行条件成立后的代码 else: 执行条件不成立的代码 注:注意有冒号,python会强制缩进!一般语句都必须顶格写,缩进是缩进一个tab键,等于4个空格 ...
- 为什么我要选择erlang+go进行server架构(2)
原创文章,转载请注明出处:server非业余研究http://blog.csdn.net/erlib 作者Sunface 为什么我要选择Erlang呢? 一.erlang特别适合中小团队创业: erl ...
- ZOJ 1489 2^x mod n = 1 数论
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=489 题目大意: 给你正整数n,求最小的x使得2^x mod n = 1. 思路 ...
- HDU 2077 汉诺塔IV 递归 通项公式
刚刚做的HDU 2064很好找规律, 回忆一下: b[1] = 2; b[n] = b[n-1] *3 + 2; 可得b[n]= 3^n-1 不懂的传送门http://blog.csdn.net/mu ...
- Unity插件之NGUI学习(5)—— 创建Label图文混排及文字点击
创建一个新的Scene,并按 Unity插件之NGUI学习(2)创建UI Root. 准备工作,制作Font.如今Project窗体创建一个Font目录.然后从系统自带字体目录中选择自己须要的字体,我 ...