今天在洛谷上刷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. Python爬取金山词霸每日一句,存储到MySQL中

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/7/3 20:25 # @Author : baoshan # @Site : ...

  2. React Native常用组件之ScrollView

    1. 两个要点 1.1 ScrollView必须有一个确定的高度才能正常工作 它实际上所做的就是将一系列不确定高度的子组件装进一个确定高度的容器(通过滚动操作) 通常有两种做法: 第一种: 直接给该S ...

  3. Oracle分析函数-排序排列(rank、dense_rank、row_number、ntile)

    (1)rank函数返回一个唯一的值,除非遇到相同的数据时,此时所有相同数据的排名是一样的,同时会在最后一条相同记录和下一条不同记录的排名之间空出排名. (2)dense_rank函数返回一个唯一的值, ...

  4. JavaScript高级用法一之事件响应与网页交互

    综述 本篇的主要内容来自慕课网,事件响应与网页交互,主要内容如下 1 什么是事件 2 鼠标单击事件( onclick ) 3 鼠标经过事件(onmouseover) 4 鼠标移开事件(onmouseo ...

  5. [algorithm] 汉诺塔问题

    汉诺塔是根据一个传说形成的一个问题.汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗 ...

  6. [Localization] MobileNet with SSD

    先来一波各版本性能展览: Pre-trained Models Choose the right MobileNet model to fit your latency and size budget ...

  7. Eclipse设置默认的换行长度

    1. 点击Window->Preferences->Java->Code Style->Formatter 2. 点击New,给profile随意取个名字,点击OK 3. Ma ...

  8. 为你的mail server增加SPF记录

    什么是SPF就是Sender Policy Framework.SPF可以防止别人伪造你来发邮件,是一个反伪造性邮件的解决方案.当你定义了你的domain name的SPF记录之后,接收邮件方会根据你 ...

  9. VS Release模式调试

    c++ -> 常规 -〉调试信息格式 选 程序数据库(/Zi)或(/ZI) c++ -> 优化 -〉优化 选 禁止(/Od) 连接器 -〉调试 -〉生成调试信息 选 是 (/DEBUG)

  10. git合并分支上指定的commit

    merge 能够胜任平常大部分的合并需求.但也会遇到某些特殊的情况,例如正在开发一个新的功能,线上说有一个紧急的bug要修复.bug修好了但并不像把仍在开发的新功能代码也提交到线上去.这时候也许想要一 ...