FZU 2109 Mountain Number 数位DP

本文介绍了一种用于寻找特定区间内所有MountainNumber数量的算法。MountainNumber是一种特殊的整数,其奇数位上的数字不低于相邻的偶数位。通过递归动态规划方法实现了高效计算。
One integer number x is called "Mountain Number" if:

(1) x>0 and x is an integer;

(2) Assume x=a[0]a[1]...a[len-2]a[len-1](0≤a[i]≤9, a[0] is positive). Any a[2i+1] is larger or equal to a[2i] and a[2i+2](if exists).

For example, 111, 132, 893, 7 are "Mountain Number" while 123, 10, 76889 are not "Mountain Number".

Now you are given L and R, how many "Mountain Number" can be found between L and R (inclusive) ?

Input

The first line of the input contains an integer T (T≤100), indicating the number of test cases.

Then T cases, for any case, only two integers L and R (1≤L≤R≤1,000,000,000).

Output
For each test case, output the number of “Mountain Number” between L and R in a single line.
Sample Input

3
1 10
1 100
1 1000

Sample Output

9
54
384
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int T,l,r,dp[15][15][2],b[15];
//v当前位置,pre前一个数字,odd_1=奇数_0=偶数,flag_1达到上限;
int seek(int v,int pre,int odd,int flag)
{
    if(v==-1)return 1;
    if(dp[v][pre][odd]!=-1&&!flag)return dp[v][pre][odd];//dp exists on flag;
    //dp若有值一定>0,故若在main外定义可免去初始化,就让其为0;
    int lim=flag?b[v]:9;
    int res=0;
    for(int i=0;i<=lim;i++)
    {
        if(odd&&i<=pre)res+=seek(v-1,i,!odd,flag&&i==b[v]);
        else if(!odd&&i>=pre)res+=seek(v-1,i,!odd,flag&&i==b[v]);
    }
    if(!flag)dp[v][pre][odd]=res;//if(flag) informal can't be tagged; 
    return res;
}
int solve(int x)
{
    if(x==0)return 1;
    int ans=0;
    while(x)
    {
        b[ans++]=x%10;
        x/=10;
    }
    return seek(ans-1,9,1,1);
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&l,&r);
        memset(dp,-1,sizeof(dp));
        //由于解有solve(r)-solve(l-1)得到,整个数据中dp值是一样的,故其实不必memset;
        printf("%d\n",solve(r)-solve(l-1));//solve(r)-solve(l-1);
    }
    return 0;
}

下面是原本的代码,是解释了题意的代码,逻辑上比第一种代码正确;
关于seek过程中出现前导0的情况,eg. 2 ~456 中的20;若不判前导0,则此时20为020,是mountain number,但其实应为20不是Moun Num;

#include<iostream>
#include<cstdio>
using namespace std;
int T,L,R,bit[15],dp[15][15][2];
//zero=1则有前导0; eg.  0~278中的(0)20;
int seek(int v,int pre,int odd,int zero,int flag)
{
    if(v<0)return 1;
    if(!flag&&dp[v][pre][odd])return dp[v][pre][odd];
    int lim=flag?bit[v]:9;
    int res=0;
    /*本来以为下面对前导0的判定可简化,但是没成功*/
    for(int i=0;i<=lim;i++)
    {
        if(zero==1&&i==0)res+=seek(v-1,9,odd/*若i为0,一直是奇数位*/,zero,0); 
        else if(odd&&i<=fig)res+=seek(v-1,i,!odd,0,flag&&i==bit[v]);//
        else if(!odd&&i>=fig)res+=seek(v-1,i,!odd,0,flag&&i==bit[v]);//
    }
    if(!flag)dp[v][pre][odd]=res;
    return res;
}
int solve(int x)
{
    int i=0;
    while(x)
    {
        bit[i++]=x%10;
        x/=10;
    }
    return seek(i-1,9,1,1,1);
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&L,&R);
        printf("%d\n",solve(R)-solve(L-1));
    }
    return 0;
} 

对前导0的判定只能做到简化部分

if(zero==1&&i==0)res+=seek(v-1,9,odd/*若i为0,一直是奇数位*/,zero&&!i,flag&&i==bit[v]); 
else if(odd&&i<=fig)res+=seek(v-1,i,!odd,zero&&!i,flag&&i==bit[v]);//
else if(!odd&&i>=fig)res+=seek(v-1,i,!odd,zero&&!i,flag&&i==bit[v]);//

对于odd和pre,目前不知道如何简化,有可能只是数学中数字的神奇,但总觉得可以透过表面简化成第一种代码;待参;

Description Mountain Watching [Jeffrey Wang, 2009] One day, Bessie was gazing off into the distance at the beautiful Wisconsin mountains when she wondered to herself: which mountain is the widest one? She decided to take N (1 <= N <= 100,000) equally-spaced height measurements H_i (1 <= H_i <= 1,000,000,000) sequentially along the horizon using her new Acme Long Distance Geoaltimeter. A mountain is defined to be a consecutive sequence of H_i values which increases (or stays the same) and then decreases (or stays the same), e.g., 2, 3, 3, 5, 4, 4, 1. It is possible for a mountain on the edge of her field of vision only to increase or only to decrease in height, as well. The width of a mountain is the number of measurements it encompasses. Help Bessie identify the widest mountain. Here's a simple example of a typical horizon: ******* * ********* *** ********** ***** *********** ********* * * ***************** *********** *** * ** ******************* ************* * * ******* * ********************************************************************** 3211112333677777776543332111112344456765432111212111112343232111111211 aaaaaa ccccccccccccccccccccc eeeeeee ggggggggg bbbbbbbbbbbbbbbbbbbbbbbbbbbb ddddd ffffffffff hhhhhhhhh The mountains are marked 'a', 'b', etc. Obviously, mountain b is widest with width 28. The mountain on the left has width 6 for the purposes of this task. Input * There are multiple test cases. * For each case: ** Line 1: A single integer: N ** Lines 2..N+1: Line i+1 contains a single integer: H_i Output * For each case: ** Line 1: A single line with a single integer that is the width of the widest mountain. Sample Input 7 3 2 3 5 4 1 6 INPUT DETAILS: The height measurements are 3, 2, 3, 5, 4, 1, 6. Sample Output 5 OUTPUT DETAILS: The widest mountain consists of the measurements 2, 3, 5, 4, 1. Other mountains include 3, 2 and 1, 6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值