修炼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 ...
随机推荐
- 解析好的静态页面.shtml浏览器无法解析.需要apache解析后再返回给浏览器
解析好的静态页面.shtml浏览器无法解析.需要apache解析后再返回给浏览器 让Apache支持SHTML(SSI)的配置方法 http.conf放开addtype text/html .shtm ...
- 第5章 字符串----char与String
1.java有8种基本数据类型: 数值型:整数类型(byte,short,int,long) :浮点类型(float,double) 字符型:char 布尔型:true,false 2.char: ...
- Linux入门学习教程:虚拟机体验之KVM篇
本文中可以学习到的命令: 1. aptitude 是apt-get 不会产生垃圾的版本 2. dpkg -L virtualbox 显示属于该包的文件 lsmod | grep kvmfi ...
- string 转 int,int 转 string
string str="12345"; int b=atoi(str.c_str()); 可以配合atof,转为double char buf[10]; sprintf(buf, ...
- xib storyboard
initWithNibName加载xib或者storyboard BLEViewController *controller = [[BLEViewController alloc] initWith ...
- 四维dp 或者 剪枝 + dfs Codeforces Beta Round #6 (Div. 2 Only) D
http://codeforces.com/contest/6/problem/D 题目大意:有一队人,排成一列,每个人都有生命值,你每次可以攻击2~n位置的一个的人,假设每次攻击的位置为pos,那么 ...
- Hibernate Tools
(声明)本文转自:http://linjia880714.iteye.com/blog/859334 hibernate-tools详细使用教程 使用hibernate-tool的版本是hiberna ...
- DataGirdView 编辑项时的验证
dgvConfig.DataSource = CreateTable(); dgvConfig.Columns["编号"].ReadOnly = true; //只读 dgvCon ...
- Object.setPrototypeOf 方法的使用
将一个指定的对象的原型设置为另一个对象或者null(既对象的[[Prototype]]内部属性). 语法 Object.setPrototypeOf(obj, prototype) 参数 obj 将被 ...
- 用ifconfig命令,只有lo,没有eth0的解决方案
解决方案: 1. 进入/etc/sysconfig/network-scripts 目录,发现有ifcfg-eth0,即网卡(驱动)存在但未启用. 2. 输入ifconfig -a命令,可显示eth0 ...