求条

P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles

hahaha_123456 @ 2024-10-01 21:57:49

#include <bits/stdc++.h>
#define ll long long
#define vt vector
#define un unsigned
#define us unordered_set
#define um unordered_map
#define qq queue
#define st set
#define stk stack
#define inl inline

using namespace std;

int dp[1005][1005];
int a[1005][1005];

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            cin >> a[i][j];
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            if (j == 1)
            {
                dp[i][j] = dp[i - 1][j] + a[i][j];
                continue;
            }
            if (i == j)
            {
                dp[i][j] = dp[i - 1][j - 1] + a[i][j];
                continue;
            }
            else
            {
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + a[i][j];
            }
        }
    }
    int mx = 0;
    for (int i = 1; i <= n; i++)
    {
        mx = max(mx, dp[n][i]);
    }
    cout << mx << endl;
    return 0;
}

求条


by blackhzm @ 2024-10-01 22:02:34

铜球


by lsd110504 @ 2024-10-12 22:32:43

#include <bits/stdc++.h>
#define ll long long
#define vt vector
#define un unsigned
#define us unordered_set
#define um unordered_map
#define qq queue
#define st set
#define stk stack
#define inl inline

using namespace std;

int dp[1005][1005];
int a[1005][1005];

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            cin >> a[i][j];
        }
    }
    for (int i = n-1; i >=1; i--)
    {
        for (int j = 1; j <= i; j++)
        {
           a[i][j]+=max(a[i+1][j],a[i+1][j+1]);
        }
    }
    cout << a[1][1] << endl;
    return 0;
}

要用倒推法

就是从最后一行向上求最大值,DP【】可以用原数组代替

虽然来晚了但是求关


by lsd110504 @ 2024-10-12 22:33:29

@huangzhaoming,@hahaha_123456


by lsd110504 @ 2024-10-12 22:34:01

@huangzhaoming @hahaha_123456


by hahaha_123456 @ 2024-10-13 13:25:19

我好像重新交上去就对了


|