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

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<map>
  4. #include<vector>
  5. #include<iomanip>
  6. #include<cmath>
  7. #include<ctime>
  8. #include<cstring>
  9. #include<string>
  10. #include<algorithm>
  11. #include<queue>
  12. #include<stack>
  13. using namespace std;
  14. inline long long read()
  15. {
  16. int x=,f=;char ch=getchar();
  17. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  18. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  19. return x*f;
  20. }
  21. int n;
  22. int a[];
  23. int ans=;
  24. int maxx=-;
  25. int main()
  26. {
  27. //freopen("1.in","r",stdin);
  28. n=read();
  29. for(int i=;i<=n;i++)a[i]=read();
  30. for(int i=;i<=n;i++)
  31. {
  32. ans+=a[i];
  33. if(ans>maxx)
  34. {
  35. maxx=ans;
  36. }
  37. if(ans<)
  38. ans=;
  39. }
  40. printf("%d\n",maxx);
  41. return ;
  42. }

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<map>
  4. #include<vector>
  5. #include<iomanip>
  6. #include<cmath>
  7. #include<ctime>
  8. #include<cstring>
  9. #include<string>
  10. #include<algorithm>
  11. #include<queue>
  12. #include<stack>
  13. using namespace std;
  14. inline long long read()
  15. {
  16. int x=,f=;char ch=getchar();
  17. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  18. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  19. return x*f;
  20. }
  21. int n;
  22. int a[];
  23. int f[];
  24. int ans=-;
  25. int main()
  26. {
  27. // freopen("1.in","r",stdin);
  28. n=read();
  29. for(int i=;i<=n;i++)
  30. {
  31. a[i]=read();
  32. f[i]=max(f[i-]+a[i],a[i]);
  33. ans=max(f[i],ans);
  34. }
  35. cout<<ans<<endl;
  36. return ;
  37. }

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<map>
  4. #include<vector>
  5. #include<iomanip>
  6. #include<cmath>
  7. #include<ctime>
  8. #include<cstring>
  9. #include<string>
  10. #include<algorithm>
  11. #include<queue>
  12. #include<stack>
  13. using namespace std;
  14. inline long long read()
  15. {
  16. int x=,f=;char ch=getchar();
  17. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  18. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  19. return x*f;
  20. }
  21. const int maxn=-;
  22. int n;
  23. int a[];
  24. int bwy(int x,int y)
  25. {
  26. if(x==y)return a[x];
  27. int mid=(x+y)>>;
  28. int maxx=maxn,maxx1=maxn,sum=;
  29. for(int i=mid;i>=x;i--){sum+=a[i];maxx=max(maxx,sum);}sum=;
  30. for(int i=mid+;i<=y;i++){sum+=a[i];maxx1=max(maxx1,sum);}
  31. return max(max(bwy(x,mid),bwy(mid+,y)),maxx+maxx1);
  32. }
  33. int main()
  34. {
  35. //freopen("1.in","r",stdin);
  36. n=read();
  37. for(int i=;i<=n;i++)a[i]=read();
  38. printf("%d\n",bwy(,n));
  39. return ;
  40. }

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

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

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<map>
  4. #include<vector>
  5. #include<iomanip>
  6. #include<cmath>
  7. #include<ctime>
  8. #include<cstring>
  9. #include<string>
  10. #include<algorithm>
  11. #include<queue>
  12. #include<stack>
  13. using namespace std;
  14. inline long long read()
  15. {
  16. int x=,f=;char ch=getchar();
  17. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  18. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  19. return x*f;
  20. }
  21. const int maxn=;
  22. int n,a[maxn][maxn],tmp[maxn],maxx=-,ans=;
  23. int main()
  24. {
  25. freopen("1.in","r",stdin);
  26. n=read();
  27. for(int i=;i<=n;i++)
  28. for(int j=;j<=n;j++)
  29. {
  30. maxx=read();
  31. a[i][j]=a[i-][j]+maxx;//前缀和
  32. }
  33. maxx=-;
  34. for(int i=;i<=n;i++)
  35. {
  36. for(int j=i;j<=n;j++)
  37. {
  38. ans=;//注意便利下一个矩阵的时候ans要清零。
  39. for(int k=;k<=n;k++)
  40. {
  41. tmp[k]=a[j][k]-a[i-][k];
  42. ans+=tmp[k];
  43. if(ans>maxx)maxx=ans;
  44. if(ans<)ans=;
  45. }
  46. }
  47. }
  48. printf("%d\n",maxx);
  49. return ;
  50. }

