zoukankan      html  css  js  c++  java
  • POJ 2796 / UVA 1619 Feel Good 扫描法

    Feel Good
     

    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

    题意:

      给你n个数,让你找到一个区间l,r使得(a[l] + a[l+1] + ......... + a[r-1]+a[r]) *min{a[l].....a[r]} 的值最大

    题解:

      我们预处理出对于每个i位置,以他为最小值时 向左向右能延伸的最远位值

      答案就是扫一遍了,看看以哪个位置为最小的答案最大

      在POJ上交比较好

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include<vector>
    #include<map>
    #include<queue>
    using namespace std;
    const int N = 1e5+10, M = 30005, mod = 1000000007, inf = 1e9+8;
    
    typedef long long ll;
    const ll mm = 1e14;
    int n;
    ll a[N],l[N],r[N];
    ll sum[N];
    int main() {
        int T = 0;
        while(scanf("%d",&n)!=EOF) {
        if(T) cout<<endl;      T++;
        sum[0] = 0;
             ll mi = mm;
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]) ,sum[i] = sum[i-1] + a[i] ,mi = min(mi,a[i]);
        l[1] = 1;a[0] = -1;a[n+1] = -1;
        for(int i=2;i<=n;i++) {
            int tmp = i-1;
            while(a[i]<=a[tmp]) tmp = l[tmp]-1;
            l[i] = tmp+1;
        }
        r[n] = n;
        for(int i=n-1;i>=1;i--) {
             int tmp = i+1;
            while(a[i]<=a[tmp]) tmp = r[tmp] + 1;
            r[i] = tmp - 1;
        }
        ll  L = 1,R = n;
        ll  mx = sum[n]*mi;
        for(int i=1;i<=n;i++) {
            ll now = (sum[r[i]] - sum[l[i]-1])*1ll*a[i];
            if(now>mx) {
                L = l[i]; R = r[i];
                mx = now;
            }
        }
        printf("%lld
    ",mx);
        printf("%lld %lld
    ",L,R);
        }
    
        return 0;
    }
  • 相关阅读:
    在网页中实现截屏粘贴的功能
    CSS3 @font-face 做自定义图标
    Visual Studio报错一箩筐(持续更新)
    Axure实现vcg官网首页原型图
    Axure实现bootstrap首页线框图
    Web第一天——准备篇
    vue动态加载组件
    组件封装之将代码放到npm上
    node连接mysql生成接口,vue通过接口实现数据的增删改查(二)
    autoCAD2007 快捷键 标注
  • 原文地址:https://www.cnblogs.com/zxhl/p/5414443.html
Copyright © 2011-2022 走看看