zoukankan      html  css  js  c++  java
  • poj 2796 Feel Good单调栈

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 20408   Accepted: 5632
    Case 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 Output60

    3 5

    题意:给出一个序列,求出一个子序列,使得这个序列中的最小值乘以这个序列的和的值最大。

    奇怪,在poj上用C++交一直TLE,改用G++就AC

    //单调递增栈
    #include<iostream>
    #include<stdio.h>
    #define ll long long
    #include<stack>
    using namespace std;
    stack<ll>p;     //栈里面存的是下标
    ll a[100005],sum[100005];//sum存的是前缀和
    ll n,mx,top,star,end2;
    int main()
    {
        while(~scanf("%lld",&n))
        {
            while(!p.empty())
                p.pop();
            sum[0]=0;
            for(int i=1;i<=n;i++)//注意这里是从i=1开始
            {
                scanf("%lld",&a[i]);
                sum[i]=sum[i-1]+a[i];//前缀和
            }
            a[n+1]=-1;//为找比a[n]小的数准备,因为是递增栈,将a[n+1]设为最小值
            mx=0;
            for(int i=1;i<=n+1;i++)
            {
                if(p.empty()||a[i]>=a[p.top()])//看题目要求是否要严格单调递增,这里只要求递增
                    p.push(i);
                else
                {
                    while(!p.empty()&&a[i]<a[p.top()])//找到第一个小于栈顶元素的数的下标
                    {
                       ll temp;
                        top=p.top();
                        p.pop();//只在在出栈的过程中以a[top]为最小值更新mx、star、end
                       temp=(sum[i-1]-sum[top-1])*a[top];
                        if(temp>=mx)
                        {
                            mx=temp;
                            star=top;
                            end2=i-1;
                        }
                    }
                    p.push(top);//只将延伸到最左端的元素入栈,并且以最左端的元素的!坐标!为起点,找下一个比a[i]大的最长增区间
                    a[top]=a[i];//修改该位置的值为a[i]
                    
                }
            }
            printf("%lld
    ",mx);
            printf("%lld %lld
    ",star,end2);
        }
        return 0;
    
    }

    2019南昌网络赛也有这题,就是范围改了一下,包含了负数,但是难度增加几倍

    https://www.cnblogs.com/-citywall123/p/10792708.html

     
  • 相关阅读:
    bestcoder 48# wyh2000 and a string problem (水题)
    Install OpenCV3.0 on Eclipse
    sql 优化 -- sql中的自定函数
    java基础知识总结1
    【Java】日志知识总结和经常使用组合配置(commons-logging,log4j,slf4j,logback)
    ESLint 配置
    Vue命名规范
    在vue中使用jsx语法
    vue中8种组件通信方式, 值得收藏!
    Vue+Express实现登录状态权限控制
  • 原文地址:https://www.cnblogs.com/-citywall123/p/10770825.html
Copyright © 2011-2022 走看看