UVA 1619/POJ2796 滑窗算法/维护一个单调栈

Feel Good
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 12409   Accepted: 3484
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

 
题意: 给你n个数,求得 max(某个区间和*区间最小值)
 
题解:此代码只能通过poj测评 .... 坑啊
         求得以a[i]为最小值的区间的左界与右界  维护一个单调栈  代码中的*****
这个叫 滑窗算法?
滑动窗口的精髓是在一个数列里保存着一个递增数列,同时这个数列维持了它在原数列的位次递增,这个窗口里保存的第一个数即在这个区间里最小的数。这样不停得把新输入的数同这个滑动窗口里右边的数比较,如果比它大,删除窗口里的这个数,同时删除的数统治区域的最右边就是新输入的数,它的左统治区域即新输入的数的左统治区域,然后不停地向窗口的左边比。这样只需要一个数组记录它左边区域的边界就可以了,  右边同理..
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define ll long long
 5 using namespace std;
 6 ll n;
 7 ll a[100005];
 8 ll sum[100005];
 9 ll l[100005],r[100005];
10 ll ans;
11 int gg;
12 int main()
13 {
14     gg=0;
15     while(scanf("%lld",&n)!=EOF)
16     {
17         if(gg)
18         cout<<endl;
19         gg++;
20     sum[0]=0;
21     ans=-1;
22     int flag=0;
23     for(int i=1;i<=n;i++)
24     {
25         scanf("%lld",&a[i]);
26         sum[i]=sum[i-1]+a[i];
27     }
28     a[0]=-1;a[n+1]=-1;
29     l[1]=1;
30     for(int i=2;i<=n;i++)//关键********
31     {
32         int temp=i-1;
33         while(a[temp]>=a[i])//维护一个递增的序列
34             temp=l[temp]-1;
35         l[i]=temp+1;
36     }
37     r[n]=n;
38     for (int i=n-1;i>=1;i--)
39     {
40         int temp=i+1;
41         while(a[temp]>=a[i])
42             temp=r[temp]+1;
43         r[i]=temp-1;
44     }
45     for(int i=1;i<=n;i++)
46     {
47         ll ggg=(sum[r[i]]-sum[l[i]-1])*a[i];
48         if(ggg>ans)
49             flag=i;
50         ans=max(ans,ggg);
51     }
52     printf("%lld
",ans);
53     printf("%lld %lld
",l[flag],r[flag]);
54     }
55     return 0;
56 }
原文地址:https://www.cnblogs.com/hsd-/p/5659673.html