• 最大连续子段和


    最大连续子段和
    sum表示以当前数a[i]结尾的最大子段和,
    如果sum<0,那么它对后面就没有积极作用,不如抛弃。
    所以
    sum+=a[i]
    维护最大值
    sum=max(sum,0)

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<ctime>
     7 #include<cstring>
     8 #define inf 2147483647
     9 #define For(i,a,b) for(register int i=a;i<=b;i++)
    10 #define p(a) putchar(a)
    11 #define g() getchar()
    12 //by war
    13 //2017.10.19
    14 using namespace std;
    15 int n;
    16 int a[200][200];
    17 int f[200];
    18 int x;
    19 int ans;
    20 int sum;
    21 int Max;
    22 void in(int &x)
    23 {
    24     int y=1;
    25     char c=g();x=0;
    26     while(c<'0'||c>'9')
    27     {
    28     if(c=='-')
    29     y=-1;
    30     c=g();
    31     }
    32     while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
    33     x*=y;
    34 }
    35 void o(int x)
    36 {
    37     if(x<0)
    38     {
    39         p('-');
    40         x=-x;
    41     }
    42     if(x>9)o(x/10);
    43     p(x%10+'0');
    44 }
    45 int main()
    46 {
    47     in(n);
    48     For(i,1,n)
    49       For(j,1,n)
    50         {
    51             in(x);
    52             a[i][j]=a[i][j-1]+x;
    53         }
    54     ans=-inf;
    55     For(l,1,n)
    56       For(r,l,n)
    57         {
    58             Max=-inf;
    59             sum=0;
    60             For(i,1,n)
    61             {
    62                 sum+=a[i][r]-a[i][l-1];
    63                 Max=max(Max,sum);
    64                 sum=max(sum,0);
    65             }
    66             ans=max(ans,Max);
    67         }
    68     o(ans);
    69      return 0;
    70 }
    View Code
  • 相关阅读:
    三个录屏软件
    不用 PS 和 AI,5个网站能做出更好看的设计
    使用vue.js开发小程序
    js异步处理
    HTTP、HTTP1.0、HTTP1.1、HTTP2.0、HTTPS
    Chrome不支持css字体小于12px的解决办法
    处理CSS前缀问题的神器——AutoPrefixer
    CSS | 字体系列
    qemu-img 命令讲解
    全面理解 git
  • 原文地址:https://www.cnblogs.com/war1111/p/7690622.html
Copyright © 2020-2023  润新知