Http://www.codeforces.com/gym/100827/attachments
Hill Number
Time limits:5000 MS Memory Limits: 200000 KB
64 -bit interger IO format: %lld 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's a hill number, print the number of hill numbers less than it. If It is not a hill number, print-1.
Input
Input would start with a single line giving the number of test cases. Each of the test case is a single positive an integer on a single line with an up to digits. The result would 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
5105510110001234321
Output for Sample Input10
55
-1
715
94708 https://open.kattis.com/problems/hillnumbers can hand over the topic here to you a number, ask you this is not the peak number. The so-called peak number is similar to 12321, but it can all be the same. If it is the number of peaks, find the number of all the peaks that are smaller than him. Idea: The number is very large, a look is the digital DP. DP[I][K][W], which is the number of W when the position is the number K. W has 3 states, an increment, a decrement, a first increment, and then a decrement. 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>#defineINF 1<<30#defineMOD 1000000007#definell Long Long#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#definePi ACOs (-1.0)using namespacestd;Const intMAXN = -; ll dp[maxn][Ten][5];intDIGIT[MAXN];CharS[MAXN];intOkChars[]) { intLen =strlen (s); intFlag =0; for(inti =1; i < Len; i++){ if(S[i] > s[i-1] &&flag) { return 0; } Else if(S[i] < s[i-1] &&!flag) {Flag=1; } } return 1;} ll Dfs (intLenintWintIsmax,intPA) { if(len = =0)return 1; if(!ismax && dp[len][pa][w]) {//the number of times when the first bit is the PA number state is W returnDp[len][pa][w]; } ll ans=0; LL fans=0; intMAXV = Ismax? Digit[len]:9; for(inti =0; I <= MAXV; i++){ if(W = =1){ if(I >=PA) {ans+ = DFS (len-1,1, Ismax && i = =maxv,i); } Else{ans+ = DFS (len-1,3, Ismax && i = =maxv,i); } } Else if(W = =2){ if(I > PA)Continue; Else{ans+ = DFS (len-1,2, Ismax && i = =maxv,i); } } Else if(W = =3) { if(I > PA)Continue; Else{ans+ = DFS (len-1,3, Ismax && i = =maxv,i); } } } if(!ismax) Dp[len][pa][w] =ans; returnans;}voidsolve () {if(!OK (s)) {printf ("-1\n"); return ; } intLen =0; Memset (DP,0,sizeof(DP)); for(inti = strlen (s)-1; I >=0; i--) {digit[++len] = s[i]-'0'; } printf ("%lld\n", DFS (Len,1,1,-1) -1);}intMain () {intT; scanf ("%d",&t); while(t--) {scanf ("%s", s); Solve (); } return 0;}
Codeforces Hill number Digit DP