博主主页:Yu·仙笙
专栏地址:洛谷千题详解
目录
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n×m 的矩阵,矩阵中的每个元素 ai,j 均为非负整数。游戏规则如下:
帅帅想请你帮忙写一个程序,对于任意矩阵,可以求出取数后的最大得分。
-------------------------------------------------------------------------------------------------------------------------------
输入文件包括 n+1 行:
第一行为两个用空格隔开的整数 n 和 m。
第 2∼n+1 行为 n×m 矩阵,其中每行有 m 个用单个空格隔开的非负整数。
-------------------------------------------------------------------------------------------------------------------------------
输出文件仅包含 1 行,为一个整数,即输入矩阵取数后的最大得分。
-------------------------------------------------------------------------------------------------------------------------------
输入 #1
2 3 1 2 3 3 4 2
输出 #1
82
-------------------------------------------------------------------------------------------------------------------------------
求n行最大得分和,每一行取数又不会影响到其他行,那么只要确保每一行得分最大,管好自家孩子就行了。(这个在动规中叫最优子结构)
每次取数是在边缘取,那么每次取数完剩下来的元素一定是在一个完整的一个区间中,又是求最优解,区间DP应运而生。
-------------------------------------------------------------------------------------------------------------------------------
- #include
- #include
- #include
- #include
- #include
-
- using namespace std;
-
- const int MAXN = 85, Mod = 10000; //高精四位压缩大法好
- int n, m;
- int ar[MAXN];
-
- struct HP {
- int p[505], len;
- HP() {
- 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[MAXN][MAXN], base[MAXN], ans;
-
- HP operator + (const HP &a, const HP &b) {
- HP 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;
- } //高精+高精
-
- HP operator * (const HP &a, const int &b) {
- HP 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;
- } //高精*单精
-
- HP max(const HP &a, const HP &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 BaseTwo() {
- base[0].p[1] = 1, base[0].len = 1;
- for (int i = 1; i <= m + 2; i++){ //这里是m! m! m! 我TM写成n调了n年...
- base[i] = base[i - 1] * 2;
- }
- } //预处理出2的幂
-
- int main(void) {
- scanf("%d%d", &n, &m);
- BaseTwo();
- while (n--) {
- memset(f, 0, sizeof f);
- for (int i = 1; i <= m; i++)
- scanf("%d", &ar[i]);
- for (int i = 1; i <= m; i++)
- for (int j = m; j >= i; j--) { //因为终值是小区间,DP自然就从大区间开始
- f[i][j] = max(f[i][j], f[i - 1][j] + base[m - j + i - 1] * ar[i - 1]);
- f[i][j] = max(f[i][j], f[i][j + 1] + base[m - j + i - 1] * ar[j + 1]);
- } //用结构体重载运算符写起来比较自然
- HP Max;
- for (int i = 1; i <= m; i++)
- Max = max(Max, f[i][i] + base[m] * ar[i]);
- ans = ans + Max; //记录到总答案中
- }
- ans.print(); //输出
- return 0;
- }
-------------------------------------------------------------------------------------------------------------------------------
- import java.math.BigInteger;
- import java.util.Scanner;
-
- public class Main {
-
- static int [] mp = new int[100];
- static BigInteger [] pow = new BigInteger[160];
-
- @SuppressWarnings("resource")
- public static void main(String[] args) {
- pow[0] = BigInteger.ONE;
- pow[1] = BigInteger.valueOf(2);
- for(int i = 2; i < 160; i++) {
- pow[i] = pow[1].pow(i);
- }
- Scanner cin = new Scanner(System.in);
- BigInteger ans = BigInteger.ZERO;
- int n=cin.nextInt(), m=cin.nextInt();
- for(int t = 1; t <= n; t++) {
- BigInteger [][] f = new BigInteger[100][100];
- BigInteger max = BigInteger.ZERO;
- for(int j = 1; j <= m; j++)
- mp[j]=cin.nextInt();
- for(int i = 0; i < 100; i++)
- for(int j = 0; j < 100; j++)
- f[i][j] = BigInteger.ZERO;
- for(int i = 1; i <= m; i++)
- for(int j = m; j > 0; j--) {
- f[i][j] = f[i][j].max(f[i - 1][j].add(pow[m - j + i - 1].multiply(BigInteger.valueOf(mp[i - 1]))));
- f[i][j] = f[i][j].max(f[i][j + 1].add(pow[m - j + i - 1].multiply(BigInteger.valueOf(mp[j + 1]))));
- }
- for(int i = 1; i <= m; i++)
- max = max.max(f[i][i].add(pow[m].multiply(BigInteger.valueOf(mp[i]))));
- ans = ans.add(max);
- }
- System.out.println(ans);
- }
- }
-------------------------------------------------------------------------------------------------------------------------------
- matrix=[]
-
- n,m=input().split()
- n,m=int(n),int(m)
-
- for i in range(n):
- row=input().split()
- row=[0]+[int(count)for count in row]+[0]
- #print(row)
- # 把列表塞到列表里形成二维列表
- matrix.append(row)
-
- a=[1]#a[0]=1初始化为1
- #求到2^m
- for i in range(m):
- a.append(a[i]*2)
- #print(a)
-
- ans=0
-
- for i in range(n):
- row=matrix[i]
- w=1
- #分配一个大小为m+2 * m+2的二维数组
- dp=[[0]*(m+2)]*(m+2)
- #print(dp)
- for st in range(1,m+1):
- for ed in range(m,0,-1):
- if ed
- continue
- dp[st][ed]=max(dp[st-1][ed]+row[st-1]*a[m-ed+st-1],dp[st][ed+1]+row[ed+1]*a[m-ed+st-1])
-
- ans+=max([dp[i][i]+row[i]*a[m]for i in range(1,m+1)])
-
- print(ans)
-------------------------------------------------------------------------------------------------------------------------------