DP file *2
来源:互联网 发布:员工管理系统java和sql 编辑:程序博客网 时间:2024/06/05 07:41
Description
73 88 1 02 7 4 44 5 2 6 5(Figure 1)
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
573 88 1 0 2 7 4 44 5 2 6 5
Sample Output
30
这题思路和dp file *1差不多,逐渐看得出动态规划所说的把大问题化为小问题的思想了。假设dp[i][j]就是(i,j)为终点的最大sum值,他就是要么从(i-1,j)来,要么从(i-1,j-1)来,所以dp[i][j]=max{dp[i-1][j],dp[i-1][j-1]}+data[i][j];假设是从(i-1,j)来,那么问题不就变成以(i-1,j)为终点的问题么,顺利把大问题化成了小问题。实现过程中只涉及到本身前面的点dp值,所以可以从开始一个一个往后推,做法比起dp file *1简单,同时这就是传闻中的数塔了。
贴代码:
#include<iostream>using namespace std;int data[101][101],dp[101][101];int main(){ int i,j,max,row,ans; cin>>row; for(i=1;i<=row;i++) { for(j=1;j<=i;j++) cin>>data[i][j]; } ans=dp[1][1]=data[1][1]; for(i=2;i<=row;i++) { for(j=1;j<=i;j++) { max=dp[i-1][j]>dp[i-1][j-1]?dp[i-1][j]:dp[i-1][j-1]; dp[i][j]=max+data[i][j]; if(dp[i][j]>ans) ans=dp[i][j]; } } cout<<ans<<endl;
}
- DP file *2
- DP file *1
- DP(2)
- dp专题2--简单dp
- 读取File内容到另个File -2
- upload file 2
- ARMLink scatter file(2)
- Struts 2 File Uploads
- command > file 2>&1
- 黑马程序员-FIle-2
- File类2
- java file 详解2
- File类(2)--递归
- ccs7.2 load file
- File
- file
- File
- file
- nf_hook_ops 钩子的注册
- 微软公司等数据结构+算法面试100题(第1-100题)全部出炉
- 重载debug版本的operator new和operator delete
- PHPUnit单元测试系列一 -- 单测的意义
- MongoDB安装
- DP file *2
- android34_android自定义权限
- http://poj.org/problem?id=3624
- jquery easyui datagrid 列自适应窗口宽度
- Solaris 10u10编译MMSLite客户端问题 -- cc: -W 选项带有未知程序 all
- Ext在同一页面创建多个表格时,不能指定ID属性
- IOS~bundle文件(一)
- 表单验证的完整应用
- 5-1 设计三角形周长和面积的类(含错误)