fzu2109--Mountain Number(数位dp)
Problem Description
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
Sample Input
Sample Output
看到题的第一反应是:数位dp。。那肯定不会。。。
感觉有点像记忆化搜索吧。cal(n)求小于等于n且满足条件的数。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int N = 15;
int num[N];
int dp[N][N][2]; // dp[i][j][1] i位数 前一位为j int dfs(int nowPos, int preNum, bool isPeak, bool isFirst, bool isMax)
{
if (nowPos == -1) return 1; if (!isMax && dp[nowPos][preNum][isPeak]) return dp[nowPos][preNum][isPeak]; int ans = 0;
int limit = isMax ? num[nowPos] : 9;
for (int i = 0; i <= limit; ++i) {
// isFirst 是说前面都是0 也就是该位是第一位
if (isFirst && i == 0) ans += dfs(nowPos - 1, 9, false, true, false);
else if (isPeak && i >= preNum) ans += dfs(nowPos - 1, i, false, false, (isMax && i == limit));
else if (!isPeak && i <= preNum) ans += dfs(nowPos - 1, i, true, false, (isMax && i == limit));
}
if (!isMax) dp[nowPos][preNum][isPeak] = ans; return ans;
} int cal(int n)
{
int idx = 0;
while (n) {
num[idx++] = n % 10;
n /= 10;
}
return dfs(idx - 1, 9, false, true, true);
} int main()
{
int t;
scanf("%d", &t);
while (t--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", cal(r) - cal(l - 1));
}
return 0;
}
fzu2109--Mountain Number(数位dp)的更多相关文章
- Fzu2109 Mountain Number 数位dp
Accept: 189 Submit: 461Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description One ...
- FZU - 2109 Mountain Number 数位dp
Mountain Number One integer number x is called "Mountain Number" if: (1) x>0 and x is a ...
- 多校5 HDU5787 K-wolf Number 数位DP
// 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- HDU 5787 K-wolf Number 数位DP
K-wolf Number Problem Description Alice thinks an integer x is a K-wolf number, if every K adjacen ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- beautiful number 数位DP codeforces 55D
题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...
- BNU 13024 . Fi Binary Number 数位dp/fibonacci数列
B. Fi Binary Number A Fi-binary number is a number that contains only 0 and 1. It does not conta ...
- hdu 5898 odd-even number(数位dp)
Problem Description For a number,if the length of continuous odd digits is even and the length of co ...
随机推荐
- 25个App免费资源网站
不少非常优秀的设计师已经在网络分享了很多出色的图标.界面 PSD 文件,再加上其他一些相关资源,设计 iOS 应用更加方便了. 模板 & PSDs Icon Template Michael ...
- Python属性、方法和类管理系列之----描述符类
什么是描述符类? 根据鸭子模型理论,只要具有__get__方法的类就是描述符类. 如果一个类中具有__get__和__set__两个方法,那么就是数据描述符,. 如果一个类中只有__get__方法,那 ...
- H5动画优化之路
H5动画60fps之路 在移动端,和Native相比,H5一直都被人吐槽性能差,尤其是在动画方面. 谈到整个Web app的生命周期,一般分为四个部分: 加载 等待用户 响应用户 动画 一般情况下,首 ...
- Spring+MyBatis实践—登录和权限控制
1.实现用户登录功能: 通过session来实现用户登录功能.在用户登录时,将用户的相关信息放在HttpSession对象用,其中HttpSession对象可以通过HttpServletRequest ...
- UIWebView 与 JS 交互(1):Objective-C 调用 Javascript
众所周知,随着硬件水平的发展,HTML5 与原生 APP 性能差距不断缩小,正在互联网科技领域扮演者越来越重要的角色.作为一种能很大程度上节约成本的技术方案,通过 HTML5 及 JS 实现的跨平台技 ...
- StatsD!次世代系统监控的核心
在互联网业务蒸蒸日上的今时今日,系统架构日渐复杂,随着软件产品和工程团队的变革,许多开源的监控工具应运而生,其中有一些相当出名,比如 Zabbix.Nagios 还有 StatsD.也有一些问题被大家 ...
- 【 NOIP2015 DAY1 T2 信息传递】带权并查集
题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...
- mysql通过frm+ibd文件还原data
此方法只适合innodb_file_per_table = 1 当误删除ibdata 该怎么办? 如下步骤即可恢复: 1.准备工作 1)准备一台纯洁的mysql环境[从启动到现在没有 ...
- [状压dp]POJ2686 Traveling by Stagecoach
题意: m个城市, n张车票, 每张车票$t_i$匹马, 每张车票可以沿某条道路到相邻城市, 花费是路的长度除以马的数量. 求a到b的最小花费, 不能到达输出Impossible $1\le n\le ...
- 李洪强iOS开发之-PCH文件的配置
pch 可以用来存储共享信息,比如设备屏幕的宽度,高度.版本号等等 公用信息 Xcode 老版本会自动为我们创建pch文件,新版本开始不自动创建了,如果需要使用可以自己手动创建 创建完成后可以在里面定 ...