P1005 [NOIP2007 提高组] 矩阵取数游戏(区间dp+__int128)

传送门

思路不解释了:大佬的题解
看到这题__int128也能过,想拿__int128试试。
遇到的小坑:

f[i][j] = max(f[i - 1][j] + (1 << (m - j + i - 1)) * d[i - 1], f[i][j]);
f[i][j] = max(f[i][j + 1] + (1 << (m - j + i - 1)) * d[j + 1], f[i][j]);

上面的写法是错的(不知道怎么解释

f[i][j] = max(f[i - 1][j] + (d[i - 1] << (m - j + i - 1)), f[i][j]);
f[i][j] = max(f[i][j + 1] + (d[j + 1] << (m - j + i - 1)), f[i][j]);

这样才是正确的写法

#include <bits/stdc++.h>

using namespace std;
//-----pre_def----
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
#define fir(i, a, b) for (int i = a; i <= b; i++)
#define rif(i, a, b) for (int i = a; i >= b; i--)
#define init_h memset(h, -1, sizeof h), idx = 0;
//---------------
const int N = 85;
int n, m;
__int128 f[N][N];
__int128 d[N];
void output(__int128 x)
{
    if (x > 9)
        output(x / 10);
    putchar(x % 10 + '0');
}
__int128 read()
{
    __int128 x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    int StartTime = clock();
#endif
    cin >> n >> m;
    __int128 ans = 0;
    while (n--)
    {
        memset(f, 0, sizeof f);
        fir(i, 1, m)
            d[i] = read();
        fir(i, 1, m)
            rif(j, m, i)
        {
            f[i][j] = max(f[i - 1][j] + (d[i - 1] << (m - j + i - 1)), f[i][j]);
            f[i][j] = max(f[i][j + 1] + (d[j + 1] << (m - j + i - 1)), f[i][j]);
        }

        __int128 maxn = 0;
        fir(i, 1, m)
            maxn = max(maxn, (f[i][i] + (d[i] << m)));
        ans += maxn;
    }
    output(ans);
#ifndef ONLINE_JUDGE
    printf("Run_Time = %d ms\n", clock() - StartTime);
#endif
    return 0;
}

(还是太菜了 要多刷题)

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页