zoukankan      html  css  js  c++  java
  • C

    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 10 6 - 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

    思路:找到区间的最小值,然后我们想办法去扩大这个区间。如果a[i]的值比最小值大肯定可以,如果比他小就不可以了

    这题要注意他的a[i]值可以是0    淦!

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <stdlib.h>
     5 #include <math.h>
     6 #include <stack>
     7 #include <queue>
     8 
     9 #define INF 0x3f3f3f3f
    10 #define MAXN 100005
    11 #define LL long long
    12 using namespace std;
    13 
    14 int L[MAXN],R[MAXN],a[MAXN];
    15 LL pre[MAXN];
    16 
    17 stack<int> stc;
    18 
    19 int main()
    20 {
    21     int n;
    22     scanf("%d",&n);
    23     pre[0] = 0;
    24     for (int i=1;i<=n;i++)
    25     {
    26         scanf("%d",&a[i]);
    27         pre[i] = pre[i-1]+a[i];
    28     }
    29     a[n+1] = -1;
    30     a[0] = -1;
    31     for (int i=1;i<=n+1;i++)
    32     {
    33         if (stc.empty() || a[i]>=a[stc.top()])
    34             stc.push(i);
    35         else
    36         {
    37             while (!stc.empty() && a[i]<a[stc.top()])
    38             {
    39                 R[stc.top()] = i-1;
    40                 stc.pop();
    41             }
    42             stc.push(i);
    43         }
    44     }
    45     while (!stc.empty())
    46         stc.pop();
    47     for (int i=n;i>=0;i--)
    48     {
    49         if (stc.empty() || a[i]>=a[stc.top()])
    50             stc.push(i);
    51         else
    52         {
    53             while (!stc.empty() && a[i]<a[stc.top()])
    54             {
    55                 L[stc.top()] = i+1;
    56                 stc.pop();
    57             }
    58             stc.push(i);
    59         }
    60     }
    61     LL ans = -1;
    62     int l,r;
    63     for (int i=1;i<=n;i++)
    64     {
    65         if ((pre[R[i]]-pre[L[i]-1])*a[i]>ans)
    66         {
    67             ans = (pre[R[i]]-pre[L[i]-1])*a[i];
    68             l = L[i];
    69             r = R[i];
    70         }
    71     }
    72     printf("%lld
    %d %d",ans,l,r);
    73     return 0;
    74 }
  • 相关阅读:
    GCD
    SQLite
    将博客搬至CSDN
    Extjs 4 总结
    spring mvc 复杂参数注入
    7/12 聊天室结束
    7/10
    7/6一些知识点
    随便写写
    spring boot 入门操作(三)
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11298184.html
Copyright © 2011-2022 走看看