fzu2109--Mountain Number(数位dp)
2016-01-19 12:11
218 查看
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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) ?
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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).
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Output
For each test case, output the number of "Mountain Number" between L and R in a single line.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Input
31 10
1 100
1 1000
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Output
954
384
看到题的第一反应是:数位dp。。那肯定不会。。。
感觉有点像记忆化搜索吧。cal(n)求小于等于n且满足条件的数。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N = 15; int num ; int dp [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; }
相关文章推荐
- hdoj1023 Train Problem II
- 人工智能简述
- Ubuntu 15.04 设置thin1.6.4作为Rails4.2.5的默认服务器
- PLMN http://baike.baidu.com/link?url=OwyPKkWC2NhTD3-SZ7W7I32RNI254nfXTo0F2GVNj4mulMZWV9WzTOxgtSmHf
- 找不到或无法加载主类(Could not find or load main class )
- Xenomai
- WebDriverWait
- 2014 Multi-University Training Contest 1 — D. Task
- svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
- With my girl unexpected gains!
- Daily Scrum – 1/18
- 0.0 人工智能笔记的说明
- Develop--Training(七)Getting Started--支持不同的设备
- [LeetCode217]Contains Duplicate
- Android笔记--对Android的四种LaunchMode和smail文件和scheme和http 的get、post的理解
- illustrator将图片转换成ai路径
- LightOJ1086 Jogging Trails(欧拉回路+中国邮递员问题+SPFA)
- 关于Cookie中的path、domain、expire、secure属性详述
- Factorial Trailing Zeroes
- Gitlab RPC failed; result=22, HTTP code = 413 解决方法