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(dp)


下一篇:【转】VS2012 中文版转英文版 英文版转中文版 界面语言切换