zoukankan      html  css  js  c++  java
  • 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]; 

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <vector>
     5 #include <cstring>
     6 #include <string>
     7 #include <algorithm>
     8 #include <string>
     9 #include <set>
    10 #include <functional>
    11 #include <numeric>
    12 #include <sstream>
    13 #include <stack>
    14 #include <map>
    15 #include <queue>
    16 #pragma comment(linker, "/STACK:102400000,102400000")
    17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
    18 
    19 #define ll long long
    20 #define inf 0x7f7f7f7f
    21 #define lc l,m,rt<<1
    22 #define rc m + 1,r,rt<<1|1
    23 #define pi acos(-1.0)
    24 
    25 #define L(x)    (x) << 1
    26 #define R(x)    (x) << 1 | 1
    27 #define MID(l, r)   (l + r) >> 1
    28 #define Min(x, y)   (x) < (y) ? (x) : (y)
    29 #define Max(x, y)   (x) < (y) ? (y) : (x)
    30 #define E(x)        (1 << (x))
    31 #define iabs(x)     (x) < 0 ? -(x) : (x)
    32 #define OUT(x)  printf("%I64d
    ", x)
    33 #define lowbit(x)   (x)&(-x)
    34 #define Read()  freopen("a.txt", "r", stdin)
    35 #define Write() freopen("b.txt", "w", stdout);
    36 #define maxn 1000000000
    37 #define N 500
    38 using namespace std;
    39 
    40 int dp[N][N];
    41 int main()
    42 {
    43     int n;
    44     scanf("%d",&n);
    45     for(int i=0;i<n;i++)
    46         for(int j=0;j<=i;j++) scanf("%d",&dp[i][j]);
    47     for(int i=n-2;i>=0;i--)
    48         for(int j=0;j<=i;j++)
    49         dp[i][j]=dp[i][j]+max(dp[i+1][j],dp[i+1][j+1]);
    50     printf("%d
    ",dp[0][0]);
    51     return 0;
    52 }

     只用一维数组。注意  保证下一层的数由上一层推出,不受同一层的干扰。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <vector>
     5 #include <cstring>
     6 #include <string>
     7 #include <algorithm>
     8 #include <string>
     9 #include <set>
    10 #include <functional>
    11 #include <numeric>
    12 #include <sstream>
    13 #include <stack>
    14 #include <map>
    15 #include <queue>
    16 #pragma comment(linker, "/STACK:102400000,102400000")
    17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
    18 
    19 #define ll long long
    20 #define inf 0x7f7f7f7f
    21 #define lc l,m,rt<<1
    22 #define rc m + 1,r,rt<<1|1
    23 #define pi acos(-1.0)
    24 
    25 #define L(x)    (x) << 1
    26 #define R(x)    (x) << 1 | 1
    27 #define MID(l, r)   (l + r) >> 1
    28 #define Min(x, y)   (x) < (y) ? (x) : (y)
    29 #define Max(x, y)   (x) < (y) ? (y) : (x)
    30 #define E(x)        (1 << (x))
    31 #define iabs(x)     (x) < 0 ? -(x) : (x)
    32 #define OUT(x)  printf("%I64d
    ", x)
    33 #define lowbit(x)   (x)&(-x)
    34 #define Read()  freopen("a.txt", "r", stdin)
    35 #define Write() freopen("b.txt", "w", stdout);
    36 #define maxn 1000000000
    37 #define N 500
    38 using namespace std;
    39 
    40 int dp[N];
    41 int main()
    42 {
    43     //Read();
    44     int n,a,r=0;
    45     scanf("%d",&n);
    46     for(int i=1;i<=n;i++)
    47     {
    48         for(int j=i;j>=1;j--)
    49         {
    50             scanf("%d",&a);
    51             dp[j]=max(dp[j],dp[j-1])+a;
    52             r=max(r,dp[j]);
    53             //printf("%d ",dp[j]);
    54         }
    55        //printf("
    ");
    56     }
    57     printf("%d
    ",r);
    58     return 0;
    59 }
  • 相关阅读:
    语言模型的压缩方法
    推荐算法之 Slope One 算法
    基于内容的推荐(Contentbased Recommendations)
    Txt文件转换为Excel文件
    WebResource 内嵌资源
    多层模态窗口showModalDialog页面提交及刷新
    屏蔽/自定义JavaScript脚本错误
    .net动态显示当前时间
    客户端自动累加
    中国IT管理之窥豹一斑
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4436749.html
Copyright © 2011-2022 走看看