zoukankan      html  css  js  c++  java
  • 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;
    }

     

     

  • 相关阅读:
    QT 应用程序关闭某个窗口时,关闭打开的所有其他窗口并退出程序 【转】
    XP配置DCOM服务【转】
    Android最佳性能实践(二)——分析内存的使用情况
    Android最佳性能实践(一)——合理管理内存
    快速实现 ListView下拉,图片放大刷新操作
    Android布局实现圆角边框
    android 自定义文字跑马灯 支持拖拽,按住停止滚动,自定义速度
    Android NDK 环境搭建 + 测试例程
    Android -- 桌面悬浮,仿360
    android-async-http AsyncHttpClient介绍
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11255263.html
Copyright © 2011-2022 走看看