hdu_3565_Bi-peak Number(数位DP)
2016-06-03 22:12
330 查看
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3565
题意:给你一个区间,让你找这个区间内有两个山峰的数的最大和,什么是两个山峰,比如121121 第一个2 和第二个2就是两个峰
题解:求的这个不满足dfs(y)-dfs(x)所以只有用一个上限和一个下限来限制
s=0:前导0的状态;
s=1:第一个山峰的上坡,且不能立马下坡;
s=2:第一个山峰的上坡,且最后一点能看成是最高点,下一个点可以是下坡;
s=3:第一个山峰的下坡;
s=4:第二个山峰的上坡,且不能立马下坡;
s=5:第二个山峰的上坡,且最后一点能看成是最高点,下一个点可以是下坡;
s=6:第二个山峰的下坡;
s=-1:其余不合法的状态。
设dp[i][j][k]为考虑第i位,上一个数字为j,状态s为k的最大数位和
#include<cstdio> #define MAX(a,b) ((a)>(b)?(a):(b)) #define F(i,a,b) for(int i=a;i<=b;i++) int dp[25][10][7],ss[25],ee[25],t,len,an;unsigned __int64 x,y; int dfs(int pos,int pre,int s,int fx,int fy){ if(!pos)return s==6?0:-1; if(!fx&&!fy&&dp[pos][pre][s]!=-2)return dp[pos][pre][s]; int st=fx?ss[pos]:0,end=fy?ee[pos]:9,ans=-1,tp,ns; F(i,st,end){ ns=s; if(!s&&i)ns=1; else if(s==1){if(i>pre)ns=2;else ns=-1;} else if(s==2){if(i<pre)ns=3;else if(i==pre)ns=-1;} else if(s==3&&i>=pre){if(i)ns=4;else ns=-1;} else if(s==4){if(i>pre)ns=5;else ns=-1;} else if(s==5){if(i<pre)ns=6;else if(i==pre)ns=-1;} else if(s==6&&i>=pre)ns=-1; if(ns!=-1)tp=dfs(pos-1,i,ns,fx&&i==st,fy&&i==end),ans=(tp==-1?ans:MAX(ans,i+tp)); } if(!fx&&!fy)dp[pos][pre][s]=ans; return ans; } int main(){ F(i,0,24)F(j,0,9)F(k,0,6)dp[i][j][k]=-2; scanf("%d",&t); F(i,1,t){ scanf("%I64u%I64u",&x,&y); for(len=0;y;x/=10,y/=10)ss[++len]=x%10,ee[len]=y%10; an=dfs(len,0,0,1,1),printf("Case %d: %d\n",i,an==-1?0:an); } return 0; }View Code
相关文章推荐
- 学习进度13
- linux高级原6
- 萤火虫算法
- DFS之拼接长棍
- staruml
- 深度学习
- C之多线程(例子很不错)
- CSS:关于CSS Hack
- Toast控件
- 四、Shell输入、输出功能和字符颜色设置
- 团队项目-个人博客6.3
- Ubuntu安装软件提示Package has no installation candidate解决方法
- “PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- ubuntu下maven3.3.9的安装
- VM VirtualBox虚拟机中OracleLinux-R6-U5-Server-x86_64系统下安装oracle11gr2
- python + 机器学习 + 爬虫
- 智软科技医疗器械GSP监管软件通过多省市药监局检查
- 【JS】The differences between 基本类型 and 引用类型
- 排序算法——冒泡的优化
- windows平台Emacs单实例原理、设置及右键菜单的添加