codeforces Hill Number 数位dp
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
55
-1
715
94708
#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的更多相关文章
- 多校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 用作标记,当现在枚举的数小 ...
- beautiful number 数位DP codeforces 55D
题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...
- CodeForces - 55D(数位dp,离散化)
题目来源:http://codeforces.com/problemset/problem/55/D Volodya is an odd boy and his taste is strange as ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- 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 ...
- Fzu2109 Mountain Number 数位dp
Accept: 189 Submit: 461Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description One ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- FZU - 2109 Mountain Number 数位dp
Mountain Number One integer number x is called "Mountain Number" if: (1) x>0 and x is a ...
- 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 ...
随机推荐
- UIScrollView解决无法触发手势
//创建一个分类 //.h #import <UIKit/UIKit.h> @interface UIScrollView (Touch) - (void)touchesBegan:(NS ...
- css相关问题
display:none和visibility:hidden的区别? 前几天遇到的这个问题,表格布局:::::display:none 隐藏对应的元素,在文档布局中不再给它分配空间,它各边的元素会合拢 ...
- memcache分布式 [一致性hash算法] 的php实现
最近在看一些分布式方面的文章,所以就用php实现一致性hash来练练手,以前一般用的是最原始的hash取模做分布式,当生产过程中添加或删除一台memcache都会造成数据的全部失效,一致性hash就是 ...
- Java集合系列:-----------02Collection架构
出处:http://www.cnblogs.com/skywang12345/p/3308513.html 我们知道Collection是和Map架构平级的,我们看一下这个架构是怎样的. 他主要的两个 ...
- 通过ssh tunnel连接内网ECS和RDS
通过ssh tunnel连接内网ECS和RDS 这里讲了ssh tunnel的原理.很清晰. 此后又给外网访问内网增加了一种思路.感觉特别棒. 拓宽了思路:
- python 播放 wav 文件
未使用其他库, 只是使用 pywin32 调用系统底层 API 播放 wav 文件. # Our raison d'etre - playing sounds import pywintypes im ...
- swift调用oc语言文件,第三方库文件或者自己创建的oc文件——简书作者
Swift是怎样调用OC的第三方库的呢?请看下面详情: 情况一: 1.首先打开Xcode,iOS->Application->Single View Application, 选Next. ...
- http缓存提高性能
秋招也算是正式结束了,现在整理一下笔记,当作巩固一下知识,也希望这个对大家有帮助 http 缓存 和 cdn 缓存可以说是面试必问的问题,竟然是必问的问题,那就总结全面一点- http缓存机制 缓存分 ...
- lecture9-提高模型泛化能力的方法
HInton第9课,这节课没有放论文进去.....如有不对之处还望指正.话说hinton的课果然信息量够大.推荐认真看PRML<Pattern Recognition and Machine L ...
- Theano1.1-安装
之前一直想弄theano,可是python不是很懂,在学习了一段时间之后开始安装theano.当然官网上的安装资料是全,可是也太繁琐了.这里介绍的是最简单,最方面的安装theano的方法.官网首页:h ...