然后还有很难的三维压缩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];——这样压缩到一维的数组里面取任意的矩阵然后用求最大字段和就行了。是很难理解的表示不想手动模拟一遍。。。

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<map>
  4. #include<vector>
  5. #include<iomanip>
  6. #include<cmath>
  7. #include<ctime>
  8. #include<cstring>
  9. #include<string>
  10. #include<algorithm>
  11. #include<queue>
  12. #include<stack>
  13. using namespace std;
  14. inline long long read()
  15. {
  16. int x=,f=;char ch=getchar();
  17. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  18. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  19. return x*f;
  20. }
  21. int a[][][],c[][],b[],ans,maxx=-;
  22. int n,h,m;
  23. int main()
  24. {
  25. //freopen("1.in","r",stdin);
  26. h=read();m=read();n=read();
  27. for(int i=;i<=h;i++)for(int j=;j<=m;j++)for(int k=;k<=n;k++)
  28. a[i][j][k]=read(),a[i][j][k]+=a[i-][j][k];
  29. for(int i=;i<=h;i++)for(int j=i;j<=h;j++)
  30. {
  31. for(int k=;k<=m;k++)for(int u=;u<=n;u++)
  32. {
  33. c[k][u]=a[j][k][u]-a[i-][k][u];
  34. c[k][u]+=c[k][u-];
  35. }
  36. for(int k=;k<=n;k++)for(int u=k;u<=n;u++)
  37. {
  38. ans=;
  39. for(int x=;x<=m;x++)
  40. {
  41. b[x]=c[x][u]-c[x][k-];
  42. ans+=b[x];
  43. if(ans>maxx)maxx=ans;
  44. if(ans<)ans=;
  45. }
  46. }
  47. }
  48. printf("%d\n",maxx);
  49. return ;
  50. }

大功告成啦。。。

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

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. java框架篇---hibernate之CRUD操作

    CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写. 下面列举实例来讲解这几个操作: 实体类: ...

  2. 【九天教您南方cass 9.1】 11 方格网土方计算

    同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 我是本节课主讲老师九天. 我们讲课的教程附件也是共享的,请注意索取测量空间中. [点击索取cass教程]5元立得 (给客服说暗号:“ ...

  3. TI am335x am437x PRU

    http://bbs.eeworld.com.cn/thread-355798-1-1.html

  4. npm国内镜像

    国内使用默认的源安装较慢,镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set registry h ...

  5. WebMisSharp升级说明,最新版本1.6.0

    尊敬的C3 AM.C3 FX.WebMisSharp用户您好: 非常感谢长期来您对WebMisSharp系列产品的支持,您的使用和反馈是我们进步的最大动力.在你们的帮助下我们又向前迈进了一步,我们功能 ...

  6. Java知多少(82)标签、按钮和按钮事件简介

    标签和按钮也许是图形界面中最常见的两种组件,按钮又总是与激发动作事件有关. 标签 标签(JLabel)是最简单的Swing组件.标签对象的作用是对位于其后的界面组件作说明.可以设置标签的属性,即前景色 ...

  7. IllegalArgumentException:@Body parameters cannot be used with form or multi-part encoding

    使用retrofit时报错IllegalArgumentException:@Body parameters cannot be used with form or multi-part encodi ...

  8. 嵌入式开发之精确延时---多线程延时阻塞精度asm("nop") nanosleep usleep sleep select

    http://blog.csdn.net/lile777/article/details/45503087

  9. SQL中in参数在存储过程中传递及使用的方法

    背景: 1.使用存储过程 2.存储过程中有in 3.in括号里面的内容作为参数传递 解决方案: 1.直接拼接sql 可在存储过程中拼接字符串,然后执行此字符串,类似于js中的eval PROCEDUR ...

  10. [Localization] SSD - Single Shot MultiBoxDetector

    Prerequisite: VGG Ref: [Object Tracking] Localization and Detection SSD Paper: http://lib.csdn.net/a ...