X - Bi-peak Number (数位dp+有上界也有下届)

A peak number is defined as continuous digits {D0, D1 … Dn-1} (D0 > 0 and n >= 3), which exist Dm (0 < m < n - 1) satisfied Di-1 < Di (0 < i <= m) and Di > Di+1 (m <= i < n - 1). 
A number is called bi-peak if it is a concatenation of two peak numbers. 



The score of a number is the sum of all digits. Love8909 is crazy about bi-peak numbers. Please help him to calculate the MAXIMUM score of the Bi-peak Number in the closed interval [A, B].
Input
The first line of the input is an integer T (T <= 1000), which stands for the number of test cases you need to solve. 
Each case consists of two integers “A B” (without quotes) (0 <= A <= B < 2^64) in a single line. 
Output
For the kth case, output “Case k: v” in a single line where v is the maximum score. If no bi-peak number exists, output 0.
Sample Input
3
12121 12121
120010 120010
121121 121121
Sample Output
Case 1: 0
Case 2: 0
Case 3: 8

题意:定义"特殊数"为两次先上升后下降形成的数,且第一位大于等于0,没有前导零,问所有满足条件的数中,位数和最大的是多少。

思路:(注意一点就是数据要是有unsigned_int64,wa了好几发

由于结果不满足区间减法,所以不能像通常那样计算cal(b)-cal(a-1),正确的处理方法是,在dfs时保存2个标记,一个标记前缀是否达上界,另一个标记是否达下界。然后就是状态分析了:
s=0:前导0的状态;
s=1:第一个山峰的上坡,且不能立马下坡;
s=2:第一个山峰的上坡,且最后一点能看成是最高点,下一个点可以是下坡;
s=3:第一个山峰的下坡;
s=4:第二个山峰的上坡,且不能立马下坡;
s=5:第二个山峰的上坡,且最后一点能看成是最高点,下一个点可以是下坡;
s=6:第二个山峰的下坡;
s=-1:其余不合法的状态。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned __int64 LL;
#define inf 0x7f7f7f7f
int a[30],b[30];
int dp[20][10][8];
int dfs(int pos,int pre,int sta,int limitx,int limity){
    if(pos==-1)return sta==6?0:-1;
    if(!limitx&&!limity&&dp[pos][pre][sta]!=inf)
        return dp[pos][pre][sta];
    int minn=limitx?a[pos]:0;
    int maxx=limity?b[pos]:9;
    int res=-1;
    for(int i=minn;i<=maxx;i++)
    {
        int s=sta;
        if(sta==0&&i)
        s=1;
        else if(sta==1)
        {
            if(i>pre)  s=2;
            else   s=-1;
        }
        else if(sta==2)
        {
            if(i<pre)  s=3;
            else if(i==pre)
                s=-1;
        }
        else if(sta==3 && i>=pre)
        {
            if(i)   s=4;
            else    s=-1;
        }
        else if(sta==4)
        {
            if(i>pre)  s=5;
            else    s=-1;
        }
        else if(sta==5)
        {
            if(i<pre)  s=6;
            else if(i==pre)
            s=-1;
        }
        else if(sta==6 && i>=pre)
        s=-1;

        if(s!=-1)
        {
            int tmp=dfs(pos-1,i,s,limitx&&i==minn,limity&&i==maxx);
            if(tmp!=-1)
            res=max(res,i+tmp);
        }
    }
    if(!limitx&&!limity)  dp[pos][pre][sta]=res;
    return res;
}
int main()
{
    int t,cas=0;
    memset(dp,0x7f,sizeof(dp));
    scanf("%d",&t);
    while(t--)
    {
        printf("Case %d: ",++cas);
        LL x,y;
        scanf("%I64u%I64u",&x,&y);
        int pos=0;
        for(;y;x/=10,y/=10) a[pos]=x%10,b[pos++]=y%10;
        int ans=dfs(pos-1,0,0,1,1);
        if(ans==-1) ans=0;
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值