洛谷 P1005 矩阵取数游戏

题目:
一道特别简单的题,没啥好说,就更新状态贪心就行了,暴力就能AC系列,不过需要考虑一个高精度的问题,第一遍写的简单程序只过了20%,后面考虑了一下高精度就过了:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int max_n = 85, mod = 10000;  
int n, m;
int a2[max_n];
struct TREE {//结构体简化元素
	int p[505], len;
	TREE() {
		memset(p, 0, sizeof p);
		len = 0;
	} 
	void print() {
		printf("%d", p[len]);  
        for (int i = len - 1; i > 0; i--) {  
            if (p[i] == 0) {
				printf("0000"); 
				continue;
			}
            for (int k = 10; k * p[i] < mod; k *= 10) 
				printf("0");
            printf("%d", p[i]);
        }
	}  
} f[max_n][max_n],a1[max_n], ans;

TREE operator + (const TREE &a, const TREE &b) {
	TREE c; c.len = max(a.len, b.len); int x = 0;
	for (int i = 1; i <= c.len; i++) {
		c.p[i] = a.p[i] + b.p[i] + x;
		x = c.p[i] / mod;
		c.p[i] %= mod;
	}
	if (x > 0)
		c.p[++c.len] = x;
	return c;
} 
TREE operator * (const TREE &a, const int &b) {
	TREE c; c.len = a.len; int x = 0;
	for (int i = 1; i <= c.len; i++) {
		c.p[i] = a.p[i] * b + x;
		x = c.p[i] / mod;
		c.p[i] %= mod;
	}
	while (x > 0)
		c.p[++c.len] = x % mod, x /= mod;
	return c;
}
TREE max(const TREE &a, const TREE &b) {
	if (a.len > b.len)
		return a;
	else if (a.len < b.len)
		return b;
	for (int i = a.len; i > 0; i--)
		if (a.p[i] > b.p[i])
			return a;
		else if (a.p[i] < b.p[i])
			return b;
	return a;
}
void search() {
	a1[0].p[1] = 1,a1[0].len = 1;
	for (int i = 1; i <= m + 2; i++){  
		a1[i] =a1[i - 1] * 2;
	}
}
int main(void) {
	scanf("%d%d", &n, &m);
	search();
	while (n--) {
		memset(f, 0, sizeof f);
		for (int i = 1; i <= m; i++)
			scanf("%d", &a2[i]);
		for (int i = 1; i <= m; i++)
			for (int j = m; j >= i; j--) { 
				f[i][j] = max(f[i][j], f[i - 1][j] +a1[m - j + i - 1] * a2[i - 1]); 
				f[i][j] = max(f[i][j], f[i][j + 1] +a1[m - j + i - 1] * a2[j + 1]);
			}
		TREE ma;
		for (int i = 1; i <= m; i++)
			ma = max(ma, f[i][i] +a1[m] * a2[i]);
		ans = ans + ma;  
	}
	ans.print(); 
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值