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 ...
随机推荐
- 第二章《深入C#数据类型》项目经理评分
一:创建MyOffices项目,创建UserInfo类,用来存储员工 工号,姓名,年龄,评价,年度得分 二:创建查看评分窗体(frmShow),添加定义员工数组,将员工数据绑定到frmShow窗体的L ...
- 用FLASH,安智和IOS打电话方法
打电话?你直接urlrequest不就打出去了吗普通网页http://xxx电话tel://xxx要啥ane
- android:TableLayout表格布局详解
1.TableLayout简介2.TableLayout行列数的确定3.TableLayout可设置的属性详解4.一个包含4个TableLayout布局的实例及效果图一.Tablelayout简介 ...
- 主机无法访问虚拟机Linux的apache
在虚拟机linux里安装了httpd,即appache,启动后,按正常情况在主机是可以用浏览器通过访问虚拟机linux的ip来访问的.如果出现无法访问的情况,解决办法可以参考如下: 这里我的虚拟机联网 ...
- Debian8.2 下的软件配置
Add "ll" to alias: ~/.bashrc里面实际上已经有这个alias,把注释去掉就可以了 小红点(指点杆)的启用 这个版本可以在系统配置里把触摸板关掉, 但是这个 ...
- workqueue机制分析之wb_workfn函数
上面一篇文章说到: process_one_work中最重要的一件事情就是worker->current_func(work); 这里就具体到一项很具体的任务了,由于我要研究文件系统嘛,所以很自 ...
- 利用ThinkPHP自带的七牛云驱动上传文件到七牛云以及删除七牛云文件方法
一.准备工作 1.注册七牛云账号 2.选择对象储存->创建空间->设置为公开 3.在config配置文件中添加以下代码 'UPLOAD_FILE_QINIU' => array ( ...
- .NET:Entity Framework 笔记
有二年没关注EF,今天无意试了下发现跟主流的Hibernate等ORM框架越来越接近了,先看下Entity类的定义: using System; using System.Collections.Ge ...
- win7下给右键菜单添加启动cmd命令
win7下给右键菜单添加启动cmd命令 (2013-07-20 19:20:56) 转载▼ 标签: it 右键 cmd 分类: 小软件操作技巧 最近编辑器在用windows下的gvim,但进入 ...
- Java 基础【09】 日期类型
java api中日期类型的继承关系 java.lang.Object --java.util.Date --java.sql.Date --java.sql.Time --java.sql.Time ...