码迷,mamicode.com
首页 > 其他好文 > 详细

XHXJ's LIS (数位dp)

时间:2020-04-22 16:11:45      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:ict   数列   continue   ora   and   other   case   att   lov   

题干

#define xhxj (Xin Hang senior sister(学姐))
If you do not know xhxj, then carefully reading the entire description is very important.
As the strongest fighting force in UESTC, xhxj grew up in Jintang, a border town of Chengdu.
Like many god cattles, xhxj has a legendary life:
2010.04, had not yet begun to learn the algorithm, xhxj won the second prize in the university contest. And in this fall, xhxj got one gold medal and one silver medal of regional contest. In the next year‘s summer, xhxj was invited to Beijing to attend the astar onsite. A few months later, xhxj got two gold medals and was also qualified for world‘s final. However, xhxj was defeated by zhymaoiing in the competition that determined who would go to the world‘s final(there is only one team for every university to send to the world‘s final) .Now, xhxj is much more stronger than ever,and she will go to the dreaming country to compete in TCO final.
As you see, xhxj always keeps a short hair(reasons unknown), so she looks like a boy( I will not tell you she is actually a lovely girl), wearing yellow T-shirt. When she is not talking, her round face feels very lovely, attracting others to touch her face gently。Unlike God Luo‘s, another UESTC god cattle who has cool and noble charm, xhxj is quite approachable, lively, clever. On the other hand,xhxj is very sensitive to the beautiful properties, "this problem has a very good properties",she always said that after ACing a very hard problem. She often helps in finding solutions, even though she is not good at the problems of that type.
Xhxj loves many games such as,Dota, ocg, mahjong, Starcraft 2, Diablo 3.etc,if you can beat her in any game above, you will get her admire and become a god cattle. She is very concerned with her younger schoolfellows, if she saw someone on a DOTA platform, she would say: "Why do not you go to improve your programming skill". When she receives sincere compliments from others, she would say modestly: "Please don’t flatter at me.(Please don‘t black)."As she will graduate after no more than one year, xhxj also wants to fall in love. However, the man in her dreams has not yet appeared, so she now prefers girls.
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。

输入格式

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).

输出格式

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.

样例

? 输入:

? 1

? 123 321 2

? 输出:

? Case #1: 139

胡说八道

怎么说呢...最近真的感觉自己就跟没了魂一样...就差来几个黑人老哥给我装进去送走了...实在太怠惰了啊...博客越来越短,唉,我不能再这样了啊...

先说这道题...典型的数位dp,然后要结合LIS,难点在于state的转移。

首先我们知道state的每一个二进制位都对应着一个数位,那么state的每个二进制位中1的个数就是LIS的长度。

我们可以手造一个小数列 12435

  • 第一位遇到1,显然前面没有更大的,state更新为0100000;
  • 第二位遇到2,显然前面也没有更大的,state更新为0110000;
  • 第三位遇到4,state更新为01101000;
  • 第四位遇到3,无法直接让LIS长度增加,但根据求LIS的知识,我们肯定是让LIS的最后一位更小,显然3比4更有利于我们后续的答案,所以我们把原来state中4那一位的1去掉,把3那位换成1
  • 这样做是可行的,因为对于后面的数来讲,我们如果想让LIS的长度增加,那么必须比前面所有的数都大,那么前面的数的相对大小顺序就无所谓了。别的就按我们上述所说的来不断更新state即可
  • 所以,state的二进制位中1的个数即为已知LIS的长度(当然前导0不算进去),最靠后的那一个数位为1的,假如是第x位,那么这个LIS的末尾元素就是x。

这里推荐一个很好的数位dp的博客:https://blog.csdn.net/wust_zzwh/article/details/52100392

代码:看注释就知道有多惨烈...

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<bitset>
#define ll long long
using namespace std;
const int maxn=20;
int dp[maxn][1300][maxn];
int len,num[maxn];
/*int get_next(int state,int x){
    bitset<10>bit=state;
    int i;
    for(i=x;i<=9;i++){
        if(bit[i]==1) break;
    }
    if(i!=10) bit[i]=0;
    bit[x]=1;
    int ans = bit.to_ulong();//将bit转成unsigned long型
    return ans;
}*/
int Update(int state,int x,bool zero){
    if(zero&&(x==0)) return 0;
    for(int i=state;i<=9;i++)
        if(state&(1<<i))
            return(state^(1<<i)|(1<<x));
    return state|(1<<x);
}
int ok(int state,int x){
    for(int i=x;i<=9;i++)
        if(state&(1<<i))
            return (state^(1<<i))|(1<<x);
    return state|(1<<x);
}
bool check(int state,int k){
    int cnt=0;
    while(state){
        if(state&1)cnt++;
        state>>=1;
    }
    return cnt==k;
}
//ll dfs(int pos/*当前枚举位*/,int state/*状态*/,int k,bool zero/*前导0*/,bool limit/*前一位是否最大*/){
/*    if(pos==0) return check(state,k);
    if(!limit&&dp[pos][state][k]!=-1) return dp[pos][state][k];//记忆化,这道题有最高位和前导0的双重限制
    int up=limit?num[pos]:9;
    ll ans=0;
    for(int i=0;i<=up;i++){
        /*if(zero&&i==0){//该位是0并且前一位也是0,直接统计进去枚举下一次即可
            ans += dfs(pos-1,state,k,zero&&i==0,limit&&i==up);
            continue;
        }*/
        //int next = (state,i);//更新状态
//        ans += dfs(pos-1,(zero&&(i==0))?0:ok(state,i),k,zero&&i==0,limit&&i==up);
//    }
    /*if(!limit) dp[pos][state][k]=ans;//与上面的记忆化是对应的
    return ans;
}*/
ll dfs(int pos,int state,int k,bool limit,bool zero){
    if(pos==0) return check(state,k);
    if(dp[pos][state][k]!=-1&&!limit)
        return dp[pos][state][k];
    int maxx=limit?num[pos]:9;
    ll ans=0;
    for(int i=0;i<=maxx;i++)
        ans+=dfs(pos-1,(zero&&(i==0))?0:ok(state,i),k,limit&&i==maxx,zero&&i==0);
    if(!limit) dp[pos][state][k]=ans;
    return ans;
}
ll solve(ll n,int k){
    if(n==-1) return 0;
    len = 0;
    while(n){//拆开每个数位
        num[++len] = n%10;
        n/=10;
    }
    return dfs(len,0,k,1,1);//最高位为len,状态初始为0
}
int main(){
    ios::sync_with_stdio(0);
    //sync_with_stdio()是一个对于cin,cout的优化
    //可以参见https://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.html
    int T;
    cin>>T;
    memset(dp,-1,sizeof(dp));
    int tot = 0;
    while(T--){
        tot++;
        ll l,r;
        int k;
        cin>>l>>r>>k;
        cout<<"Case #"<<tot<<": ";
        cout<<solve(r,k)-solve(l-1,k)<<endl;
    }
    return 0;
}


XHXJ's LIS (数位dp)

标签:ict   数列   continue   ora   and   other   case   att   lov   

原文地址:https://www.cnblogs.com/Zfio/p/12752637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!