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 ...
随机推荐
- AC日记——判断字符串是否为回文 openjudge 1.7 33
33:判断字符串是否为回文 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个字符串,输出该字符串是否回文.回文是指顺读和倒读都一样的字符串. 输入 输入为一行字符串(字符串中 ...
- 批处理文件指定jre路径启动java桌面应用程序
应用场景: 我开发了一个应用程序,并连同jre一起刻成光盘,提供给用户,用户直接双击批处理文件即可运行,而不需要自己额外装jre. 目录组织结构如下: client |-images |-jre |- ...
- 强加密RNGCryptoServiceProvider
.net RNGCryptoServiceProvider 对应 java SecureRandom public class SecureRandomextends Random This cla ...
- 利用 Process Monitor 找出某个 Windows 选项所对应的注册表值
多 时候我们要调整一项 Windows 的功能时只需更改一下注册表即可实现.而很多大家眼中所谓的高手,对 Windows 注册表更是玩得出神入化.难道这些高手把 Windows 注册表都记下来了?答案 ...
- [No000034]知乎-长期接收碎片化知识有什么弊端?
你所接受的一切信息,构成了你的思维方式. 所以,长期接受碎片信息的后果,就是让你的思维变得狭隘,难以进行复杂的思考. 碎片信息通常具备这样的特征: •它们往往是一些事实的集合而非逻辑 •它们往往大量简 ...
- java 22 - 4 多线程的代码实现的方式1
需求:我们要实现多线程的程序. 如何实现呢? 由于线程是依赖进程而存在的,所以我们应该先创建一个进程出来. 而进程是由系统创建的,所以我们应该去调用系统功能创建一个进程. Java是不能直接调用系统功 ...
- 修复垂直滑动RecyclerView嵌套水平滑动RecyclerView水平滑动不灵敏问题
在 Android 应用中,大部分情况下都会使用一个垂直滚动的 View 来显示内容(比如 ListView.RecyclerView 等).但是有时候你还希望垂直滚动的View 里面的内容可以水平滚 ...
- UIScrollView创建相册
1.设置滚动相册 1.1将存放图片数组传过来,及当前图片的索引 1.2在控制器中创建ScrollView,设置它的contentSize,contentOffset. 1.3通过传过来的图片数组创建U ...
- Python的高级特性7:闭包和装饰器
本节跟第三节关系密切,最好放在一起来看:python的高级特性3:神奇的__call__与返回函数 一.闭包:闭包不好解释,只能先看下面这个例子: In [23]: def outer(part1): ...
- RPM方式编译升级centos内核
[root@iZ2893wjzgyZ ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org [root@iZ2893wjzgyZ ...