• POJ——T 2796 Feel Good


    http://poj.org/problem?id=2796

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 15375   Accepted: 4252
    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 Output

    60
    3 5

    Source

     
    处理前缀和以及当前点向左最大数的位置和向右最大数的位置
     1 #include <algorithm>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 #define LL long long
     8 const int N(100000+5);
     9 int ans_l,ans_r,l[N],r[N];
    10 LL n,ans_val=-1,sum[N],val[N];
    11 
    12 inline void read(LL &x)
    13 {
    14     x=0; LL ch=getchar();
    15     for(;ch>'9'||ch<'0';) ch=getchar();
    16     for(;ch>='0'&&ch<='9';ch=getchar()) x=ch-'0'+x*10;
    17 }
    18 
    19 int main()
    20 {
    21     read(n);
    22     for(int i=1;i<=n;i++)
    23     {
    24         read(val[i]),l[i]=r[i]=i;
    25         sum[i]=sum[i-1]+val[i];
    26     }
    27     for(int i=2;i<=n;i++)
    28       for(;l[i]>1&&val[l[i]-1]>=val[i];)
    29         l[i]=l[l[i]-1];
    30     for(int i=n-1;i>=1;i--)
    31       for(;r[i]<n&&val[r[i]+1]>=val[i];)
    32         r[i]=r[r[i]+1];
    33     for(int i=1;i<=n;i++)
    34     {
    35         LL tmp=val[i]*(sum[r[i]]-sum[l[i]-1]);
    36         if(tmp>ans_val)
    37         {
    38             ans_l=l[i];
    39             ans_r=r[i];
    40             ans_val=tmp;
    41         }
    42     }
    43     printf("%I64d
    %d %d",ans_val,ans_l,ans_r);
    44     return 0;
    45 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    s4-9 二层设备
    s4-9 二层设备
    s5-1 网络层引言
    LeetCode Factorial Trailing Zeroes (阶乘后缀零)
    UVA 658 It's not a Bug, it's a Feature! (最短路,经典)
    UVA 1151 Buy or Build (MST最小生成树,kruscal,变形)
    LeetCode Reverse Linked List (反置链表)
    LeetCode Contains Duplicate (判断重复元素)
    UVA 1395 Slim Span (最小生成树,MST,kruscal)
    割点,桥,边双连通分量,点双连通分量
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7391057.html
Copyright © 2020-2023  润新知