The Triangle_DP
| ime Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 45620 | Accepted: 27612 |
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
Output
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int N=;
int mp[N][N];
int dp[N][N];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&mp[i][j]); }
dp[i][i]=dp[i-][i-]+mp[i][i];
} for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
if(i==) dp[i][j]=mp[i][j];
else if(j==) dp[i][j]=dp[i-][j]+mp[i][j];
else if(j==i) dp[i][j]=dp[i-][j-]+mp[i][j];
else dp[i][j]=max(dp[i-][j-]+mp[i][j],dp[i-][j]+mp[i][j]);
}
}
int ans=;
for(int i=;i<=n;i++)
{
if(ans<dp[n][i])
ans=dp[n][i];
}
cout<<ans<<endl;
}
return ;
}
The Triangle_DP的更多相关文章
随机推荐
- hdu ---(4517)小小明系列故事——游戏的烦恼(Dp)
小小明系列故事——游戏的烦恼 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- CI(CodeIgniter)框架介绍
CodeIgniter 是一个为用 PHP 编写网络应用程序的人员提供的工具包.它的目标是实现让你比从零开始编写代码更快速地开发项目,为此,CI 提供了一套丰富的类库来满足通常的任务需求,并且提供了一 ...
- python 将数据随机分为训练集和测试集
# -*- coding: utf-8 -*- """ Created on Tue Jun 23 15:24:19 2015 @author: hd "&qu ...
- JQ基础练习---图片划过变暗
简单分享下,划过一张图片其余图片变暗,图片划过变暗的简单效果,JQ实现主要是css写法跟思路变化. <script src="http://ajax.googleapis.com/aj ...
- 高度30px,宽度自适应,点线在文字中间
<style> .div{ position: relative; width: 100%; height: 30px; background: #ffff00} .div ...
- h5 本地存储和读取信息
总结:cookie的缺点很明显,最多只能存储4KB的数据,每个HTTP请求都会被传送回服务器,明文传输(除非你使用SSL)对于购物网站而言,cookie是非常重要的,为了实现购物车功能,把已选物品加入 ...
- Oracle select case when
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- 嵌套 click 第二层 click会叠加 导致 触发 多次
$("#appearHiddenDiv").click(function(){ $("#hiddenDiv").css({display:"block ...
- 用户列表-投资记录sql
--普通标.定向标.新手标.老互融计划-投资记录表select bid.borrow_id, (select yyb.borrow_valid_time from YYD_Borrow_BorrowI ...