今天在洛谷上刷dp,忽然冒出一道求最大字段和的问题,然后忘了瞬间忘了这是dp,几分钟一个贪心出来了成功ac,忽然想起自己在作dp,于是乖乖刷dp。

这个可能很多人都会但是今天有4种解法哦,本人只尝试了3种解法。

NO.1:明显一个贪心一个sum求当前的累加和如果小于0就清零,继续累加并不断去max即可。

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
inline long long read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
int a[];
int ans=;
int maxx=-;
int main()
{
//freopen("1.in","r",stdin);
n=read();
for(int i=;i<=n;i++)a[i]=read();
for(int i=;i<=n;i++)
{
ans+=a[i];
if(ans>maxx)
{
maxx=ans;
}
if(ans<)
ans=;
}
printf("%d\n",maxx);
return ;
}

NO.2:说是要练dp嘛,然后点开题解看dalao们的dp,f[i]=max(f[i-1],a[i]);这样最大值在f[i]之中,最后找max即可。

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
inline long long read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
int a[];
int f[];
int ans=-;
int main()
{
// freopen("1.in","r",stdin);
n=read();
for(int i=;i<=n;i++)
{
a[i]=read();
f[i]=max(f[i-]+a[i],a[i]);
ans=max(f[i],ans);
}
cout<<ans<<endl;
return ;
}

NO.3:题解之中有一个分治的想法,十分巧妙的把所有情况递归出来从而求解。在每一个区间之中取max即可。

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
inline long long read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=-;
int n;
int a[];
int bwy(int x,int y)
{
if(x==y)return a[x];
int mid=(x+y)>>;
int maxx=maxn,maxx1=maxn,sum=;
for(int i=mid;i>=x;i--){sum+=a[i];maxx=max(maxx,sum);}sum=;
for(int i=mid+;i<=y;i++){sum+=a[i];maxx1=max(maxx1,sum);}
return max(max(bwy(x,mid),bwy(mid+,y)),maxx+maxx1);
}
int main()
{
//freopen("1.in","r",stdin);
n=read();
for(int i=;i<=n;i++)a[i]=read();
printf("%d\n",bwy(,n));
return ;
}

这个就比较难理解了,要多看看。

No.4:有大神用的是线段树来维护,tql,我不想打线段树代码就没有了。。。

下面回到了本校oj看起来以前过得题想要再想想。求最大连续子矩阵累加和,这个就要压缩维度了。

仔细想想,我可以先把每一列的前缀和全部求出来进行维度的压缩,然用一个一维数组来表示第几列从第i行到第j行的累加,十分巧妙的思想,我想了十几分钟才搞懂。这样的话就可以便利到每一个矩阵了,十分巧妙!!!

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
inline long long read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=;
int n,a[maxn][maxn],tmp[maxn],maxx=-,ans=;
int main()
{
freopen("1.in","r",stdin);
n=read();
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
maxx=read();
a[i][j]=a[i-][j]+maxx;//前缀和
}
maxx=-;
for(int i=;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
ans=;//注意便利下一个矩阵的时候ans要清零。
for(int k=;k<=n;k++)
{
tmp[k]=a[j][k]-a[i-][k];
ans+=tmp[k];
if(ans>maxx)maxx=ans;
if(ans<)ans=;
}
}
}
printf("%d\n",maxx);
return ;
}

然后还有很难的三维压缩qwq真不想写。。。

三维的压缩,但首先要看懂题。

看着学长的代码自慢慢干,发现书上的方法并不是很优,还不如和第二题一样进行压缩。

a[i][j][k]+=a[i-1][j][k];压缩高度——c[k][u]=a[j][k][u]-a[i-1][k][u];再取任意宽度和长度的立方体块压到一个二维数组之中实现任意立方体小块的拿取,

这样就可以了,c[k][u]+=c[k][u-1];压缩宽度这样就和上一题一样开始压缩二维数组,取任意矩阵的和。b[x]=c[x][u]-c[x][k-1];——这样压缩到一维的数组里面取任意的矩阵然后用求最大字段和就行了。是很难理解的表示不想手动模拟一遍。。。

代码:

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
inline long long read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int a[][][],c[][],b[],ans,maxx=-;
int n,h,m;
int main()
{
//freopen("1.in","r",stdin);
h=read();m=read();n=read();
for(int i=;i<=h;i++)for(int j=;j<=m;j++)for(int k=;k<=n;k++)
a[i][j][k]=read(),a[i][j][k]+=a[i-][j][k];
for(int i=;i<=h;i++)for(int j=i;j<=h;j++)
{
for(int k=;k<=m;k++)for(int u=;u<=n;u++)
{
c[k][u]=a[j][k][u]-a[i-][k][u];
c[k][u]+=c[k][u-];
}
for(int k=;k<=n;k++)for(int u=k;u<=n;u++)
{
ans=;
for(int x=;x<=m;x++)
{
b[x]=c[x][u]-c[x][k-];
ans+=b[x];
if(ans>maxx)maxx=ans;
if(ans<)ans=;
}
}
}
printf("%d\n",maxx);
return ;
}

