tjut 3565

#include"cstdlib"  
#include"cstdio"  
#include"cstring"  
#include"cmath"  
#include"queue"  
#include"algorithm"  
#include"iostream"  
using namespace std;  
#define ll unsigned __int64  
int dp[30][10][7];  
int numx[30],numy[30];  
int dfs(int site,int cur,int ok,int fa,int fb)  //因为是大小 所以要在中间判断  
{  
    if(site==0) return ok==6?0:-1;  //状态6代表成立的数  
    if(!fa&&!fb&&~dp[site][cur][ok]) return dp[site][cur][ok];  //都不是边界  
    int Min=fa?numx[site]:0; //上界  
    int Max=fb?numy[site]:9; //下界  
    int ans=-1;           //初值  
    for(int i=Min; i<=Max; i++)  
    {  
        int tep=0;  
        if(ok==0&&i) tep=1;  //去前导0  
        else if(ok==1)     
        {  
            if(i>cur) tep=2; //往上走  
            else tep=-1;    //无法走  
        }  
        else if(ok==2)  
        {  
            if(i>cur) tep=2;  //继续上  
            else if(i==cur) tep=-1; //相等不能走  
            else tep=3;   //往下  
        }   
        else if(ok==3)  
        {  
            if(i>cur) tep=4;  //跳到第二个峰  
            else if(i==cur)   //相等的话0不能跳,因为不能前导0  
            {  
                if(i) tep=4;  
                else tep=-1;  
            }  
            else tep=3;  //继续下  
        }  
        else if(ok==4) //下同上  
        {  
            if(i>cur) tep=5;  
            else tep=-1;  
        }  
        else if(ok==5)  
        {  
            if(i>cur) tep=5;  
            else if(i==cur) tep=-1;  
            else tep=6;  
        }  
        else if(ok==6)  
        {  
            if(i>=cur) tep=-1;  //最后只能下不能跳了  
            else tep=6;  
        }  
        if(tep!=-1)  
        {  
            int sum=dfs(site-1,i,tep,fa&&i==Min,fb&&i==Max); //这位放完 后面的最大值  
            if(sum!=-1) ans=max(ans,sum+i);  //加上这位比大小  
        }  
    }  
    if(!fa&&!fb) dp[site][cur][ok]=ans;  //不是边界保存值  
    return ans;   
}  
int main()  
{  
    int t,cas=1;  
    cin>>t;  
    memset(dp,-1,sizeof(dp));  
    while(t--)  
    {  
        ll x,y;  
        scanf("%I64u%I64u",&x,&y);  //注意2^64次方 要用无符号64位读入  
        int cnt=0;  
        while(y)  
        {  
            cnt++;  
            numx[cnt]=x%10;  
            x/=10;  
            numy[cnt]=y%10;  
            y/=10;  
        }  
        int ans=dfs(cnt,0,0,1,1);  
        printf("Case %d: %d\n",cas++,ans==-1?0:ans);  
    }  
    return 0;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值