您的位置:首页 > 运维架构

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

2016-07-11 11:26 302 查看
Feel Good

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 12409Accepted: 3484
Case Time Limit: 1000MSSpecial 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

Northeastern Europe 2005

题意: 给你n个数,求得 max(某个区间和*区间最小值)

题解:此代码只能通过poj测评 .... 坑啊
求得以a[i]为最小值的区间的左界与右界 维护一个单调栈 代码中的*****
这个叫 滑窗算法?
滑动窗口的精髓是在一个数列里保存着一个递增数列,同时这个数列维持了它在原数列的位次递增,这个窗口里保存的第一个数即在这个区间里最小的数。这样不停得把新输入的数同这个滑动窗口里右边的数比较,如果比它大,删除窗口里的这个数,同时删除的数统治区域的最右边就是新输入的数,它的左统治区域即新输入的数的左统治区域,然后不停地向窗口的左边比。这样只需要一个数组记录它左边区域的边界就可以了, 右边同理..

#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
using namespace std;
ll n;
ll a[100005];
ll sum[100005];
ll l[100005],r[100005];
ll ans;
int gg;
int main()
{
gg=0;
while(scanf("%lld",&n)!=EOF)
{
if(gg)
cout<<endl;
gg++;
sum[0]=0;
ans=-1;
int flag=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sum[i]=sum[i-1]+a[i];
}
a[0]=-1;a[n+1]=-1;
l[1]=1;
for(int i=2;i<=n;i++)//关键********
{
int temp=i-1;
while(a[temp]>=a[i])//维护一个递增的序列
temp=l[temp]-1;
l[i]=temp+1;
}
r
=n;
for (int i=n-1;i>=1;i--)
{
int temp=i+1;
while(a[temp]>=a[i])
temp=r[temp]+1;
r[i]=temp-1;
}
for(int i=1;i<=n;i++)
{
ll ggg=(sum[r[i]]-sum[l[i]-1])*a[i];
if(ggg>ans)
flag=i;
ans=max(ans,ggg);
}
printf("%lld\n",ans);
printf("%lld %lld\n",l[flag],r[flag]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: