zoukankan      html  css  js  c++  java
  • 洛谷P1005 矩阵取数游戏

    题目描述

    帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n×m的矩阵,矩阵中的每个元素ai,j均为非负整数。游戏规则如下:

    1. 每次取数时须从每行各取走一个元素,共n个。经过m次后取完矩阵内所有元素;
    2. 每次取走的各个元素只能是该元素所在行的行首或行尾;
    3. 每次取数都有一个得分值,为每行取数的得分之和,每行取数的得分 = 被取走的元素值×2^i,其中ii表示第i次取数(从1开始编号);
    4. 游戏结束总得分为m次取数得分之和。

    帅帅想请你帮忙写一个程序,对于任意矩阵,可以求出取数后的最大得分。

    输入输出格式

    输入格式:

    输入文件包括n+1行:

    1行为两个用空格隔开的整数n和m。

    2n+1行为n×m矩阵,其中每行有m个用单个空格隔开的非负整数。

    输出格式:

    输出文件仅包含1行,为一个整数,即输入矩阵取数后的最大得分。

    输入输出样例

    输入样例#1: 复制
    2 3
    1 2 3
    3 4 2
    
    输出样例#1: 复制
    82

    说明

    NOIP 2007 提高第三题

    数据范围:

    60%的数据满足:1n,m30,答案不超过10^16
    100%的数据满足:1n,m80,0ai,j1000

    /*
        这一道题区间DP简单题
        状态转移:f[i][j] = max(f[i+1][j] + map[i],f[i][j-1] + map[j])*2
        此处i,j表示一段i到j的区间 *2是因为每一次dp的过程都要多乘一个2,保证答案*2^i
        (好像不怎么好理解233333)
    */
    #include <bits/stdc++.h>
    #define ll long long
    
    using namespace std;
    
    const int maxn = 100;
    
    ll dp[maxn][maxn],ans;
    int n,m,x,mapp[maxn];
    
    int main(){
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= n;i++){
            memset(dp,0,sizeof(dp));
            for(int i = 1;i <= m;i++){
                scanf("%d",&mapp[i]);
                dp[i][i] = mapp[i] << 1;
            }
            for(int len = 1;len < m;len++){
                for(int l = 1;l <= m;l++){
                    int r = l + len;
                    dp[l][r] = max(dp[l+1][r] + mapp[l],dp[l][r-1] + mapp[r]) << 1;
                }
            }
            ans += dp[1][m];
        }
        printf("%lld
    ",ans);
        return 0;
    }
    不加高精 60分
    /*
        和朴素算法的区别在于加了高精
        由于答案可能达到2^80 所以要用高精
        这里用的是封装的大整数类 效率可能比裸的高精慢
        思路来自刘汝佳的紫书
    */
    #include <bits/stdc++.h>
    #define ll long long
    
    using namespace std;
    
    const int maxn = 100;
    
    int n,m,x,mapp[maxn];
    
    struct BigInteger{
        static const int BASE = 100000000;
        static const int WIDTH = 8;
        vector<long long> s;
    
        BigInteger(long long num = 0) {*this = num;}
        BigInteger operator = (long long num) {
            s.clear();
            do{
                s.push_back(num % BASE);
                num /= BASE;
            }while(num > 0);
            return *this;
        }
        BigInteger operator = (const string& str){
            s.clear();
            int x,len = (str.length() - 1) / WIDTH + 1;
            for(int i = 0;i < len;i++){
                int end = str.length() - i*WIDTH;
                int start = max(0,end - WIDTH);
                sscanf(str.substr(start,end-start).c_str(),"%d",&x);
                s.push_back(x);
            }
            return *this;
        }
        BigInteger operator + (const BigInteger& b) const {
            BigInteger c;
            c.s.clear();
            for(int i = 0,g = 0;;i++){
                if(g == 0 && i >= s.size() && i >= b.s.size()) break;
                int x = g;
                if(i < s.size()) x += s[i];
                if(i < b.s.size()) x += b.s[i];
                c.s.push_back(x % BASE);
                g = x /BASE;
            }
            return c;
        }
        BigInteger operator * (const BigInteger& b) const {
            BigInteger c;
            c.s.resize(s.size() + b.s.size());
            for(int i=0; i<s.size(); i++)
                for(int j = 0; j < b.s.size(); j++)
                c.s[i+j] += s[i] * b.s[j];
            for(int i=0; i<c.s.size()-1; i++) {
                c.s[i+1] += c.s[i] / BASE;
                c.s[i] %= BASE;
            }
            while(c.s.back() == 0 && c.s.size()>1)
            c.s.pop_back();
            return c;
        }
        BigInteger operator += (const BigInteger& b){
            *this = *this + b;return *this;
        }
        bool operator < (const BigInteger& b)const {
             if(s.size() != b.s.size()) return s.size() < b.s.size();
             for(int i = s.size()-1;i >= 0;i--){
                 if(s[i] != b.s[i]) return s[i] < b.s[i];
             }
             return false;
        }
        bool operator > (const BigInteger& b)const {
             return b < *this;
        }
        bool operator <= (const BigInteger& b)const {
             return !(b < *this);
        }
        bool operator >= (const BigInteger& b)const {
             return !(*this < b);
        }
        bool operator != (const BigInteger& b)const {
             return b < *this || *this < b;
        }
        bool operator == (const BigInteger& b)const {
             return !(b < *this) && !(*this < b);
        }
        friend ostream& operator << (ostream &out,const BigInteger& x){
             out << x.s.back();
             for(int i = x.s.size() - 2; i >= 0;i--){
                 char buf[20];
                 sprintf(buf,"%08d",x.s[i]);
                 for(int j = 0;j < strlen(buf);j++) out << buf[j];
             }
             return out;
         }
         friend istream& operator >> (istream &in,BigInteger& x){
             string s;
             if(!(in >> s)) return in;
             x = s;
             return in;
         }    
    };
    
    BigInteger dp[maxn][maxn],ans;
    
    inline BigInteger BigInt_max (BigInteger a, BigInteger b)
    {
        return a > b ? a : b;
    }
    
    int main(){
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= n;i++){
            memset(dp,0,sizeof(dp));
            for(int i = 1;i <= m;i++){
                scanf("%d",&mapp[i]);
                dp[i][i] = mapp[i] << 1;
            }
            for(int len = 1;len < m;len++){
                for(int l = 1;l <= m;l++){
                    int r = l + len;
                    dp[l][r] = BigInt_max(dp[l+1][r] + mapp[l],dp[l][r-1] + mapp[r]) * 2;
                }
            }
            ans += dp[1][m];
        }
        cout << ans;
        return 0;
    }
    加高精 100分
  • 相关阅读:
    java_实现接口的枚举类
    java_枚举类枚举值
    使用abstract关键字的使用
    final在类和方法中的使用
    构造方法的作用
    final在Java中的作用
    this和super不能同时出现在构造方法中
    构造方法和全局变量的关系
    IO流
    递归列出一个目录下所有的文件夹和文件
  • 原文地址:https://www.cnblogs.com/bryce02/p/9895626.html
Copyright © 2011-2022 走看看