传送门

题目大意:

求[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. mysql数据库忘记密码时如何修改(转)

    当我们忘记mysql数据库密码时我们就无法正常进入数据库,也就无法修改密码,那么这时该怎么修改密码呢,这里教大家一个简单常用修改密码的方式. (如果图简单快速修改密码的话,直接跳过查询步骤,依照图上执 ...

  2. 关于python中数组的问题,序列格式转换

    https://blog.csdn.net/sinat_34474705/article/details/74458605?utm_source=blogxgwz1 https://www.cnblo ...

  3. 【习题 6-3 UVA - 536】 Tree Recovery

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 递归题 [代码] #include <bits/stdc++.h> using namespace std; const ...

  4. 三星Galaxy Tab S2上市,压制苹果之心凸显

        平板市场正在迎来史上最为关键的一次PK,众所周知,平板市场的苹果和三星一直是行业的领头羊,而在激烈的竞争中.三星平板似乎后劲更足.众多性能优异的产品频频推出.平板之王的称谓呼之欲出. 去年三星 ...

  5. 【Codeforces Round #301 (Div. 2) E】Infinite Inversions

    [链接] 我是链接,点我呀:) [题意] 给你一个无限长的序列1,2,3,4... 然后给你n个操作. 每个操作ai,bi; 表示调换位置为ai和位置为bi的数的位置. (ai,bi<=10^9 ...

  6. 【 2017 Multi-University Training Contest - Team 9 && hdu 6162】Ch’s gift

    [链接]h在这里写链接 [题意] 给你一棵树,每个节点上都有一个权值. 然后给你m个询问,每个询问(x,y,a,b); 表示询问x->y这条路径上权值在[a,b]范围内的节点的权值和. [题解] ...

  7. [Angular] Component architecture and Reactive Forms

    It it recommeded that when deals with form component, we can create a container component to hold st ...

  8. js进阶 13-2 jquery动画滑动效果哪些注意事项

    js进阶 13-2 jquery动画滑动效果哪些注意事项 一.总结 一句话总结:滑动里面这里up是隐藏,down是显示. 1.jquery动画默认的两个切换效果是什么(swing默认和linear的区 ...

  9. js闭包中的this(匿名函数中的this指向的是windows)

    js闭包中的this(匿名函数中的this指向的是windows) 一.总结 1.普通函数中的this指向的是对象,匿名函数中的this指向的是windows,和全局变量一样 2.让匿名函数中的thi ...

  10. 10.7 android输入系统_Dispatcher线程情景分析_Reader线程传递事件和dispatch前处理

    android输入系统C++最上层文件是com_android_serve_input_InputManagerService.cpp global key:按下按键,启动某个APP可以自己指定,修改 ...