【AtCoder-3881】解题报告(简单DP)
原始题目
C - Candies
Time limit : 2sec
Memory limit : 256MB
Score : 300 points
Problem Statement
We have a 2×N grid. We will denote the square at the i-th row and j-th column (1≤i≤2, 1≤j≤N) as (i,j).
You are initially in the top-left square, (1,1). You will travel to the bottom-right square, (2,N), by repeatedly moving right or down.
The square (i,j) contains Ai,j candies. You will collect all the candies you visit during the travel. The top-left and bottom-right squares also contain candies, and you will also collect them.
At most how many candies can you collect when you choose the best way to travel? ### Constraints - 1≤N≤100 - 1≤Ai,j≤100 (1≤i≤2, 1≤j≤N)
### Input
Input is given from Standard Input in the following format:
N
A1,1 A1,2 … A1,N
A2,1 A2,2 … A2,N
### Output
Print the maximum number of candies that can be collected.
### Sample Input 1
5
3 2 2 4 1
1 2 2 2 1
### Sample Output 1
14
The number of collected candies will be maximized when you:
move right three times, then move down once, then move right once.
### Sample Input 2
4
1 1 1 1
1 1 1 1
### Sample Output 2
5
You will always collect the same number of candies, regardless of how you travel.
### Sample Input 3
7
3 3 4 5 4 5 3
5 3 4 4 2 3 2
### Sample Output 3
29
### Sample Input 4
1
2
3
### Sample Output 4
5
# 题目大意
2*N个格子,每个格子里均含有不同数量的蜡烛。每一次只能向右或者向下走,问从(1,1)走到(2,N)最多能收集多少根蜡烛(包括首尾)。
# 解题思路
入门DP,简化成2*N了,状态转移方程:dp[i][j]=max(dp[i-1][j],dp[i][j-1])+a[i][j]
# 解题代码
1 |
|
# 收获与反思
- 第一次小比赛实战应用dp,看了看大神们也都是dp秒出说明思路是正确的。能写好状态转移方程就行。
- 2018.8.23更新 代码优化,利用dp外围空白。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!