洛谷P1005 矩阵取数游戏(附__int128版本)(区间dp入门)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<map>
#include<string>
#include<vector>
#include<set>
#include<bitset>
#include<algorithm>
#include<ctime>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ull unsigned long long
#define endl '\n'
#define clr(a, b) memset(a, b, sizeof(a))
#define lowbit(x) x & -x
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define PB push_back
#define POP pop_back
#define max_ll 9223372036854775807
const double eps = 1e-10;
const double pi = acos(-1);
const int maxn = 100 + 10;
const int maxm = (maxn<<5) + 5;
const ll mod = 1e9 + 7;
const int hash_mod = 19260817;
int n, m;
__int128 dp[maxn][maxn][maxn], a[maxn][maxn];
__int128 ans;
inline __int128 Max(__int128 x, __int128 y){return (x > y) ? x : y;}
inline __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;
}
inline void write(__int128 x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}
int main()
{
    scanf("%d %d", &n, &m);
    ans = 0; 
    for(int i = 1 ; i <= n ; ++ i){
        for(int j = 1 ; j <= m ; ++ j){
            a[i][j] = read();
        }
    }
    for(int i = 1 ; i <= n ; ++ i)
        for(int j = 1 ; j <= m ; ++ j)
            dp[i][j][j] = a[i][j] * 2;
    for(int i = 1 ; i <= n ; ++ i){
        for(int len = 2 ; len <= m ; ++ len){
            for(int l = 1 ; l + len - 1 <= m ; ++ l){
                int r = l + len - 1;
                dp[i][l][r] = Max(dp[i][l][r], Max(dp[i][l][r-1] * 2 + a[i][r] * 2, dp[i][l+1][r] * 2 + a[i][l] * 2));
            }
        }
        ans += dp[i][1][m];
    }
    write(ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值