【HDU4352/ZCMU1781】XHXJ's LIS(数位DP)

3 篇文章 0 订阅
3 篇文章 0 订阅

记录一个菜逼的成长。。

题目链接
题目大意:
把一个数看成序列,可以求出一个严格最长上升子序列的长度。
如123可以看成1,2,3 严格最长上升子序列长度为3
现在给你一个区间[l,r]和一个k,问在这个区间内满足严格最长上升子序列的长度为k的数有多少个。

ps:没想到今年的校赛竟然出了原题,而且还是之前做过的。。然而比赛中却没什么印象。。血亏。。这算是数位dp的模板题吧。
分析:
定义一个数组
dp[i][j][k] := i表示当前位置,j表示状态压缩后的状态,k表示严格最长上升子序列的长度。
重点是将一个数转化为二进制的状态,利用二进制求LIS,算是一个亮点。
二进制的状态就像nlogn算法里的那个存放最长上升子序列的数组

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <deque>
#include <cctype>
#include <bitset>
#include <cmath>
#include <cassert>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a,b) memset(a,b,sizeof(a))
#define bp __builtin_popcount
#define pb push_back
#define mp make_pair
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
#define lson t<<1,l,mid
#define rson t<<1|1,mid+1,r
#define seglen (node[t].r-node[t].l+1)
#define pi 3.1415926
#define exp  2.718281828459
#define lowbit(x) (x)&(-x)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
template <typename T>
inline void read(T &x){
    T ans=0;
    char last=' ',ch=getchar();
    while(ch<'0' || ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    x = ans;
}
LL dp[21][1<<10][12];
int bit[21];
int k;
//利用二进制求LIS,提示:|(位或)其实无论在哪里就是一个加法 ,^(异或)在这里是一个减法
//如同更新经典LIS算法里的那个存放最长上升子序列的数组
int update_status(int x,int s)
{
    for( int i = x; i < 10; i++ ){
        if(s&(1<<i))
            return (s^(1<<i))|(1<<x);
    }
    return s|(1<<x);
}
//显然1的个数就是LIS的长度
int getnum(int s)
{
    int ret = 0;
    while(s){
        if(s&1)ret++;
        s >>= 1;
    }
    return ret;
}
//limit 判断上一位是否是上界,first判断第一位是否放0,status为状态
LL dfs(int pos,int limit,int first,int status)
{
    if(pos == -1){
        return getnum(status) == k;
    }
    if(!limit && ~dp[pos][status][k])return dp[pos][status][k];
    int ed = (limit ? bit[pos] : 9);
    LL ans = 0;
    for( int i = 0; i <= ed; i++ ){
        ans += dfs(pos-1,(limit&&i==ed),first&&(!i),(first&&(!i))?0:update_status(i,status));
    }
    if(!limit)dp[pos][status][k] = ans;
    return ans;
}
LL solve(LL x)
{
    int ind = 0;
    while(x){
        bit[ind++] = x % 10;
        x /= 10;
    }
    return dfs(ind - 1,1,1,0);
}
int main()
{
    cl(dp,-1);
    int T,cas = 1;
    scanf("%d",&T);
    while(T--){
        LL l,r;
        scanf("%lld%lld%d",&l,&r,&k);
        printf("Case #%d: %lld\n",cas++,solve(r) - solve(l-1));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值