POJ 1163&& 3176 The Triangle(DP)

The Triangle

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 41169   Accepted: 24882

Description

7
3   8
8   1   0
2   7   4   4
4   5   2   6   5

(Figure 1)

Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.

Input

Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle,
all integers, are between 0 and 99.

Output

Your program is to write to standard output. The highest sum is written as an integer.

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30

Source

IOI 1994

    题意:求走过这个三角形时的最大数值。起点为第一行的唯一的那一个数,终点是第n行的某一个数。当中要走dp[i][j]的话。他的上一步仅仅能是dp[i-1][j-1]或者dp[i-1][j];

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define inf 9999
#define INF -9999

using namespace std;

int n;
int dp[361][361];

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=i;j++)
            {
                scanf("%d",&dp[i][j]);
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=i;j++)
            {
                dp[i][j] += max(dp[i-1][j-1],dp[i-1][j]);
            }
        }
        int maxx = 0;
        for(int i=1;i<=n;i++)
        {
            if(maxx<dp[n][i])
            {
                maxx = dp[n][i];
            }
        }
        printf("%d\n",maxx);
    }
    return 0;
}
时间: 2024-08-27 01:51:02

POJ 1163&amp;&amp; 3176 The Triangle(DP)的相关文章

POJ 1163&amp;&amp; 3176 The Triangle(DP)

The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41169   Accepted: 24882 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed

【POJ 1191】 棋盘分割(DP)

[POJ 1191] 棋盘分割(DP) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13811   Accepted: 4917 Description 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘.(每次切割都只能沿着棋盘格子的边进行) 原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分

poj 3267 The Cow Lexicon (dp)

链接:poj 3267 题意:给定一个主串,和单词序列,问最少在主串删除多少字母, 可以使其匹配到单词序列,如 browndcodw cow milk white black brown farmer 删除主串中的两个d,brown和cow就与整个主串匹配了 分析:dp[i]表示从主串中第i个字符开始,到第L个字符(结尾处) 这段区间最少要删除的字符数, 则状态转移方程为: dp[i]=dp[i+1]+1  不能匹配时 dp[i]=min(dp[i],dp[pos]+pos-i-m)  可以匹配

poj - 1953 - World Cup Noise(dp)

题意:n位长的01序列(0 < n < 45),但不能出现连续的两个1,问序列有多少种. 题目链接:http://poj.org/problem?id=1953 -->>设dp[i][j]表示前 i 位中第 i 位为 j 时的序列数,则状态转移方程为: dp[i][0] = dp[i - 1][0] + dp[i - 1][1]; dp[i][1] = dp[i - 1][0]; 因为对于相同的n,其结果是固定的,所以可以对一个n只计算一次,然后记住她.. #include <

poj - 1050 - To the Max(dp)

题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 -->>将二维压缩为一维,对一维进行dp求解. 将二维压缩成一维: 1.第1行 2.第2行加第1行 3.第3行加第2行加第1行 -- N.第N行加第N-1行加--加第1行 1.第2行 2.第3行加第2行 -- 1.第N行 对于一维情况,设dp[i]表示以第i个元素结尾的最大连续和,则状态转移方程为

POJ 3486 &amp; HDU 1913 Computers(dp)

题目链接:PKU:HDU: PKU:http://poj.org/problem?id=3486 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1913 Description Everybody is fond of computers, but buying a new one is always a money challenge. Fortunately, there is always a convenient way to deal wi

POJ 3666 Making the Grade (DP)

题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大的是 j,那么转移方程为,d[i][j] = abs(j-w[i])+min(d[i-1][k]);(k<=j). 用滚动数组更加快捷,空间复杂度也低. 代码如下: #include <cstdio> #include <string> #include <cstdlib&

POJ 3666 Making the Grade(DP)

题目链接:点击打开链接 题意:给n个数, 要求把这个数列变成非减或者非增数列, 求最小该变量之和. 思路:可以这样设计DP, d[i][j]表示第i个数变成j的最优解, 这样它转移到d[i-1][k], 其中k<=j, 这是变成上升的, 代价是abs(a[i] - j). 但是数太大了, 又因为每个数肯定会变成这些数中的一个数会最优, 所以我们不妨将n个数先离散化一下, 这样状态就表示成d[i][j]表示第i个数变成第j小的数, 转移到d[i-1][k],其中k<=j. 但是这样还是超时了,

POJ 2581 Exact Change Only(dp)

Language: Default Exact Change Only Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2584   Accepted: 883 Description Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we at?" Thibod