CF 358 D Dima and Hares(dp)
2015-08-28 11:05
246 查看
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
output
input
output
input
output
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
/* 思路: dp[i][0][0] 代表吃这个的时候左右都没有吃 dp[i][1][0] 代表吃这个的时候 左边已经吃了右边没吃 dp[i][1][1] 代表吃这个的时候 右边已经吃了左边没吃 dp[i][2][0] 代表吃这个的时候,左右都吃了 */ #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #include<set> #include<map> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define bug printf("hihi\n") #define mod 1000000007 #define eps 1e-8 typedef __int64 ll; using namespace std; #define INF 0x3f3f3f3f #define N 3005 #define mod 1000000007 int dp [3][2]; int a [3]; int n; int main() { int i,j; while(~scanf("%d",&n)) { for(i=0;i<3;i++) for(j=1;j<=n;j++) scanf("%d",&a[j][i]); dp[1][0][0]=a[1][0]; dp[1][1][0]=-INF; dp[1][1][1]=a[1][1]; dp[1][2][0]=-INF; for(i=2;i<=n;i++) { dp[i][0][0]=max(dp[i-1][2][0]+a[i][0],dp[i-1][1][1]+a[i][0]); dp[i][1][0]=max(dp[i-1][0][0]+a[i][1],dp[i-1][1][0]+a[i][1]); dp[i][1][1]=max(dp[i-1][1][1]+a[i][1],dp[i-1][2][0]+a[i][1]); dp[i][2][0]=max(dp[i-1][1][0]+a[i][2],dp[i-1][0][0]+a[i][2]); } printf("%d\n",max(dp [0][0],dp [1][0])); } return 0; }
相关文章推荐
- 关于VBS连接MySQL与连接Excel
- 百度pc wap适配
- [LeetCode] 9 - Palindrome Number
- android中activity,window,view之间的关系
- js实现显示当前状态的导航效果代码
- 完全背包——方案个数 UVA11137 Ingenuous Cubrency
- Maven介绍
- C语言中操作密码文件的一些函数总结
- 服务器tcp连接timewait过多优化及详细分析
- 说说微信支付那点事
- 图像分割之(一)概述
- Lua中的九九乘法表以及字符串拼接
- c++中的rand函数
- Mac环境下svn的使用
- 人生职业规划
- DNS从服务器迁移(基于windows为主,linux为从实现同步)
- 类模板Template
- ActiveMQ 即时通讯服务 浅析
- 网络编程socket accept函数的理解
- Android性能优化之使用SparseArray代替HashMap