hdu2845——Beans

Beans

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3011    Accepted Submission(s): 1450

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

Source

2009 Multi-University Training Contest 4 - Host by HDU

Recommend

先每行求一下最大值,dp1[i] = max(dp[i - 1], dp[i - 2] + cow[i]);

再对这些最大值求一个最大值

dp2[i] = max(dp2[i - 1], dp2[i - 2] + col[i]);

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
int cow[200010];
int dp[200010];
int col[200010];
int dp2[200010];

int main()
{
	int n, m;
	while (~scanf("%d%d", &n, &m))
	{
		memset (dp2, 0, sizeof(dp2));
		for (int i = 1; i <= n; ++i)
		{
			memset (dp, 0, sizeof(dp));
			for (int j = 1; j <= m; ++j)
			{
				scanf("%d", &cow[j]);
			}
			dp[1] = cow[1];
			for (int j = 2; j <= m; ++j)
			{
				dp[j] = max(dp[j - 1], dp[j - 2] + cow[j]);
			}
			col[i] = dp[m];
		}
		dp2[1] = col[1];
		for (int i = 2; i <= n; ++i)
		{
			dp2[i] = max(dp2[i - 1], dp2[i - 2] + col[i]);
		}
		printf("%d\n", dp2[n]);
	}
	return 0;
}
时间: 2024-11-11 09:27:39

hdu2845——Beans的相关文章

Spring之 Auto-Wiring All Beans of Compatible Type

Auto-Wiring All Beans of Compatible Type @Autowired 注解按类型(type)依赖入住的时候,可以把类型兼容的所有类注入到数组.链表.map等集合数据结构中.如:mybatis中TypeHandler为例: package com.doctor.practice01; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.Resul

XML中&lt;beans&gt;属性

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring

springsecurity启动出现org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: You must use a 3.0 schema with Spring Security 3.0.

在换了spring-security的jar包以后启动出现org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: You must use a 3.0 schema with Spring Security 3.0.Please update your schema declarations to the 3.0.3 schema (spring-securi

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;App&#39; is defined

工具:Eclipse mars 环境:jdk1.8 说明:这是在学习Spring Task时遇到的一个bug,代码如下: 定时任务类: package com.task.test; import java.util.Date; import org.springframework.stereotype.Component; @Component public class App { public void execute1(){ System.out.printf("Task: %s, Curr

spring beans源码解读

spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类.org.springframework.beans.annotation, 支持包,提供对java 5注解处理bean样式的支持.org.springframework.beans.factory, 实现spring轻量级IoC容器的核心包.org.springframework.beans.factory.access, 定位和获取bean工程的辅助工具类

异常:Struts:org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find BasicDataSource

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [beans.xml]; nested exception is java.lang.ClassNotFoundExcepti

关于命名空间beans错误

命名空间自动生成xmlns:beans="http://www.springframework.org/schema/beans"  测试正常 加载tomcat会报出   cvc-elt.1: 找不到元素 'beans' 的声明    的异常 修改成      xmlns="http://www.springframework.org/schema/beans"

源码跟读,Spring是如何解析和加载xml中配置的beans

Spring版本基于: 跟踪代码源码基于: https://github.com/deng-cc/KeepLearning commit id:c009ce47bd19e1faf9e07f12086cd440b7799a63 1.配置启动Spring所需的监听器 web.xml中配置监听器 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-cla

Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException:

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoSupport': Injection of resou