大功告成啦。。。

如果你的青春感到迷茫,那就对了,因为谁的青春不迷茫。

b[x]=c[x][u]-c[x][k-1];

压缩维度oj P1173+P1174+P1164的更多相关文章

  1. Pytorch Tensor 维度的扩充和压缩

    维度扩展 x.unsqueeze(n) 在 n 号位置添加一个维度 例子: import torch x = torch.rand(3,2) x1 = x.unsqueeze(0) # 在第一维的位置 ...

  2. Careercup - Facebook面试题 - 4909367207919616

    2014-05-01 01:23 题目链接 原题: WAP to modify the array such that arr[I] = arr[arr[I]]. Do this in place i ...

  3. numpy 实践记录

    reshape是从低维度到高维度.max,sum等函数都是注意axis,不选择就是全体计算. swapaxes 转换轴,将两个选择的轴对调,在CNN中X乘W有的时候需要拉伸,如果轴不同结果不对. 看p ...

  4. tensorflow错误:Shape (10, ?) must have rank at least 3

    错误的代码 outputs, _ = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) 错误原因: 该错误的意思是传入的数据集X的维度只有二维,而tf.nn.d ...

  5. FCN 项目部分代码学习

    下面代码由搭档注释,保存下来用作参考. github项目地址:https://github.com/shekkizh/FCN.tensorflowfrom __future__ import prin ...

  6. Neural Networks and Deep Learning(week2)Logistic Regression with a Neural Network mindset(实现一个图像识别算法)

    Logistic Regression with a Neural Network mindset You will learn to: Build the general architecture ...

  7. 神经网络前向后向传播(理论推导+代码) 单层神经网络相当于logistic regression

    建立神经网络的主要步骤是: 1. 定义模型结构(例如输入特征的数量) 2. 初始化模型的参数 3. 循环: # 3.1 计算当前损失(正向传播) # 3.2 计算当前梯度(反向传播) # 3.3 更新 ...

  8. Deeplearning——Logistics回归

    资料来源:1.博客:http://binweber.top/2017/09/12/deep_learning_1/#more——转载,修改更新 2.文章:https://www.qcloud.com/ ...

  9. scikit-learn和tensorflow的区别

    1.功能不同 Scikit-learn(sklearn)的定位是通用机器学习库,而TensorFlow(tf)的定位主要是深度学习库.一个显而易见的不同:tf并未提供sklearn那种强大的特征工程, ...

随机推荐

  1. Oracle数据库学习(一)安装和简单使用

    新公司的新项目,需要用到Oracle数据库,所以现在便来解除此数据库,不得不说,这个数据库还这是麻烦. 安装倒是简单,就是中间会遇到各种问题. 安装步骤参考:https://blog.csdn.net ...

  2. EntLib 自动数据库连接字符串加密

    const string provider = "RsaProtectedConfigurationProvider"; Configuration config = null; ...

  3. 【linux】——cscope

    cscope是一款linux下的软件,其功能主要是用在阅读代码,堪称Windows下的Source Insight,但是配合vim使用,效率无与伦比.如需了解其具体使用,请先安装vim,然后在终端执行 ...

  4. hdoj:2042

    #include <iostream> using namespace std; int main() { int n,a; while (cin >> n) { while ...

  5. Android 5.0 Phone初始化分析

    已经更新至个人blog:http://dxjia.cn/2015/07/android-5-0-phone-init-analysis/ persistent属性 要想了解phone的框架,首先需要了 ...

  6. ubuntu中文件夹的作用

    /bin系統有很多放置執行檔的目錄,但/bin比較特殊.因為/bin放置的是在單人維護模式下還能夠被操作的指令. 在/bin底下的指令可以被root與一般帳號所使用,主要有:cat, chmod, c ...

  7. 关于Unity中ARPG游戏人物移动(专题十一)

    ARPG:动作型角色扮演类游戏 大多数的ARPG游戏都是使用摇杆操作,以第三人称摄像机的方式来跟随主角,实际上人物只走八个方向,上,下,左,右,左上,左下,右下,右上 控制角色移动的思路1: 在ARP ...

  8. Go语言_range(范围)理解

    一.Go语言中的range Go 语言中 range 关键字用于 for循环中迭代数组(array).切片(slice).链表(channel)或集合(map)的元素: 在数组和切片中它返回元素的索引 ...

  9. ubuntu安装anaconda后,终端输入conda,出现未找到命令

    解决办法: 终端输入:vim  ~/.bashrc 键盘大写“G”,在最末端输入:export PATH=~/anaconda2/bin:$PATH 使其生效:source  ~/.bashrc 打印 ...

  10. mysql+redis

    微博的系统架构,想用mysql+redis配合使用,具体操作步骤: 写入数据到Redis,,然后在写个运行cron的脚本,美妙读内存,并写入数据库即可. 使用注意: 1.MySQL使用需要注意的地方: ...