Gym-102448 dp问题

K - Kongey Donk

                    Gym - 102448K 

The monkey Kongey Donk loves to eat bananas, but he is always really tired. Kongey lives in a region that has nn banana trees lined in a row, and he just woke up over a platform from which he can jump to the top of any of the banana trees in the region. The banana tree in nlogonia(region where Kongey lives) have a peculiar characteristic in which there are only bananas in fixed points of the tree with distance of 1 meter between them. Kongey needs to go to the floor(the lowest position of the tree), but in his way down he wants to take as much bananas as possible.
Kongey starts his journey from the platform and he can jump to the top of any tree. When he is in a tree, he can jump to any adjacent tree or jump to change his position in the tree he is now. But, as he is really tired, when performing a jump he will always go to a position below the one he was before.
Before he starts his journey, Kongey asks what is the maximum number of bananas he can take considering he can carry an unlimited number of them.

    Input
    The first line of the input consists of 22 numbers nn and hh (0<n,h≤2∗1050<n,h≤2∗105, 0<n∗h≤1060<n∗h≤106), the number of banana trees and the height of each of them, respectively.

Each of the following nn lines describes a banana tree.
The ii-th of them contains hh non negative integer numbers describing how many bananas there are in each position of the ii-th tree from top to bottom.
The number of bananas in each position is always less than or equal to 106106.

    Output
    The output consists of a single integer number describing the maximum number of bananas Kongey can take.

    Examples

Input

3 3
1 5 5
9 0 0
15 2 1

Output

20

Input

5 6
1 100 2 8 9 8
10 55 30 2 2 2
30 10 200 10 3 100
50 0 1 2 3 9
1 130 9 29 3 40

Output

398

    Note
    For all 0<i<n0<i<n, the ii-th banana tree and the (i−1)(i−1)-th banana tree are adjacent.

In the first sample, the best path goes through trees: 3, 2, 1 getting respectively 15, 0, 5 bananas.

using namespace std;
#include<bits/stdc++.h>
#define int long long
vector<int> dp[200005];
int n,h;
signed main()
{
     cin>>n>>h;
     for(int i=0;i<n;i++)
     {
         for(int j=0;j<h;j++)
         {
             int x;
             scanf("%lld",&x);
             dp[i].push_back(x);
         }
     }
      for(int j=1;j<h;j++)
      {
          for(int i=0;i<n;i++)
          {
              int a=0,b=0;
              if(i>=1) a=dp[i-1][j-1];
              if(i+1<n) b=dp[i+1][j-1];
              int c=dp[i][j-1];
              dp[i][j]+=max(a,max(b,c));
         }
      }
      int ans = 0;
      for(int i=0;i<n;i++)
      {
          ans=max(ans,dp[i][h-1]);
      }
      printf("%lld\n",ans);
    return 0;
}

总结:个人目前vector和map用的不是很熟练,这算是道简单题吧。
就是要注意一下那个边界问题

发布了9 篇原创文章 · 获赞 0 · 访问量 172

猜你喜欢

转载自blog.csdn.net/luoxutimberjack/article/details/104220484
今日推荐