hdu4352 XHXJ's LIS【数位dp】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cdsszjj/article/details/80350382

题目大意:

定义f(i)表示将i看成字符串的最长上升子序列长度。
给定l,r,k,求满足l<=i<=r且f(i)=k的个数。
1<=l<=r<=10^18,1<=k<=10,T<=10000。

解题思路:

注意是最长上升序列,如果是不下降序列就无法做了。
回忆O(nlogn)求 LIS 的过程,维护一个上升序列,每次新加一个数的时候,用它去替换里面最小的大于它的数,最后序列长度就是答案。
对于本题,因为数位只可能是 0 到 9,所以可以考虑用一个10位二进制数来唯一确定这个需要维护的序列。
设f(i,j,lim,z)为从高到低填了前 i 位,之前部分的序列情况为j,是否封顶,前缀是否全为0的数字个数,然后dp即可。
注意询问组数很多,所以要保证dp记忆化具有通用性,不能每次重新dp。
时间复杂度O(logr*2^10*10)。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll getint()
{
    ll i=0,f=1;char c;
    for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
    if(c=='-')c=getchar(),f=-1;
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}
int T,k,num[25];
ll l,r,dp[25][1<<10][11];
int get_num(int s)
{
    int res=0;
    for(int i=0;i<10;i++)res+=(s>>i&1);
    return res;
}
int get_s(int s,int x)
{
    for(int i=x;i<10;i++)
        if(s>>i&1)return (s^(1<<i))|(1<<x);
    return s|(1<<x);
}
ll dfs(int pos,int s,bool lim,bool z)
{
    if(pos==-1)return get_num(s)==k;
    if(!lim&&dp[pos][s][k]!=-1)return dp[pos][s][k];
    int end=lim?num[pos]:9;ll res=0;
    for(int i=0;i<=end;i++)
        res+=dfs(pos-1,(i==0&&z)?0:get_s(s,i),lim&&(i==end),z&&(i==0));
    if(!lim) dp[pos][s][k]=res;
    return res;
}
ll solve(ll n)
{
    int len=0;
    while(n)num[len++]=n%10,n/=10;
    return dfs(len-1,0,1,1);
}
int main()
{
    //freopen("lx.in","r",stdin);
    memset(dp,-1,sizeof(dp));
    T=getint();
    for(int t=1;t<=T;t++)
    {
        l=getint(),r=getint(),k=getint();
        printf("Case #%d: %lld\n",t,solve(r)-solve(l-1));
    }
    return 0;
}

XHXJ's LIS

01-01

Problem Descriptionn#define xhxj (Xin Hang senior sister(学姐)) nIf you do not know xhxj, then carefully reading the entire description is very important.nAs the strongest fighting force in UESTC, xhxj grew up in Jintang, a border town of Chengdu.nLike many god cattles, xhxj has a legendary life: n2010.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.nAs 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.nXhxj 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.nAnother 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.nFor the first one to solve this problem,xhxj will upgrade 20 favorability rate。n nInputnFirst a integer T(T<=10000),then T lines follow, every line has three positive integer L,R,K.(n0

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试