Codeforces Round #208 (Div. 2)D. Dima and Hares

本文深入探讨了如何使用动态规划和贪心策略来解决Dima为兔子喂食以最大化总快乐值的问题。通过分析兔子之间的依赖关系和喂食顺序的影响,我们构建了一个高效的算法,旨在为每个兔子选择最优的喂食时机,从而实现整体最大快乐值。详细介绍了算法的步骤、边界条件和关键数学公式,并提供了源代码以供参考。

http://codeforces.com/contest/358/problem/D

D. Dima and Hares
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.

Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?

Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.

Help Inna maximize the total joy the hares radiate. :)

Input

The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.

Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.

Output

In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.

Sample test(s)
input
4
1 2 3 4
4 3 2 1
0 1 1 0
output
13
input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
output
44
input
3
1 1 1
1 2 1
1 1 1
output
4

分析:

注意此题的题意理解,题目是要找到一个feed 的顺序,也就是每个hare只有feed之后才有收益。

貌似第一眼看起来,排列是指数级别的复杂度,这道题有一个非常明显的标志,每个hare只和相邻的hare有关系,由此复杂度剧减。

而实际上,也无需求出真正的feed序列,只需要计算出每个hare和相邻hare的feed的先后顺序就可以!

动态规划:

dp[i][0]表示hare i 比 i-1 后feed 情况下, 前i-1 个 hare 的最大收益

dp[i][1]表示hare i 比 i-1 先feed 情况下, 前i-1 个 hare 的最大收益


cst[k][i]表示输入矩阵的第k行,对应的收益值,具体背景参考题目描述。

转移方程: 分别考虑i-1比i-2是先feed还是后feed

dp[i][0]= max(dp[i-1][0]+cst[1][i-1],dp[i-1][1]+cst[0][i-1]);

dp[i][1]= max(dp[i-1][0]+cst[2][i-1],dp[i-1][1]+cst[1][i-1]);

边界条件:(边界条件可以放入转移方程中,参考代码)

dp[1][0]=cst[0][0]; 1比0后feed,考虑0的收益。
dp[1][1]=cst[1][0]; 1比0先feed,考虑0的收益。


源代码:

#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<cmath>
using namespace std;
#define N 3005
#define INF 1e8
int dp[N][2];
int cst[3][N];
//dp[i][0]表示i比i-1后feed,前i-1的最大收益
int main()
{
	int n;
	scanf("%d",&n);
	for(int j=0;j<3;j++)
	for(int i=0;i<n;i++)
	scanf("%d",&cst[j][i]);
dp[0][0]=-INF;
dp[0][1]=0;
//dp[1][0]=cst[0][0];
//dp[1][1]=cst[1][0];
for(int i=1;i<=n;i++){
	dp[i][0]= max(dp[i-1][0]+cst[1][i-1],dp[i-1][1]+cst[0][i-1]);
	dp[i][1]= max(dp[i-1][0]+cst[2][i-1],dp[i-1][1]+cst[1][i-1]);
}
printf("%d\n",dp[n][0]);
//system("pause");
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值