【数位DP】双峰数Bi-peak Number HDU3565 (目前TLE。。。)
发布时间:2020-12-14 04:11:53 所属栏目:大数据 来源:网络整理
导读: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} (D
Bi-peak NumberTime 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].
?
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.
?
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
?
Sample Output
?
Author
love8909
?
Source
2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC
?
其实这一题的难点不在状态,在于它不是找个数,而是去找其中最大的那个(也就是不满足减法原理) 首先说说状态(我设计了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,4,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,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,sizeof(L)); memset(R,sizeof(R)); len=predoing(a,L); len=max(len,predoing(b,R)); printf("%dn",calc(len,1,0)); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |