• Feel Good


    Feel Good

    Time Limit: 3000MS           Memory Limit: 65536KTotal Submissions: 11626           Accepted: 3212Case Time Limit: 1000MS           Special Judge
    Description
    Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life.

    A new idea Bill has recently developed assigns a non-negative integer value to each day of human life.

    Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day.

    Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.

    Input
    The first line of the input contains n - the number of days of Bill's life he is planning to investigate(1 <= n <= 100 000). The rest of the file contains n integer numbers a1, a2, ... an ranging from 0 to 106 - the emotional values of the days. Numbers are separated by spaces and/or line breaks.

    Output
    Print the greatest value of some period of Bill's life in the first line. And on the second line print two numbers l and r such that the period from l-th to r-th day of Bill's life(inclusive) has the greatest possible value. If there are multiple periods with the greatest possible value,then print any one of them.

    Sample Input

    6
    3 1 6 4 5 2

    Sample Output

    60
    3 5
    题意:给出一个序列,要求的是一个区间,这个区间的最小值乘以这个区间数字的和 是最大值。求这个最大值与这个区间的位置。

     

    单调栈

     

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<stack>
    #define ll long long
    using namespace std;
    ll l[500005], r[500005], a[500005], sum[500005];
    ll n,mx,l_pos,r_pos;
    int main()
    {
        freopen("joseph.in","r",stdin);
        freopen("feelgood.out","w",stdout);
        while (~scanf("%lld", &n))
        {
            for (int i = 0; i < n; i++)//刚开始是从1开始输入的,然后前缀和也是从1开始处理的,一位可以简单一点,但是因为
            {                          //栈是从0开始处理的,下面的a[p.top()]可能为a[0],导致一直TLE,找这个错误到自闭。。。
                scanf("%lld", &a[i]);
            }
            sum[0] = a[0];
            for (int i = 1; i < n; i++)
            {
                sum[i] = sum[i - 1] + a[i];
            }
            stack<ll>p;
            for (int i = 0; i < n; i++)
            {
                while (!p.empty() && a[p.top()] >= a[i])//按照大于等于a[i]的规则,从a[i]开始,向左边可以延伸最远的数的下标
                    p.pop();
                if (p.empty())//说明a[i]左边的所有数都大于等于a[i]
                    l[i] = 0;
                else//找到就记录从a[i]开始向左边寻找大于等于a[i]的最大边界下标
                    l[i] = p.top() + 1;
                p.push(i);
            }
            while (!p.empty())
                p.pop();
            for (int i = n - 1; i >= 0; i--)//往右边找第一个比a[i]小的数
            {
                while (!p.empty() && a[p.top()] >= a[i])
                    p.pop();
                if (p.empty())
                    r[i] = n - 1;
                else
                    r[i] = p.top() - 1;
                p.push(i);
            }
            mx = 0;
            for (int i = 0; i < n; i++)
            {
                if (a[i] >= 0)
                {
                    if(mx<a[i] * (sum[r[i]] - sum[l[i]] + a[l[i]]))
                    {
                        mx=a[i] * (sum[r[i]] - sum[l[i]] + a[l[i]]);
                        l_pos=l[i];
                        r_pos=r[i];
                    }
                }
                
            }
    
            printf("%lld
    ", mx);
            printf("%lld %lld
    ",l_pos+1,r_pos+1);
        }
        return 0;
    }

     

     

  • 相关阅读:
    asp.net过滤数据中有异常数据字符串
    微信内置浏览器的 User Agent的判断
    最近突然想了很久还是开博每天写点什么
    Sonar-scanner 插件配置应用
    存clob的值
    动态代理
    在oracle函数中不可直接将变量作为sql语句中的参数
    按照行、列进行统计(按两个维度进行统计)
    查询关联不上的数据,三张表查询
    前台页面——js/jq循环
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11255263.html
Copyright © 2020-2023  润新知