今天在洛谷上刷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. cp显示进度条

    cp显示进度条 alias cp='rsync -av --progress'

  2. 【Unity】ShareSDK、SMSSDK的基本使用与常见问题

    概要 测试使用ShareSDK的一些常用功能.包括: 用微博帐号做第三方登录 获取用户的帐号详细信息 获取好友列表 分享功能 测试使用SMSSDK插件,包括: 导入插件,解决包冲突 短信登录功能:发验 ...

  3. TestNg学习

    参考:https://www.yiibai.com/testng/junit-vs-testng-comparison.html#article-start 1.JUnit缺点: 最初的设计,使用于单 ...

  4. Netbeans 8.1 检测不到Tomcat8.5.3以上版本已经启动的Bug

    Tomcat实际上已经启动,但是netbeans就是检测不到,只要在server.xml中,找到http/1.1的connector 添加 属性 server="Apache-Coyote/ ...

  5. 查看CPU/CACHE的拓扑结构

    转自 http://smilejay.com/2017/12/cpu-cache-topology/ Linux上,CPU和Cache相关的拓扑结构,都可以从sysfs文件系统的目录 /sys/dev ...

  6. IntellIJ IDEA 启动 参数 配置

    系统环境: 型号名称: MacBook Pro型号标识符: MacBookPro11,4处理器名称: Intel Core i7处理器速度: 2.8 GHz处理器数目: 1核总数: 4L2 缓存(每个 ...

  7. Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

    异常信息如下: 错误: Unexpected exception parsing XML document from class path resource [spring/applicationCo ...

  8. Markdown 标题

    用 Markdown 书写时,只需要在文本前面加上 # 即可创建标题,Markdown 支持六级标题,语法及效果如下 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五 ...

  9. sonar-scanner的执行流程和对ClassLoader,动态代理的使用

    最近项目上使用了sonarqube来提供静态代码检查的服务,在看sonar-scanner的源码的时候,发现sonar-scanner用来分析的jar包是从sonar的服务器上下载下来的,使用自定义的 ...

  10. 1. Mysql数据库的安装

    1. Mysql数据库的安装 (1)打开Mysql安装软件,同意相关协议进入下一步安装,在选择安装类型中选择[自定义]进入下一步安装. (2)选择安装的组件信息. (3)服务器软件安装目录 (4)数据 ...