zoukankan      html  css  js  c++  java
  • UVALive 4869 Profits DP

    G - Profits
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

    Description

    Download as PDF

    Your friends have just opened a new business, and you want to see how well they are doing. The business has been running for a number of days, and your friends have recorded their net profit on each day. You want to find the largest total profit that your friends have made during any consecutive time span of at least one day. For example, if your friends' profits looked like this:


    Day 1: -3

    Day 2: 4

    Day 3: 9

    Day 4: -2

    Day 5: -5

    Day 6: 8


    Their maximum profit over any span would be 14, from days 2 to 6.

    Input

    There will be several test cases in the input. Each test case will begin with an integer N(1$ le$N$ le$250, 000) on its own line, indicating the number of days. On each of the next N lines will be a single integer P(- 100$ le$P$ le$100), indicating the profit for that day. The days are specified in order. The input will end with a line with a single 0.

    Output

    For each test case, output a single integer, representing the maximum profit over any non-empty span of time. Print each integer on its own line with no spaces. Do not print any blank lines between answers.

    Sample Input

    6 
    -3 
    4 
    9 
    -2 
    -5 
    8 
    2 
    -100
    -19 
    0
    

    Sample Output

    14 
    -19

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 100001
    const int inf=0x7fffffff;   //无限大
    
    int main()
    {
        sspeed;
        int n;
        while(cin>>n)
        {
            if(n==0)
                break;
            ll sum=0;
            ll ans=0;
            ll a;
            ll k=0;
            ll kiss=-maxn;
            int flag=0;
            for(int i=0;i<n;i++)
            {
                cin>>a;
                if(a>0)
                {
                    flag=1;
                }
                else
                {
                    kiss=max(kiss,a);
                }
                sum+=a;
                sum=max(sum,k);
                ans=max(ans,sum);
            }
            if(flag==0)
                cout<<kiss<<endl;
            else
                cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    爱普生L4168打印出来是白纸,复印OK,打印机测试也OK 解决方案
    json序列化对象
    "割裂"的西安
    资金投资心得
    【练内功,促成长】算法学习(3) 二分查找
    在ReactNative中实现Portal
    node创建GIT分支,并修改代码提交
    关于"三分钟热度"问题的思考
    参考vue-cli实现自己的命令行工具(demo)
    【练内功,促成长】算法学习(2) 排序算法
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4238208.html
Copyright © 2011-2022 走看看