XHXJ's LIS HDU - 4352(数位DP)

XHXJ's LIS

HDU - 4352
#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。

Input First a integer T(T<=10000),then T lines follow, every line has three positive integer L,R,K.(
0<L<=R<2 63-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 
题意:要求数字的位数满足,数字数位的最长严格上升子序列的长度恰好为k的个数
思路:很明显数位是LIS,求LIS有个nlog(n)的算法,这道题因为数位一共0-9十个数,我们利用二进制状态压缩来实现这个算法
首先先在此说明一下这个算法的过程
memset(dp,INF,sizeof(dp));  
  
   for(i = 0; i < n; i++){  
  
       *lower_bound(dp,dp+n,a[i]) = a[i];//二分查找需要修改的点,并修改为当前值  
  
    }  
  
   cout << lower_bound(dp,dp+n,INF)-dp << endl;//找到第一个INF值,减去首地址就是实际长度,因为是数组是从0开始的,所以恰好是加一个到第一个INF值的下标  
枚举数字i时,在dp数组中二分找到第一个大于等于这个数字的数并替换,最后输出dp数组的长度
具体的原理看博客点击打开链接
而因为我们的数一共0-9十个,所以直接暴力在二进制中,直接从枚举的这个数(包括)开始往后移位找到第一1,然后通过异或变成0退出循环,然后再按位或把这个数移位后的位变成1
如果不太懂看看代码就懂了
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
int digit[30];
ll dp[30][1<<10][12];
int Update(int state,int bit){
    for(int i = bit; i <= 9; i++){
        if(state & (1 << i)){
            state ^= (1 << i);
            break;
        }
    }
    return state | (1 << bit);
}
int Getlen(int state){
    int cnt = 0;
    while(state){
        cnt += (state & 1);
        state >>= 1;
    }
    return cnt;
}
ll dfs(int pos,int state,int lead,int limit,int k){
    if(pos == -1)
        return Getlen(state) == k;
    ll &dpnow = dp[pos][state][k];
    if(!limit && dpnow != -1)
        return dpnow;
    int max_digit = limit ? digit[pos] : 9;
    ll ans = 0;
    for(int i = 0; i <= max_digit; i++){
        ans += dfs((pos - 1), (lead && i == 0 ? 0 : Update(state,i)), (lead && i == 0), (limit && i == max_digit), k);
    }
    if(!limit)
        dpnow = ans;
    return ans;
}

ll solve(ll n,int k){
    int pos = 0;
    while(n){
        digit[pos++] = n % 10;
        n /= 10;
    }
    return dfs(pos-1,0,1,1,k);
}
int main(){
    int T,cas = 0;
    cin >> T;
    memset(dp,-1,sizeof(dp));
    while(T--){
        ll l,r;
        int k;
        cin >> l >> r >> k;
        cout << "Case #" << ++cas << ": ";
        cout << solve(r,k) - solve(l-1,k) << endl;
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/80435450
今日推荐