poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)
经典的数塔模型。
动态转移方程: dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j];
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 500
using namespace std; int dp[N][N];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
for(int j=;j<=i;j++) scanf("%d",&dp[i][j]);
for(int i=n-;i>=;i--)
for(int j=;j<=i;j++)
dp[i][j]=dp[i][j]+max(dp[i+][j],dp[i+][j+]);
printf("%d\n",dp[][]);
return ;
}
只用一维数组。注意 保证下一层的数由上一层推出,不受同一层的干扰。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 500
using namespace std; int dp[N];
int main()
{
//Read();
int n,a,r=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=i;j>=;j--)
{
scanf("%d",&a);
dp[j]=max(dp[j],dp[j-])+a;
r=max(r,dp[j]);
//printf("%d ",dp[j]);
}
//printf("\n");
}
printf("%d\n",r);
return ;
}
poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)的更多相关文章
- POJ 3176 Cow Bowling(dp)
POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...
- poj 1163 The Triangle &poj 3176 Cow Bowling (dp)
id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...
- POJ 3176 Cow Bowling
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13016 Accepted: 8598 Desc ...
- poj 3176 Cow Bowling(dp基础)
Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...
- poj 3176 Cow Bowling(区间dp)
题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...
- POJ - 3176 Cow Bowling 动态规划
动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...
- POJ 3176 Cow Bowling (水题DP)
题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- POJ 3176:Cow Bowling
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13464 Accepted: 8897 Desc ...
随机推荐
- 02.XMemcached的使用
关于XMemcached的介绍或文档请参考:https://code.google.com/p/xmemcached/wiki/User_Guide_zh 关于Memcached的命令 ...
- 【BZOJ】【1597】【USACO 2008 Mar】土地购买
DP/斜率优化 Orz Hzwer…… 想到排序了,但没想到其实可以将序列转化为x递增且y递减的序列……因为x是递增的,若y[i]>y[i-1]那么第i-1个就足够小……以至于可以在搞定第 i ...
- Java多线程——<七>多线程的异常捕捉
一.概述 为什么要单独讲多线程的异常捕捉呢?先看个例子: public class ThreadException implements Runnable{ @Override public void ...
- IIS Express 及 vs2008下使用IIS Express
介绍 IIS Express 开发 ASP.NET 的应用程序是我的主要工作.当然我会选择最适合的开发环境.客户多属于企业用户,我的开发的选择,多半是 ASP.NET Web Application ...
- 理解Session的几种模式
一.写在前面 我们在使用ASP.NET开发的过程中,有时会进行数据存储以实现请求前后的状态保持(HTTP是无状态保持的协议),而Session作为一种快速简单易于实现的方式被我们经常使用,当然如果出于 ...
- 后缀树系列一:概念以及实现原理( the Ukkonen algorithm)
首先说明一下后缀树系列一共会有三篇文章,本文先介绍基本概念以及如何线性时间内构件后缀树,第二篇文章会详细介绍怎么实现后缀树(包含实现代码),第三篇会着重谈一谈后缀树的应用. 本文分为三个部分, 首先介 ...
- LA 3211
As you must have experienced, instead of landing immediately, an aircraft sometimes waits in a holdi ...
- POJ 1222
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6196 Accepted: 40 ...
- Web App中的Flexbox应用
虽然语法可能比较混杂,但 Flexbox 还是名不虚传的.它创造的是可伸缩的.有弹性的.可改变视觉顺序的智能盒子.它提供了简单的CSS布局方案范例让容器总是处于垂直水平居中的位置.使用盒模型来工作是非 ...
- JAVA WEB新进展
哈哈,终于搞来页面来了,丑是丑,但是好的进展. 高手的思路,自己用库建连接池,确实利害. 最关键的一个HELPER代码: package org.smart4j.chapter2.helper; im ...