zoukankan      html  css  js  c++  java
  • poj1050

    经典的最大子矩阵和。。

    转化成最大子段和

    (通过枚举列的长度,然后对于选定的那一列做一次的最长子段和)最后选取最大就是答案。。

     1 /*
     2  State:Accepted
     3  Time:2013.3.1
     4 */
     5 
     6 #include <iostream>
     7 #include <cstdlib>
     8 #include <cstdio>
     9 #include <cstring>
    10 #include <algorithm>
    11 #include <fstream>
    12 #include <string>
    13 #include <cmath>
    14 using namespace std;
    15 int n , a[150][150] ,f[200], sumrow[200][200] , ans ;
    16 
    17 void init(){      
    18       scanf("%d",&n);
    19       for (int i = 1; i <= n; ++i)
    20           for (int j = 1; j <= n; ++j){
    21               scanf("%d",&a[i][j]);
    22               sumrow[i][j] = sumrow[i - 1][j] + a[i][j];
    23           }
    24       
    25 }
    26 
    27 void dp(){
    28      int nowv;
    29      ans = -1000000;
    30      for (int l = 1; l <= n; ++l)
    31         for (int r = l; r <= n; ++r){
    32             for (int i = 1; i <= n; ++i)  f[i] = -1000000;
    33             for (int i = 1; i <= n; ++i){
    34                nowv = sumrow[r][i] - sumrow[l - 1][i];
    35                f[i] = max(nowv , f[i -1] + nowv);
    36                ans  = max(ans , f[i]);
    37             }
    38         }
    39      printf("%d\n",ans);
    40      
    41 }
    42 int main(){
    43       freopen("poj1050.in","r",stdin);
    44       freopen("poj1050.out","w",stdout);
    45       init();
    46       dp();
    47       fclose(stdin);
    48       fclose(stdout);
    49 }
  • 相关阅读:
    js Touch事件(向左滑动,后退)
    浏览器滚动条到底部自动加载更多
    如何终止terminal后台进程
    DFT介绍
    GDS area estimation
    Web Scraping with Python第二章
    Web Scraping with Python第一章
    python技巧汇总
    centos6.4安装zsh与dircolors
    linux解压命令汇总
  • 原文地址:https://www.cnblogs.com/yzcstc/p/2977605.html
Copyright © 2011-2022 走看看