HDU - 4352 XHXJ's LIS(数位DP,二分法求LIS)

本文介绍了一种利用数位DP算法解决特定区间内最长严格递增子序列(LIS)计数问题的方法。通过维护LIS状态并采用二分法思想,将状态以二进制形式转化为十进制数进行存储,从而实现快速计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:HDU - 4352 XHXJ’s LIS

Another hobby of xhxj is yy(speculation) some magical problems to discover the special properties. For example, when she see a number, she would think whether the digits of a number are strictly increasing. If you consider the number as a string and can get a longest strictly increasing subsequence the length of which is equal to k, the power of this number is k… It is very simple to determine a single number’s power, but is it also easy to solve this problem with the numbers within an interval? xhxj has a little tired,she want a god cattle to help her solve this problem,the problem is: Determine how many numbers have the power value k in [L,R] in O(1)time.
For the first one to solve this problem,xhxj will upgrade 20 favorability rate。

Input

First a integer T(T<=10000),then T lines follow, every line has three positive integer L,R,K.(
0< L<= R < 263-1 and 1 <= K<=10).

Output

For each query, print “Case #t: ans” in a line, in which t is the number of the test case starting from 1 and ans is the answer.

Sample Input

1
123 321 2

Sample Output

Case #1: 139



题意:

将数都视为一个序列,求范围 [L, R] 内其最长严格递增子序列(LIS)长度为K的数的个数。



分析:

数位DP每次都用DP去求LIS并不现实,所以这里要维护一个LIS,维护的方法就是二分法求LIS的方法:https://blog.youkuaiyun.com/Ratina/article/details/87525881

数位DP的DFS搜索到某一位的状态就是当前LIS的状态,由于是将数视为序列,且是严格递增的,所以LIS最多就10个数(0 ~ 9),而且各不相同、递增排列,那么就可以将其转化为状态。

由于LIS各不相同、递增排列,所以只需要记录0、1、2、… 、9是否存在于序列中,只要知道存在性,就能确定一个唯一的LIS,然后将其以二进制形式转化为十进制存储为一个数即可(共210种LIS的状态

例如:LIS为13579,则其对应的二进制状态就是0101010101,然后转化为十进制即可。

同时注意判断前导零,如果当前0是前导零,则不能放入LIS中

此外,由于该题T较大,而K为1~10,那么可以在dp数组中再加一维记录K,这样可以减少大量重复计算。



以下代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<climits>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
const int INF=0x3f3f3f3f;
int a[30],K;
LL L,R;
LL dp[30][1<<10][15];
int d[15],LEN;    //d存储LIS,LEN为LIS的长度
int getnum()      //将LIS转化为数字
{
    int ret=0;
    bool flag[10]={false};
    for(int i=1;i<=LEN;i++)
        flag[d[i]]=true;
    for(int i=0;i<10;i++)
        ret=2*ret+flag[i];   //二进制转十进制
    return ret;
}
LL dfs(int pos,int lis,bool lead,bool limit)
{
    if(pos<0)
    {
        if(LEN==K)     //满足LNE==K才返回1
            return 1;
        else
            return 0;
    }
    if(!limit&&dp[pos][lis][K]!=-1)
        return dp[pos][lis][K];
    int up=limit?a[pos]:9;
    LL sum=0;
    for(int i=0;i<=up;i++)
    {
        if(lead&&i==0)     //前导零不可放入LIS
            sum+=dfs(pos-1,0,true,limit&&i==up);
        else
        {
            if(LEN==0||d[LEN]<i)     //维护LIS
            {
                d[++LEN]=i;
                sum+=dfs(pos-1,getnum(),false,limit&&i==up);
                LEN--;      //还原
            }
            else
            {
                int t1=lower_bound(d+1,d+LEN+1,i)-d;
                int t2=d[t1];
                d[t1]=i;
                sum+=dfs(pos-1,getnum(),false,limit&&i==up);
                d[t1]=t2;   //还原
            }
        }
    }
    if(!limit)
        dp[pos][lis][K]=sum;
    return sum;
}
LL solve(LL x)
{
    int len=0;
    while(x)
    {
        a[len++]=x%10;
        x/=10;
    }
    LEN=0;
    return  dfs(len-1,0,true,true);
}
int main()
{
    int T,kase=0;
    scanf("%d",&T);
    memset(dp,-1,sizeof(dp));
    while(T--)
    {
        scanf("%I64d %I64d %d",&L,&R,&K);
        printf("Case #%d: %I64d\n",++kase,solve(R)-solve(L-1));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值