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

相关文章

Spring中管理bean对象的方式(专业级说明)

《Spring中管理bean对象的方式(专业级说明)》在Spring框架中,Bean的管理是核心功能,主要通过IoC(控制反转)容器实现,下面给大家介绍Spring中管理bean对象的方式,感兴趣的朋... 目录1.Bean的声明与注册1.1 基于XML配置1.2 基于注解(主流方式)1.3 基于Java

利用Python实现时间序列动量策略

《利用Python实现时间序列动量策略》时间序列动量策略作为量化交易领域中最为持久且被深入研究的策略类型之一,其核心理念相对简明:对于显示上升趋势的资产建立多头头寸,对于呈现下降趋势的资产建立空头头寸... 目录引言传统策略面临的风险管理挑战波动率调整机制:实现风险标准化策略实施的技术细节波动率调整的战略价

PostgreSQL 序列(Sequence) 与 Oracle 序列对比差异分析

《PostgreSQL序列(Sequence)与Oracle序列对比差异分析》PostgreSQL和Oracle都提供了序列(Sequence)功能,但在实现细节和使用方式上存在一些重要差异,... 目录PostgreSQL 序列(Sequence) 与 oracle 序列对比一 基本语法对比1.1 创建序

SpringIOC容器Bean初始化和销毁回调方式

《SpringIOC容器Bean初始化和销毁回调方式》:本文主要介绍SpringIOC容器Bean初始化和销毁回调方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录前言1.@Bean指定初始化和销毁方法2.实现接口3.使用jsR250总结前言Spring Bea

Spring实现Bean的初始化和销毁的方式

《Spring实现Bean的初始化和销毁的方式》:本文主要介绍Spring实现Bean的初始化和销毁的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Bean的初始化二、Bean的销毁总结在前面的章节当中介绍完毕了ApplicationContext,也就

浅析如何使用xstream实现javaBean与xml互转

《浅析如何使用xstream实现javaBean与xml互转》XStream是一个用于将Java对象与XML之间进行转换的库,它非常简单易用,下面将详细介绍如何使用XStream实现JavaBean与... 目录1. 引入依赖2. 定义 JavaBean3. JavaBean 转 XML4. XML 转 J

Spring 基于XML配置 bean管理 Bean-IOC的方法

《Spring基于XML配置bean管理Bean-IOC的方法》:本文主要介绍Spring基于XML配置bean管理Bean-IOC的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录一. spring学习的核心内容二. 基于 XML 配置 bean1. 通过类型来获取 bean2. 通过

C++从序列容器中删除元素的四种方法

《C++从序列容器中删除元素的四种方法》删除元素的方法在序列容器和关联容器之间是非常不同的,在序列容器中,vector和string是最常用的,但这里也会介绍deque和list以供全面了解,尽管在一... 目录一、简介二、移除给定位置的元素三、移除与某个值相等的元素3.1、序列容器vector、deque

Spring 中使用反射创建 Bean 实例的几种方式

《Spring中使用反射创建Bean实例的几种方式》文章介绍了在Spring框架中如何使用反射来创建Bean实例,包括使用Class.newInstance()、Constructor.newI... 目录1. 使用 Class.newInstance() (仅限无参构造函数):2. 使用 Construc

Springboot控制反转与Bean对象的方法

《Springboot控制反转与Bean对象的方法》文章介绍了SpringBoot中的控制反转(IoC)概念,描述了IoC容器如何管理Bean的生命周期和依赖关系,它详细讲解了Bean的注册过程,包括... 目录1 控制反转1.1 什么是控制反转1.2 SpringBoot中的控制反转2 Ioc容器对Bea