http://www.codeforces.com/gym/100827/attachments

Hill Number

Time Limits:  5000 MS   Memory Limits:  200000 KB

64-bit interger IO format:  %lld   Java class name:  Main

Description

A Hill Number is a number whose digits possibly rise and then possibly fall, but never fall and then rise.

• 12321 is a hill number.
• 101 is not a hill number.
• 1111000001111 is not a hill number.

Given an integer n, if it is a hill number, print the number of hill numbers less than it. If it is not a hill number, print -1.

Input

Input will start with a single line giving the number of test cases. Each test case will be a single positive integer on a single line, with up to 70 digits. The result will always fit into a 64-bit long.

Output

For each test case, print -1 if the input is not a hill number. Print the number of hill numbers less than the input value if the input value is a hill number.

Sample Input

5
10
55
101
1000
1234321

Output for Sample Input

10
55
-1
715
94708
 
给你一个数,问你这个是不是山峰数。所谓山峰数,就是类似于12321这种,但是可以都一样。如果是山峰数,求出比他小的所有的山峰数个数。
 
思路:
数字很大,一看就是数位dp。
dp[i][k][w],表示第i位,为数字k的时候,状态是w的个数。w有3中状态,一种递增,一种递减,一种先递增,后递减。
dp[i][k][w] = sum(dp[i-1][j][p]);
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
ll dp[MAXN][][];
int digit[MAXN];
char s[MAXN];
int ok(char s[])
{
int len = strlen(s);
int flag = ;
for(int i = ; i < len; i++){
if(s[i] > s[i-] && flag){
return ;
}
else if(s[i] < s[i-] && !flag){
flag = ;
}
}
return ;
}
ll dfs(int len,int w,int ismax,int pa)
{
if(len == )return ;
if(!ismax && dp[len][pa][w]){//第i位 为pa数字 状态为w的时候的个数
return dp[len][pa][w];
}
ll ans = ;
ll fans = ;
int maxv = ismax ? digit[len] : ;
for(int i = ; i <= maxv; i++){
if(w == ){
if(i >= pa){
ans += dfs(len-,,ismax && i == maxv,i);
}
else {
ans += dfs(len-,,ismax && i == maxv,i);
}
}
else if(w == ){
if(i > pa)continue;
else {
ans += dfs(len-,,ismax && i == maxv,i);
}
}
else if(w == ) {
if(i > pa)continue;
else {
ans += dfs(len-,,ismax && i == maxv,i);
}
}
}
if(!ismax)dp[len][pa][w] = ans;
return ans;
}
void solve()
{
if(!ok(s)){
printf("-1\n");
return ;
}
int len = ;
memset(dp,,sizeof(dp));
for(int i = strlen(s) - ; i >= ; i--){
digit[++len] = s[i] - '';
}
printf("%lld\n",dfs(len,,,-) - );
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%s",s);
solve();
}
return ;
}

codeforces Hill Number 数位dp的更多相关文章

  1. 多校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 用作标记,当现在枚举的数小 ...

  2. beautiful number 数位DP codeforces 55D

    题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...

  3. CodeForces - 55D(数位dp,离散化)

    题目来源:http://codeforces.com/problemset/problem/55/D Volodya is an odd boy and his taste is strange as ...

  4. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  5. 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 ...

  6. Fzu2109 Mountain Number 数位dp

    Accept: 189    Submit: 461Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description One ...

  7. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  8. FZU - 2109 Mountain Number 数位dp

    Mountain Number One integer number x is called "Mountain Number" if: (1) x>0 and x is a ...

  9. 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 ...

随机推荐

  1. Codeforces 461B. Appleman and Tree[树形DP 方案数]

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. ArrayList如何实现线程安全

    一:使用synchronized关键字,这个大家应该都很熟悉了,不解释了: 二:使用Collections.synchronizedList();使用方法如下: 假如你创建的代码如下:List< ...

  3. AC日记——紧急措施 openjudge 1.7 22

    22:紧急措施 总时间限制:  1000ms 内存限制:  65536kB 描述 近日,一些热门网站遭受黑客入侵,这些网站的账号.密码及email的数据惨遭泄露.你在这些网站上注册若干账号(使用的用户 ...

  4. django复习笔记3:urls/views/templates三板斧

    0.先看看文件结构 mysite/ mysite/ ├── __pycache__ │   └── manage.cpython-.pyc ├── blog │   ├── __init__.py │ ...

  5. Js多国时间动态更新

    Js多国时间动态更新 点击下载

  6. Java集合系列:-----------07Map架构

    我们之前了解了Collection框架,我们再来了解一下Map架构.前面我们讲了Conllection中的List结合,没有讲Collection中的Set集合, 因为Collection框架中的Se ...

  7. css3在不同型号手机浏览器上的兼容一览表

    网上搜集了css3对不同系统手机浏览器的支持情况(ios/android/winphone)备份一下以便查看.  以下资料由微信产品部"白树"整理, 转载请注明. √:完全支持   ...

  8. AlwaysOn Group Listener

    1.Listener是什么 Listener实际上是一个 VirtualNetworkName,客户端通过这个VNN来连接的具体的sqlserver实例 .Listener包含了DNS名称,port和 ...

  9. 在SharePoint列表中使用自增栏

    问:sps2010里能不能新建个栏,数字型的,自动加一 答:在SharePoint里,有很多方法可以实现一个自增栏.在这里,我将介绍其中两种方式. 1.计算栏 2.列表项事件接收器 1.采用计算栏来实 ...

  10. express:webpack dev-server中如何将对后端的http请求转到https的后端服务器中?

    在上一篇文章(Webpack系列:在Webpack+Vue开发中如何调用tomcat的后端服务器的接口?)我们介绍了如何将对于webpack-dev-server的数据请求转发到后端服务器上,这在大部 ...