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 ...
随机推荐
- RS232与RS485时序分析
转载于:RS232,RS485波形分析 经常遇到初学者,对单片机串行通讯出了问题不知道如何办的情况.其实最有效的调试方法是用示波器观察收发数据的波形.通过观察波形可以确定以下情况: 是否有数据接收或发 ...
- 【Selenium】自动化调试后C盘越来越大
在本机调试了一段时间自动化脚本后发现C盘占用越来越大,增长速度比较明显 通过360等工具清理系统垃圾表明并不好使 最后在系统临时文件中看到大量因为调试或不正常结束而Driver而产生的临时文件 具体方 ...
- 设计的SOA架构
新来老大年前开会说各位同学,公司业务越来越重,未来几年要成倍增长......,要梳理出一套新架构,才能更好的支持N万用户.....,以后升职加薪当上....打败..... 想想还有点小激动呢,于是过年 ...
- 乱序双发射 和 GHB的分支预测
乱序执行(out-of-order execution)是指CPU采用了允许将多条指令不按程序规定的顺序分开发送给各相应电路单元处理的技术.比方Core乱序执行引擎说程序某一段有7 条指令,此时CPU ...
- 插入排序InsertionSort
/** * * @author Administrator * 功能:插入排序法 */ package com.test1; import java.util.Calendar; public cla ...
- 164. Maximum Gap
题目: Given an unsorted array, find the maximum difference between the successive elements in its sort ...
- 2、RenderScript的计算(2013.05.07)
一.官方文档翻译: Android Renderscript计算 参考网址: http://blog.csdn.net/fireofstar/article/details/7748143 http: ...
- Java Memory Management(1)
Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...
- Wpf配置文件属性
public MainWindow() { InitializeComponent(); this.WindowState = Properties.Settings.Default.WindowSt ...
- 【转】Eclipse提示No java virtual machine(转载)
原文网址:http://blog.sina.com.cn/s/blog_6cd73dfb01013zkg.html 第一次运行Eclipse,经常会提示下面的问题:... No java virtua ...