Codeforces Round #286 (Div. 2) C题 Mr. Kitayuta, the Treasure Hunter (DFS+记忆化DP)
来源:互联网 发布:一夫多妻 知乎 编辑:程序博客网 时间:2024/06/07 09:28
题目地址:http://codeforces.com/contest/505/problem/C
从d点开始,每个点都有三个方向,形成了一棵树,那么就从跟结点开始进行dfs查找,dp数组记录当前的点和长度,当这两个条件相同的时候,显然,后面的子树是完全相同的,于是用记忆化来优化。
代码如下:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=100000000;const int INF=0x3f3f3f3f;const double eqs=1e-8;int dp[31000][300], w[31000], max1, max2;int dfs(int cur, int lenth){ if(cur>max1) return 0; if(dp[cur][lenth]!=-1) return dp[cur][lenth]; if(lenth>1) dp[cur][lenth]=max(dp[cur][lenth],dfs(cur+lenth-1,lenth-1)+w[cur]); dp[cur][lenth]=max(dp[cur][lenth],dfs(cur+lenth,lenth)+w[cur]); dp[cur][lenth]=max(dp[cur][lenth],dfs(cur+lenth+1,lenth+1)+w[cur]); max2=max(max2,dp[cur][lenth]); return dp[cur][lenth];}int main(){ int n, d, i, j, x, len, pre; max1=-1; scanf("%d%d",&n,&d); memset(w,0,sizeof(w)); memset(dp,-1,sizeof(dp)); for(i=0; i<n; i++) { scanf("%d",&x); w[x]++; max1=max(max1,x); } max2=0; dfs(d,d); printf("%d\n",max2); return 0;}
1 0
- Codeforces Round #286 (Div. 2) C题 Mr. Kitayuta, the Treasure Hunter (DFS+记忆化DP)
- Codeforces Round #286 (Div. 2) C. Mr. Kitayuta, the Treasure Hunter(经典的DP)
- Codeforces Round #286 (Div. 2) C. Mr. Kitayuta, the Treasure Hunter——dp
- Codeforces Round #286 (Div. 2) C. Mr. Kitayuta, the Treasure Hunter+dp+优化
- Codeforces Round #286 (Div. 2) C Mr. Kitayuta, the Treasure Hunter ( DP )
- Codeforces Round #286 (Div. 2) C. Mr. Kitayuta, the Treasure Hunter dp+范围压缩
- Codeforces Round #286 (Div. 2) C. Mr. Kitayuta, the Treasure Hunter dp
- Codeforces Round #286 C. Mr. Kitayuta, the Treasure Hunter dp
- Codeforces Round #286 (Div. 2) C. Mr. Kitayuta, the Treasure Hunter
- Codeforces Round #286 (Div. 2)C. Mr. Kitayuta, the Treasure Hunter
- 【DP】 Codeforces Round #286 A - Mr. Kitayuta, the Treasure Hunter
- codeforces 505C Mr. Kitayuta, the Treasure Hunter(dp)
- #286 (Div. 2) C. Mr. Kitayuta, the Treasure Hunter
- Codeforces 505C Mr. Kitayuta, the Treasure Hunter【记忆化搜索+分块数据处理】好题
- codeforces 505C C. Mr. Kitayuta, the Treasure Hunter (dp)
- codeforces 505C Mr. Kitayuta, the Treasure Hunter(DP)
- codeforces 505C Mr. Kitayuta, the Treasure Hunter(DP)
- [Codeforces Round #286 DIV1A (CF506A)] Mr. Kitayuta, the Treasure Hunter
- 海思3531芯片资料汇总--转载
- java日志-各种日志框架使用讲解-***
- 6个流行的直线提取算法——基于2D测距数据(摘自自主移动机器人导论)
- bash(详解)
- spi 上拉下拉
- Codeforces Round #286 (Div. 2) C题 Mr. Kitayuta, the Treasure Hunter (DFS+记忆化DP)
- 微软发布Windows 10:连Windows 7都能免费升级了
- C# 反射技术应用
- JAVA学习笔记11——继承+重写
- MTK项目配置文件ProjectConfig.mk解析
- 创建最大堆,对指定位置元素进行删减
- common lisp教程
- (转载)linux命令之四十free 命令
- 淘宝内部分享:怎么跳出MySQL的10个大坑