NYOJ 题目17 单调递增最长子序列 (DP) hdu 题目2845 Bean

2024-06-03 19:48

本文主要是介绍NYOJ 题目17 单调递增最长子序列 (DP) hdu 题目2845 Bean,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

吃土豆

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述
Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect the qualities, but everyone must obey by the following rules: if you eat the bean at the coordinate(x, y), you can’t eat the beans anyway at the coordinates listed (if exiting): (x, y-1), (x, y+1), and the both rows whose abscissas are x-1 and x+1.


Now, how much qualities can you eat and then get ?
输入
There are a few cases. In each case, there are two integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of bean isn't beyond 1000, and 1<=M,N<=500.
输出
For each case, you just output the MAX qualities you can eat and then get.
样例输入
4 6
11 0 7 5 13 9
78 4 81 6 22 4
1 40 9 34 16 10
11 22 0 33 39 6
样例输出
242

二次dp,

1.首先单独对每行的数据进行DP处理,得到一个最大值;

2,.每行的最大值又组成一个新的数组,再次dp求最大值



 
#define N 505
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int MAX(int x,int y){return x>y?x:y;
}
int main()
{int m,n,i,j,k,max,a[N][N],dp[N][2],f[N]; while(scanf("%d%d",&m,&n)!=EOF){for(i=0;i<m;i++) for(j=0;j<n;j++)scanf("%d",&a[i][j]);for(i=0;i<m;i++) {dp[0][0]=0; dp[0][1]=a[i][0];		for(j=1;j<n;j++){dp[j][0] = MAX(dp[j-1][0],dp[j-1][1]);dp[j][1] = dp[j-1][0] + a[i][j];}		f[i] = MAX(dp[n-1][0],dp[n-1][1]);}dp[0][0] = 0;  dp[0][1] = f[0];for(j=1;j<m;j++){		dp[j][0] = MAX(dp[j-1][0],dp[j-1][1]);dp[j][1] = dp[j-1][0] + f[j];}				printf("%d\n",MAX(dp[m-1][0],dp[m-1][1]));}return 0;
}         


HDU 题目

Beans

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2144    Accepted Submission(s): 1081


Problem Description
Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect the qualities, but everyone must obey by the following rules: if you eat the bean at the coordinate(x, y), you can’t eat the beans anyway at the coordinates listed (if exiting): (x, y-1), (x, y+1), and the both rows whose abscissas are x-1 and x+1.


Now, how much qualities can you eat and then get ?

Input
There are a few cases. In each case, there are two integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of bean isn't beyond 1000, and 1<=M*N<=200000.

Output
For each case, you just output the MAX qualities you can eat and then get.

Sample Input
  
4 6 11 0 7 5 13 9 78 4 81 6 22 4 1 40 9 34 16 10 11 22 0 33 39 6

Sample Output
  
242

代码要严谨,直接上面的代码通不过;

改进代码:


#define N 200005
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int MAX(int x,int y){return x>y?x:y;
}
int a[N],dp[N][2],f[N]; 
int main()
{int m,n,i,j,k,max;        while(scanf("%d%d",&m,&n)!=EOF){    for(i=0;i<m;i++) {for(j=0;j<n;j++)scanf("%d",&a[j]);                            dp[0][0]=0; dp[0][1]=a[0];    for(j=1;j<n;j++){dp[j][0] = MAX(dp[j-1][0],dp[j-1][1]);dp[j][1] = dp[j-1][0] + a[j];}        f[i] = MAX(dp[n-1][0],dp[n-1][1]);                                                        }dp[0][0] = 0;  dp[0][1] = f[0];for(j=1;j<m;j++){        dp[j][0] = MAX(dp[j-1][0],dp[j-1][1]);dp[j][1] = dp[j-1][0] + f[j];}    printf("%d\n",MAX(dp[m-1][0],dp[m-1][1]));}return 0;
} 



这篇关于NYOJ 题目17 单调递增最长子序列 (DP) hdu 题目2845 Bean的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1027947

相关文章

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

关于最长递增子序列问题概述

《关于最长递增子序列问题概述》本文详细介绍了最长递增子序列问题的定义及两种优化解法:贪心+二分查找和动态规划+状态压缩,贪心+二分查找时间复杂度为O(nlogn),通过维护一个有序的“尾巴”数组来高效... 一、最长递增子序列问题概述1. 问题定义给定一个整数序列,例如 nums = [10, 9, 2

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

SpringBoot项目删除Bean或者不加载Bean的问题解决

《SpringBoot项目删除Bean或者不加载Bean的问题解决》文章介绍了在SpringBoot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实... 使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。@C

Spring中Bean有关NullPointerException异常的原因分析

《Spring中Bean有关NullPointerException异常的原因分析》在Spring中使用@Autowired注解注入的bean不能在静态上下文中访问,否则会导致NullPointerE... 目录Spring中Bean有关NullPointerException异常的原因问题描述解决方案总结

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm

hdu4865(概率DP)

题意:已知前一天和今天的天气概率,某天的天气概率和叶子的潮湿程度的概率,n天叶子的湿度,求n天最有可能的天气情况。 思路:概率DP,dp[i][j]表示第i天天气为j的概率,状态转移如下:dp[i][j] = max(dp[i][j, dp[i-1][k]*table2[k][j]*table1[j][col] )  代码如下: #include <stdio.h>#include