P1005 矩阵取数游戏(动态规划+高精度)
题目链接:传送门
题目大意:
给定长度为m的数列aj,每次从两端取一个数,得到2k * aj的价值(k为当前的次数,从1开始到m),总共有n行这样的数列,求最大价值总和。
1 ≤ n, m ≤ 80, 0 ≤ aj ≤ 1000;
思路:
状态f[i][j]表示取剩下ai,ai+1,…,aj时的最大价值。
起始状态:
f[0][m-1] = 0;
转移方程:
f[i][j-1] = max(f[i][j-1], f[i][j] + 取掉aj得到的价值);
f[i+1][j] = max(f[i+1][j], f[i][j] + 取掉ai得到的价值);
PS:吃灰模板居然出了bug,还因此WA了一发,是时候更新一下高精模板了。
#include <bits/stdc++.h> using namespace std;
const int maxn = ;
const int MAX_N = + ; struct bigInt{
int len, d[maxn]; void clean() { while(len > && !d[len-]) len--; }
string str() const {
string s;
for (int i = ; i < len; i++) s += d[len--i] + '';
return s;
} bigInt() { memset(d, , sizeof d); len = ; }
bigInt(int num) { *this = num; }
bigInt(char* num) { *this = num; } bool operator < (const bigInt& b) const {
if(len != b.len)
return len < b.len;
for (int i = len-; i >= ; i--)
if (d[i] != b.d[i])
return d[i] < b.d[i];
return false;
}
bool operator >(const bigInt& b) const{return b < *this;}
bool operator<=(const bigInt& b) const{return !(b < *this);}
bool operator>=(const bigInt& b) const{return !(*this < b);}
bool operator!=(const bigInt& b) const{return b < *this || *this < b;}
bool operator==(const bigInt& b) const{return !(b < *this) && !(b > *this);} bigInt operator = (const char* num) {
memset(d, , sizeof d);
len = strlen(num);
for (int i = ; i < len; i++)
d[i] = num[len--i] - '';
clean();
return *this;
}
bigInt operator = (int num) {
char s[];
sprintf(s, "%d", num);
*this = s;
return *this;
}
bigInt operator + (const bigInt& b) {
bigInt c = *this;
for (int i = ; i < b.len; i++) {
c.d[i] += b.d[i];
c.d[i+] += c.d[i]/;
c.d[i] %= ;
}
c.len = max(len, b.len)+;
c.clean();
return c;
}
bigInt operator - (const bigInt& b) {
bigInt c = *this;
int i;
for (i = ; i < b.len; i++) {
c.d[i] -= b.d[i];
if (c.d[i] < ) c.d[i] += , c.d[i+]--;
}
while (c.d[i] < ) c.d[i++] += , c.d[i]--;
c.clean();
return c;
}//只能正数大减小
bigInt operator * (const bigInt& b) const {
bigInt c;
for (int i = ; i < len; i++)
for (int j = ; j < b.len; j++)
c.d[i+j] += d[i] * b.d[j];
for (int i = ; i < len+b.len || !c.d[i]; c.len = ++i) {
c.d[i+] += c.d[i] / ;
c.d[i] %= ;
}
c.clean();
return c;
}
bigInt operator / (const bigInt& b) {
bigInt c = *this, res = ;
for (int i = ; i < len; i++) {
res = res* + c.d[len--i];
int j;
for (j = ; j < ; j++)
if(res < b*(j+))
break;
c.d[len--i] = j;
res = res - b*j;
}
c.clean();
return c;
}
bigInt operator % (const bigInt& b) {
bigInt res = ;
for (int i = ; i < len; i++) {
res = res* + d[len--i];
int j;
for (j = ; j < ; j++)
if(res < b*(j+))
break;
res = res - b*j;
}
return res;
}
bigInt operator += (const bigInt& b) {
*this = *this + b;
return *this;
}
}; istream& operator >> (istream& in, bigInt& x)
{
string s;
in >> s;
x = s.c_str();
return in;
} ostream& operator << (ostream& out, const bigInt& x)
{
out << x.str();
return out;
} int N, M;
bigInt mat[MAX_N];
bigInt mul[MAX_N];
bigInt f[MAX_N][MAX_N]; bigInt dp()
{
bigInt cur;
for (int i = ; i < M; i++) {
for (int j = M-; j > i; j--) {
// cout << "f[" << i << "][" << j << "] = " << f[i][j] << ":" << endl;
// cout << "f[" << i << "][" << j-1 << "] = " << f[i][j-1] << ' ' << "f[" << i+1 << "][" << j << "] = " << f[i+1][j] << endl;
f[i][j-] = max(f[i][j-], f[i][j] + mul[M-(j-i+)+]*mat[j]);
f[i+][j] = max(f[i+][j], f[i][j] + mul[M-(j-i+)+]*mat[i]);
// cout << "f[" << i << "][" << j-1 << "] = " << f[i][j-1] << ' ' << "f[" << i+1 << "][" << j << "] = " << f[i+1][j] << endl;
}
}
for (int i = ; i < M; i++) {
cur = max(cur, f[i][i] + mul[M]*mat[i]);
}
// cout << cur << endl;
return cur;
} int main()
{
cin >> N >> M;
mul[] = ;
for (int i = ; i < MAX_N; i++)
mul[i] = mul[i-] * ;
bigInt ans;
while (N--) {
for (int i = ; i < M; i++) {
cin >> mat[i];
}
for (int i = ; i < M; i++)
for (int j = i; j < M; j++)
f[i][j] = ;
ans = ans + dp();
}
//模板居然出了bug,怕是在硬盘里吃灰吃多了
while (ans.d[ans.len-] > ) {
ans.d[ans.len] = ans.d[ans.len-] / ;
ans.d[ans.len-] %= ;
ans.len++;
}
cout << ans << endl;
return ;
}
P1005 矩阵取数游戏(动态规划+高精度)的更多相关文章
- 【Luogu】P1005矩阵取数游戏(高精度+DP)
题目链接 yeah终于过辣! DP,f[i][j]表示每行还剩i到j这个区间的数没取的时候的值.借这个题我也把高精度的短板弥补了一下,以后高精加高精乘应该是没问题了. 哇终于不怂高精了…… 放上代码. ...
- [LuoguP1005]矩阵取数游戏 (DP+高精度)
题面 传送门:https://www.luogu.org/problemnew/show/P1005 Solution 我们可以先考虑贪心 我们每一次都选左右两边尽可能小的数,方便大的放在后面 听起来 ...
- 洛谷P1005 矩阵取数游戏
P1005 矩阵取数游戏 题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次 ...
- P1005 矩阵取数游戏 区间dp 高精度
题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n \times mn×m的矩阵,矩阵中的每个元素a_{i,j}ai,j均为非负整数.游戏规则如下: 每次取数时须从每行各取走一个元素,共n ...
- [NOIP2007] 提高组 洛谷P1005 矩阵取数游戏
题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...
- 洛谷 P1005 矩阵取数游戏
题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...
- P1005 矩阵取数游戏[区间dp]
题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的\(m*n\)的矩阵,矩阵中的每个元素\(a_{i,j}\)均为非负整数.游戏规则如下: 每次取数时须从每行各取走一个元素,共n个.经过m次后 ...
- 洛谷 P1005 矩阵取数游戏 (区间dp+高精度)
这道题大部分时间都在弄高精度-- 还是先讲讲dp吧 这道题是一个区间dp,不过我还是第一次遇到这种类型的区间dp f[i][j]表示取了数之后剩下i到j这个区间的最优值 注意这里是取了i之前和j之后的 ...
- P1005 矩阵取数游戏
传送门 思路: △ 区间动规 对于每行,有 f [ i ][ j ] 代表取区间 [ i , j ] 的最大值. 然后转移方程我们考虑,对于每一个新的 f [ i ][ j ],有两种情况(下面定义 ...
随机推荐
- python字典和列表的高级应用
1.将序列分解为单独的变量 1.1问题 包含n个元素的元组或列表.字符串.文件.迭代器.生成器,将它分解为n个变量 1.2方案 直接通过赋值操作 要求:变量个数要等于元素个数 当执行分解操作时,有时需 ...
- ckeditor富文本编辑器的基本配置设置:
原文地址:http://blog.csdn.net/wei365456yin/article/details/54618970?locationNum=5&fps=1 1.首先下载ckedit ...
- Win10系列:VC++ Direct3D模板介绍1
Visual Studio为开发Direct3D应用程序提供了便捷的模版,读者可以不必手动去新建Direct3D中所使用到的基础资源,而只需专注于图形的绘制.本小节主要为读者介绍这个模版中用于绘制图形 ...
- 微信和支付宝支付模式详解及实现(.Net标准库)
支付基本上是很多产品都必须的一个模块,大家最熟悉的应该就是微信和支付宝支付了,不过更多的可能还是停留在直接sdk的调用上,甚至和业务系统高度耦合,网上也存在各种解决方案,但大多形式各异,东拼西凑而成. ...
- day29 socketsever ftp功能简单讲解
今日所学 一.ftp上传简单实例 二.socketsever的固定用法 三.验证合法性连接 1.ftp上传实例 这个题目是我们现在网络编程比较基础一点的题目 下面我们只写简单上传的代码 上传服务端的代 ...
- 《python》join、守护进程、锁/信号量/事件、进程队列
一.multiprocess.process模块 1.join方法 阻塞主进程,等待子进程执行完毕再放开阻塞 import time import random from multiprocessin ...
- 《Python》常用模块之collections模块
内置的数据类型: int float complex str list tuple dict set 基础数据类型: int float complex str list tuple ...
- 将自己的域名解析跳转到博客主页(GitHub中的gitpage跳转)
最近突然迷上了博客,突然又突发奇想,将自己几个月前买的现在限制的域名拿来跳转到自己的csdn博客.经过一番研究,总结---- 把自己的购买的域名(比如我买的circleyuan.top)跳转到CSDN ...
- 十九. Python基础(19)--异常
十九. Python基础(19)--异常 1 ● 捕获异常 if VS异常处理: if是预防异常出现, 异常处理是处理异常出现 异常处理一般格式: try: <............. ...
- mysql中sql查询使用注意
1.注意DESC关键字仅适用于在它前面的列名(birth):不影响species列的排序顺序. SELECT name, species, birth FROM pet ORDER BY specie ...