• 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;
    }
  • 相关阅读:
    Python基础学习Day2
    Python基础学习
    字符串
    function对象
    GCN入门理解
    L1、L2正则化详解
    Matplotlib数据可视化基础
    sklearn 中模型保存的两种方法
    一文弄懂神经网络中的反向传播法——BackPropagation
    seaborn可视化
  • 原文地址:https://www.cnblogs.com/dishu/p/4307749.html
Copyright © 2020-2023  润新知