Mountain Number(数位dp)

本文介绍了一种使用数位DP算法解决“山数”问题的方法。山数是一种特殊的整数,其奇数位置的数字必须大于等于相邻的两个数字。文章详细解释了如何通过递归函数计算在给定范围内山数的数量,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://cn.vjudge.net/problem/FZU-2109

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

x=a[0]a[1]...a[len-2]a[len-1](0≤a[i]≤9, a[0] is positive)

下标为奇数的要大于等于两边

数位dp,注意ll用I64d

#include<cstdio>
#include<cstring>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
ll dp[15][2][10];
int a[15];
ll dfs(int pos,int sta,int pre,bool lead,bool limit){
	if(pos==0) return 1;
	if(!lead&&!limit&&dp[pos][sta][pre]!=-1) return dp[pos][sta][pre];
	int up=limit?a[pos]:9;
	ll ans=0;
	for(int i=0;i<=up;i++){
		if(lead&&i==0) ans+=dfs(pos-1,0,9,true,limit&&i==up);
		else if(sta==0&&i<=pre) ans+=dfs(pos-1,1,i,false,limit&&i==up);
		else if(sta==1&&i>=pre) ans+=dfs(pos-1,0,i,false,limit&&i==up);
	}
	if(!lead&&!limit) dp[pos][sta][pre]=ans;
	return ans;
}
ll solve(ll x){
	int pos=0;
	while(x){
		a[++pos]=x%10;
		x/=10;
	}
	return dfs(pos,0,9,true,true);
}
int main(){
    memset(dp,-1,sizeof(dp));
    int T;
    scanf("%d",&T);
    while(T--){
    	ll L,R;
    	scanf("%I64d%I64d",&L,&R);
    	printf("%I64d\n",solve(R)-solve(L-1));
	}
	return 0;
}



 

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、付费专栏及课程。

余额充值