修炼dp( 2 )
题解:dp+dfs.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = ;
int dp[maxn][maxn];
int cnt[maxn][maxn];
int n;
int dfs(int x,int y)
{
if(x==&&y==) return cnt[][];
if(y==) return dfs(x-,y)+cnt[x][y];
if(x>)return max(dfs(x-,y),dfs(x-,y-))+cnt[x][y];
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&cnt[i][j]);
}
}
int x,y;
scanf("%d %d",&x,&y);
for(int i=;i<=n;i++) dp[n][i] = cnt[n][i];
for(int i=n-;i>=x;i--)
{
for(int j=;j<=i;j++)
{
dp[i][j] = max(dp[i+][j],dp[i+][j+])+cnt[i][j];
}
}
int sum = ;
sum = dfs(x,y);
sum = sum+dp[x][y]-cnt[x][y];
printf("%d\n",sum);
return ;
}
/*
4
7
1 2
3 4 5
7 3 8 9
3 2
*/
卷珠帘
题解:dfs.咦,我本来是来炼dp的。。。不做水题了。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
const int maxn = ;
int cnt[maxn][maxn];
int vis[maxn][maxn];
int dp[maxn][maxn];
int dfs(int x,int y)
{
if(x<||y<) return ;
if(x>n||y>m) return ;
if(vis[x][y]) return dp[x][y];
vis[x][y] = ;
int a,b,c,d;
a = b = c = d = ;
if(cnt[x-][y]<cnt[x][y]) a = dfs(x-,y);
if(cnt[x+][y]<cnt[x][y]) b = dfs(x+,y);
if(cnt[x][y+]<cnt[x][y]) c = dfs(x,y+);
if(cnt[x][y-]<cnt[x][y]) d = dfs(x,y-);
dp[x][y] = max(max(a,b),max(c,d))+;
return dp[x][y];
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&cnt[i][j]);
}
}
int maxx = ;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
maxx = max(dfs(i,j),maxx);
}
}
printf("%d\n",maxx);
}
卷珠帘
题解:接触的第一道区间dp题。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = ;
char s[maxn];
int dp[maxn][maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s+);
int n = strlen(s+);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i == j) dp[i][j] = ;
else dp[i][j] = ;
}
dp[i][i-] = ;
}
//i和j分别表示一段连续字符的起点和终点
for(int l=;l<=n-;l++)
{
for(int i=;i+l<=n;i++)
{
int j = i+l;
if(s[i]=='('&&s[j]==')'||(s[i]=='['&&s[j]==']')) dp[i][j] = min(dp[i][j],dp[i+][j-]);
for(int k=i;k<j;k++)
{
dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
}
printf("%d\n",dp[][n]);
}
} /*
31
()([)][])
*/
卷珠帘
修炼dp( 2 )的更多相关文章
- 修炼dp(1)
从最简单的开始: POJ:The Triangle #include <cstdio> #include <algorithm> #include <cstring> ...
- hdoj 2059 :龟兔赛跑 (DP)[转]
转的别人的找了很多就这个比较好理解. Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下 ...
- 杭电2059(dp)
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 【巧妙消维DP】【HDU2059】龟兔赛跑
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- hdu 2059 龟兔赛跑(dp)
龟兔赛跑 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成 ...
- 龟兔赛跑(DP)
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 「NOI2013」小 Q 的修炼 解题报告
「NOI2013」小 Q 的修炼 第一次完整的做出一个提答,花了半个晚上+一个上午+半个下午 总体来说太慢了 对于此题,我认为的难点是观察数据并猜测性质和读入操作 我隔一会就思考这个sb字符串读起来怎 ...
- HDU 2059 龟兔赛跑(超级经典的线性DP,找合适的j,使得每个i的状态都是最好的)
龟兔赛跑 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 2059:龟兔赛跑(动态规划 DP)
龟兔赛跑 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
随机推荐
- OSPF 原理
关于OSPF的数据结构Link-State Protocol Data Structures链路状态路由器与距离矢量路由器,可以知道关于整个网络的更多信息Neighbor table:also kno ...
- asp的RegExp对象正则表达式功能用法
RegExp对象提供简单的正则表达式支持功能. RegExp对象的用法: 以下为引用的内容: Function RegExpTest(patrn, strng) Dim regEx, Match, M ...
- SCP测试服务器的上行/下行带宽
SCP测试服务器的上行/下行带宽,这个咋弄呢?有时间再研究一下.
- 【Tomcat】Tomcat配置之请求字符串编码
默认情况下,如果tomcat中部署的webservice或者web网站需要有中文的请求参数,而这时候我们直接在浏览器中输入中文那么接受到的将是乱码,无法达到我们的需求,这时候我们就需要对Tomcat的 ...
- 【中国剩余定理】 poj 1006
生理周期 简单模拟 对于超出23 * 28 * 33(21252)时进行求余运算即可. #include<stdio.h> int main() { //freopen("in ...
- 怎样将MySQL数据库上传到服务器
首先,需要将本地的数据库导出来,作为一个数据文件,以备稍后上传到服务器用,在本地登陆phpmyadmin控制面板: 登陆成功后,在左侧选择需要操作的数据库: 选择后,页面会自动刷新,然后再在右边点击[ ...
- 下拉刷新和上拉加载 Swift
转载自:http://iyiming.me/blog/2015/07/05/custom-refresh-and-loading/ 关于下拉刷新和上拉加载,项目中一直使用MJRefresh(原先还用过 ...
- gameUnity 0.2 网络游戏框架(计划)
能说的就是 请大家都耐心等待,不做国产垃圾,只追求国外经典,这就是 这套框架未来的发展 一:2d 3d场景融合 人物移动 2d 3d 层 移动 有差值(共6层,2d天空层.前景3d物体层有 景深), ...
- Android接收wifi路由器发送过来的一组字节数据
1.字节数组转换为字符串 byte[] byBuffer = new byte[20];... ...String strRead = new String(byBuffer);strRead = S ...
- Video Pooling
Video pooling computes video representation over the whole video by pooling all the descriptors from ...