dp萌新,求问关于dp初始化
查看原帖
dp萌新,求问关于dp初始化
165533
浮梦CalcuLus楼主2022/2/27 22:52
const int MAXN = 105;
int dp[MAXN][MAXN];
tuple<int, int, int> a[MAXN];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int d, g;
    cin >> d >> g;
    for (int i = 1; i <= g; i++)
    {
        int t, f, h;
        cin >> t >> f >> h;
        a[i] = make_tuple(t, f, h);
    }
    memset(dp, ~0x3f, sizeof(dp));
    sort(a + 1, a + g + 1);
    a[0] = make_tuple(0, 0, 0);
    dp[0][0] = 10;
    int ans2 = -1;
    for (int i = 1; i <= g; i++)
    {
        int f = get<1>(a[i]), h = get<2>(a[i]), t = get<0>(a[i]), tt = get<0>(a[i - 1]), tdis = t - tt;
        for (int j = d; j >= 0; j--)
        {
            if (dp[i - 1][j] < tdis)
                continue;
            if (j + h >= d)
            {
                cout << t << endl;
                return 0;
            }
            dp[i][j + h] = max(dp[i][j + h], dp[i - 1][j] - tdis);
            dp[i][j] = max(dp[i][j], dp[i - 1][j] + f - tdis);
        }
        ans2 = max(ans2, dp[i][0] + t);
    }
    cout << ans2 << endl;
    return 0;
}
 

这题初始化原本我是用

memset(dp, 0, sizeof(dp))

只能的45pts

随即改成

memset(dp, -1, sizeof(dp))

得到91pts

看了相似的题解 改成

memset(dp, ~0x3f, sizeof(dp))

才能满分 球球各位巨佬解答一下

2022/2/27 22:52
加载中...