zoukankan      html  css  js  c++  java
  • hdu 1003 Max Sum 简单DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

    转移方程:dp[i]=max(dp[i-1]+a[i],a[i])

    虽然是dp 但不用真的申请一个dp数组

    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <set>
    #include <queue>
    #include <vector>
    
    using namespace std;
    
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int, int> P;
    
    const int maxn = 100100;
    
    int a[maxn];
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
    
        int T;
        scanf("%d", &T);
        for(int t = 1; t <= T; t++)
        {
            int n;
            scanf("%d", &n);
            for(int i = 0; i < n; i++)
                scanf("%d", &a[i]);
    
            int l, r, tmp_r, tmp_l;
            int ans = -1001;
            int tmp = -1001;
    
    
            for(int i = 0; i < n; i++)
            {
                if(tmp + a[i] >= a[i])
                {
                    tmp_r = i+1;
                    tmp += a[i];
                    if(tmp > ans)
                    {
                        ans = tmp;
                        l = tmp_l;
                        r = tmp_r;
                    }
                }
                else
                {
                    tmp_l = i+1;
                    tmp_r = i+1;
                    tmp = a[i];
                    if(tmp > ans)
                    {
                        ans = tmp;
                        l = tmp_l;
                        r = tmp_r;
                    }
                }
            }
    
    
            printf("Case %d:
    ", t);
            printf("%d %d %d
    ", ans, l, r);
            if(t != T)
                printf("
    ");
    
    
        }
    
        return 0;
    }
  • 相关阅读:
    js计算两个时间相差天数
    享元模式
    外观模式
    组合模式
    装饰者模式
    桥接模式
    适配器模式
    元素量词 ? + *
    linux安装使用7zip
    linux shell使用别名,切换当前目录
  • 原文地址:https://www.cnblogs.com/dishu/p/4307749.html
Copyright © 2011-2022 走看看