当前位置: 代码迷 >> 综合 >> P1005 [NOIP2007 提高组] 矩阵取数游戏(区间dp+__int128)
  详细解决方案

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

热度:23   发布时间:2024-01-12 15:06:41.0

传送门

思路不解释了:大佬的题解
看到这题__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_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);int StartTime = clock();
#endifcin >> 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_JUDGEprintf("Run_Time = %d ms\n", clock() - StartTime);
#endifreturn 0;
}

(还是太菜了 要多刷题)