Little Charlie is a nice boy addicted to candies. He is even a subscriber to All Candies Magazine and was selected to participate in the International Candy Picking Contest.

In this contest a random number of boxes containing candies are disposed in M rows with N columns each (so, there are a total of M × N boxes). Each box has a number indicating how many candies it contains.

The contestant can pick a box (any one) and get all the candies it contains. But there is a catch (there is always a catch): when choosing a box, all the boxes from the rows immediately above and immediately below are emptied, as well as the box to the left and the box to the right of the chosen box. The contestant continues to pick a box until there are no candies left.

The figure bellow illustrates this, step by step. Each cell represents one box and the number of candies it contains. At each step, the chosen box is circled and the shaded cells represent the boxes that will be emptied. After eight steps the game is over and Charlie picked 10+9+8+3+7+6+10+1 = 54 candies.

For small values of M and N, Charlie can easily find the maximum number of candies he can pick, but when the numbers are really large he gets completely lost. Can you help Charlie maximize the number of candies he can pick?

Input

The input contains several test cases. The first line of a test case contains two positive integers M and N (1 ≤ M×N ≤ 105 ), separated by a single space, indicating the number of rows and columns respectively. Each of the following M lines contains N integers separated by single spaces, each representing the initial number of candies in the corresponding box. Each box will have initially at least 1 and at most 103 candies. The end of input is indicated by a line containing two zeroes separated by a single space

Output

For each test case in the input, your program must print a single line, containing a single value, the integer indicating the maximum number of candies that Charlie can pick

Sample Input

5 5

1 8 2 1 9

1 7 3 5 2

1 2 10 3 10

8 4 7 9 1

7 1 3 1 6

Sample Output

54

水dp

一开始觉得n m的范围很大,仔细一看才知道n*m<=100000。。。。

我们可以每一行进行一次dp得到这一行的最大值  构造数组 a  然后对a数组在进行一次dp.

横着dp和竖着dp的状态转移是一样的   dp[i]=max(dp[i-1],dp[i-2]+a[i])   即前i个数能得到的最大值

/* ***********************************************
Author :guanjun
Created Time :2016/10/7 12:08:19
File Name :la4212.cpp
************************************************ */
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
vector<int>v[];
int dp[];
int a[];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n,m,x;
while(cin>>n>>m){
if(n==&&m==)break;
for(int i=;i<=n;i++){
v[i].clear();
for(int j=;j<=m;j++){
scanf("%d",&x);
v[i].push_back(x);
}
}
//先横着dp 再竖着dp
cle(a);
for(int i=;i<=n;i++){
m=v[i].size();
cle(dp);
for(int j=;j<m;j++){
if(j<)dp[j]=max(dp[j-],v[i][j]);
else dp[j]=max(dp[j-]+v[i][j],dp[j-]);
}
//cout<<"hang "<<dp[m-1]<<endl;
a[i]=dp[m-];
}
//for(int i=1;i<=n;i++)cout<<a[i]<<endl;
cle(dp);
for(int i=;i<=n;i++){
if(i<=)dp[i]=max(dp[i-],a[i]);
else dp[i]=max(dp[i-]+a[i],dp[i-]);
}
if(n==)cout<<max(dp[],dp[])<<endl;
else cout<<dp[n]<<endl;
}
return ;
}

更新:2018.6.15

/* ***********************************************
Author :guanjunace@foxmail.com
Created Time :2018年06月15日 星期五 09时59分17秒
File Name :131.cpp
************************************************ */
#include <iostream>
#include <vector>
using namespace std; int solve(vector<int>& a) {
int len = a.size();
vector<int> dp(len, );
dp[] = a[];
for (int i = ; i < len; ++i) {
if (i >= ) dp[i] = max(dp[i-] + a[i], dp[i-]);
else dp[i] = max(a[i-], a[i]);
//cout << dp[i] << "-" << endl;
}
return dp[len - ];
} int main() {
int n, m, x;
while (cin >> n >> m && n && m) {
vector<int> v;
for (int i = ; i < n; ++i) {
vector<int> a;
for (int j = ; j < m; ++j) {
cin >> x;
a.push_back(x);
}
//cout << solve(a) << endl;
v.push_back(solve(a));
}
cout << solve(v) << endl;
}
return ;
}

UVALive 4212 - Candy的更多相关文章

  1. UVALive 5791 Candy's Candy 解题报告

    比赛总结 题目 题意: 有f种口味的糖果,现在要把每颗糖果分到一些packs里面去.packs分两种: flavored pack:只有一种口味. variety pack:每种口味都有. 求满足下列 ...

  2. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  3. [LeetCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  4. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  5. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  6. Leetcode Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  7. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  8. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  9. 【leetcode】Candy(hard) 自己做出来了 但别人的更好

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

随机推荐

  1. ERwin逻辑模型

    1.自动排序 Format>>Preferences>>Layout Entire Diagram CA ERwin

  2. discuz x3论坛搬家换虚拟主机完美使用教程 亲测可行 附操作步骤

    第一步:备份网站数据进入后台—站长—数据库—备份,数据备份类型选择“Discuz!和 UCenter数据”,备份成功以后,数据自动保存在data文件夹下. 第二步:网站文件下载 把整个网站文件打包(虚 ...

  3. 二叉查找树(Binary Search Tree)

    Date:2019-06-25 14:40:32 基本操作 注意:数据量较大时,插入建树的时间复杂度会很高,慎用! //查找 void Search(node *root, int x) { if(r ...

  4. 集合类(Collection和Map接口)简介

    集合分为Collection和Map,详细分类如下图所示: 以下是测试验证代码: //HashSet,无序(存取不一致).去重 Set set_H = new HashSet(); set_H.add ...

  5. Servlet的说明及使用案例

    Servlet的说明及使用案例 制作人:全心全意 Servle的基础介绍 Servlet结构体系 Servlet对象.ServletConfig对象与Serializable对象是接口对象,其中Ser ...

  6. CCF201409-2 画图 java(100分)

    试题编号: 201409-2 试题名称: 画图 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在一个定义了直角坐标系的纸上,画一个(x1,y1)到(x2,y2)的矩形指将横坐 ...

  7. 回车符号 ‘\r’ 的实际应用

    由于最近开始研究自动化测试 首先是自动定时去下载安装包,需要实时显示进度. 于是了解了进度条相关的方法. 作下记录. 区别 \r 表示将光标的位置回退到本行的开头位置 \n 表示光标从下一行的开头位置 ...

  8. 洛谷 2484 [SDOI2011]打地鼠

    [题解] n^6的做法很好想,然而这样复杂度不对.. 然后我们可以发现R和C可以分开求,这样复杂度降到了n^4. 使用树状数组可以把复杂度降到n^3logn,可以顺利通过. #include<c ...

  9. i2c中start和restart的区别

    有的硬件芯片提供了一个个寄存器,供我们很好的操作i2c,但是,在用的时候,我们是不知道他到地是怎么操作的,下边,我就探讨下i2c中的start和restart的区别. start是在scl是高电平的时 ...

  10. cocos2d 接 android sdk 的一个小坑 关于armbeabi 和 armbeabi-v7a

    cocos2d 接 android sdk 的时候,有些sdk会要求外链到某个工程中,而这个工程的lib文件夹里会包含armbeabi 和 armbeabi-v7a这两个文件夹,如果直接打包会闪退.只 ...