zoukankan      html  css  js  c++  java
  • poj2593

    dp,分别从左侧和右侧求最大子段和,然后把fleft[i]变成0~i区间中最大子段和,fright以此类推。

    然后枚举中间分割点。

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 100003

    int f[maxn], fleft[maxn], fright[maxn];
    int n;

    void input()
    {
    for (int i = 0; i < n; i++)
    scanf(
    "%d", &f[i]);
    }

    void work1()
    {
    fleft[
    0] =f[0];
    for (int i = 1; i < n; i++)
    if (f[i] > fleft[i - 1] + f[i])
    fleft[i]
    = f[i];
    else
    fleft[i]
    = fleft[i - 1] + f[i];
    for (int i = 1; i < n; i++)
    fleft[i]
    = max(fleft[i - 1], fleft[i]);
    }

    void work2()
    {
    fright[n
    - 1] =f[n - 1];
    for (int i = n - 2; i >= 0; i--)
    if (f[i] > fright[i + 1] + f[i])
    fright[i]
    = f[i];
    else
    fright[i]
    = fright[i + 1] + f[i];
    for (int i = n - 2; i >= 0; i--)
    fright[i]
    = max(fright[i + 1], fright[i]);
    }

    void work3()
    {
    int ans = -1000000000;

    for (int i = 0; i < n - 1; i++)
    ans
    = max(ans, fleft[i] + fright[i + 1]);
    printf(
    "%d\n", ans);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    while (scanf("%d", &n), n != 0)
    {
    input();
    work1();
    work2();
    work3();
    }
    return 0;
    }

  • 相关阅读:
    文件层次结构思维导图
    root的密码破解.linux
    常用Linux命令
    设计模式之-状态模式
    ThreadLocal详解及仿写实例
    SpringBoot资料
    27.枚举
    菜鸟python---元组
    菜鸟python---列表的增删改查嵌套
    菜鸟python---字符串详解
  • 原文地址:https://www.cnblogs.com/rainydays/p/2051996.html
Copyright © 2011-2022 走看看