洛谷P1005 矩陣取數遊戲
阿新 • • 發佈:2018-11-02
題目描述
帥帥經常跟同學玩一個矩陣取數遊戲:對於一個給定的n×m的矩陣,矩陣中的每個元素ai,j均為非負整數。遊戲規則如下:
- 每次取數時須從每行各取走一個元素,共n個。經過m次後取完矩陣內所有元素;
- 每次取走的各個元素只能是該元素所在行的行首或行尾;
- 每次取數都有一個得分值,為每行取數的得分之和,每行取數的得分 = 被取走的元素值×2^i,其中ii表示第i次取數(從1開始編號);
- 遊戲結束總得分為m次取數得分之和。
帥帥想請你幫忙寫一個程式,對於任意矩陣,可以求出取數後的最大得分。
輸入輸出格式
輸入格式:
輸入檔案包括n+1行:
第1行為兩個用空格隔開的整數n和m。
第2∽n+1行為n×m矩陣,其中每行有m個用單個空格隔開的非負整數。
輸出格式:
輸出檔案僅包含1行,為一個整數,即輸入矩陣取數後的最大得分。
輸入輸出樣例
輸入樣例#1: 複製2 3 1 2 3 3 4 2輸出樣例#1: 複製
82
說明
NOIP 2007 提高第三題
資料範圍:
60%的資料滿足:1≤n,m≤30,答案不超過10^16
100%的資料滿足:1≤n,m≤80,0≤ai,j≤1000
/* 這一道題區間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)不加高精 60分*/ #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\n",ans); return 0; }
/* 和樸素演算法的區別在於加了高精 由於答案可能達到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分