Bi-peak Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 594 Accepted Submission(s): 165
Problem Description
A peak number is defined as continuous digits {D0, D1 … Dn-1} (D0 > 0 and n >= 3), which exist Dm (0 < m < n - 1) satisfied Di-1 < Di (0 < i <= m) and Di > Di+1 (m <= i < n - 1).
A number is called bi-peak if it is a concatenation of two peak numbers.
The score of a number is the sum of all digits. Love8909 is crazy about bi-peak numbers. Please help him to calculate the MAXIMUM score of the Bi-peak Number in the closed interval [A, B].
A number is called bi-peak if it is a concatenation of two peak numbers.
The score of a number is the sum of all digits. Love8909 is crazy about bi-peak numbers. Please help him to calculate the MAXIMUM score of the Bi-peak Number in the closed interval [A, B].
Input
The first line of the input is an integer T (T <= 1000), which stands for the number of test cases you need to solve.
Each case consists of two integers “A B” (without quotes) (0 <= A <= B < 2^64) in a single line.
Each case consists of two integers “A B” (without quotes) (0 <= A <= B < 2^64) in a single line.
Output
For the kth case, output “Case k: v” in a single line where v is the maximum score. If no bi-peak number exists, output 0.
Sample Input
3 12121 12121 120010 120010 121121 121121
Sample Output
Case 1: 0 Case 2: 0 Case 3: 8
Author
love8909
Source
其实这一题的难点不在状态,在于它不是找个数,而是去找其中最大的那个(也就是不满足减法原理)
首先说说状态(我设计了6种)
- 当前状态为0,只能转移到1
- 当前状态为1,只能转移到2
- 当前状态为2,可以转移到2或3
- 当前状态为3,转移到3或4均可
- 当前状态为4,只能转移到5
- 当前状态为5,可以转移到5或6
- 当前状态为6,只能转移到6
转移的时候要注意排除一些不合法的状态
这样,到递归边界的时候只需要判断状态是否为6即可
本来写的记忆化,不过后来发现要求(与求个数不同)。。。。
所以就把记忆化取了,就 T 了。。。。。
至今没想出什么记忆化的方法。。。。。
望高手帮忙
C++ TLE Code
/*http://blog.csdn.net/jiangzh7
By Jiangzh*/
#include<cstdio>
#include<cstring>
#define max(a,b) ((a)>(b)?(a):(b))
typedef unsigned long long LL;
LL a,b;
int L[30],R[30],len;
int f[30][5][5][15][10];
int predoing(LL a,int *num)
{
int le=0;
while(a)
{
num[++le]=a%10;
a/=10;
}
return le;
}
int calc(int pos,int d,int u,int last,int sta,int ans)
{
if(pos==0) return (sta==6)*ans;
int &res=f[pos][d][u][last][sta];
//if(res!=-1) return res;
res=0;
int st=d?L[pos]:0;
int ed=u?R[pos]:9;
for(int i=st;i<=ed;i++)
{
if(i==last&&sta!=3) continue;
int flag=sta;
if(sta==0)
{
if(i!=0) flag=1;
}
else if(sta==1)
{
if(i>last) flag=2;
else if(i<last) continue;
if(i==0) continue;
}
else if(sta==2)
{
if(i>last) flag=2;
else if(i<last) flag=3;
}
else if(sta==3)
{
if(i==0) continue;
int t=calc(pos-1,d&&i==L[pos],u&&i==R[pos],i,3,ans+i);
res=max(res,t);
t=calc(pos-1,d&&i==L[pos],u&&i==R[pos],i,4,ans+i);
res=max(res,t);
continue;
}
else if(sta==4)
{
if(i>last) flag=5;
else if(i<last) continue;
}
else if(sta==5)
{
if(i>last) flag=5;
else if(i<last) flag=6;
}
else{
if(i<last) flag=6;
else continue;
}
int t=calc(pos-1,d&&i==L[pos],u&&i==R[pos],i,flag,ans+i);
res=max(res,t);
}
return res;
}
int main()
{
freopen("bipeak.in","r",stdin);
freopen("bipeak.out","w",stdout);
while(scanf("%llu%llu",&a,&b)==2)
{
memset(f,-1,sizeof(f));
memset(L,0,sizeof(L));
memset(R,0,sizeof(R));
len=predoing(a,L);
len=max(len,predoing(b,R));
printf("%d\n",calc(len,1,1,0,0,0));
}
return 0;
}