[原创]POJ 2796 Feel Good 【单调栈】

2017-01-07 01:11:20 Tabris_ 阅读数:412


博客爬取于2020-06-14 22:42:26
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/54150706


题目链接:http://poj.org/problem?id=2796

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

Northeastern Europe 2005
----------------------------------------------------------------------------------------------------.

题目大意:
给定一个序列,让你求[ijaimina[i,j]\sum_i^ja_i *mina_{[i,j]}]的最大值。

解题思路:
如果暴力求解的话很明显需要O(n2)O(n^2)来解决,但是更明显的就是会超时.
于是换一种想法,由于我是找的单调栈类型题,所以,就没有想算法…

至于单调栈怎么维护,其实很简单了,
于其类似的有单调队列,
单调,单调,所以栈中所有元素都是递增的,
那么我们在维护一个单调栈的时候要将所有的于即将进栈的元素不单调的元素出栈,这样下去就能维护一个单调栈了.
维护单调栈的好处也很明显,能够快速的维护一个区间内的最小值.
这样下来在进行统计的时候就能减少很多不必要的计算.

首先区间和用一个前缀和来表示,将求取区间和的部分降到O(1)O(1),在每一次维护一个元素的时候,采用一个结构体,

1
2
3
4
5
6
struct node
{
int val; //元素的值
int weizhi; //元素在序列中的位置
int l,r; //这个元素能控制的区间
} des[N];

在每一次出栈操作的时候维护一下所需要计算的结果就行.

这个题解介绍的非常详细了,自己写的是在太罗嗦还是删了吧…

并不是很难想,但是注意元素全部为0的时候,初始化ans需要<0

附本题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//#include <bits/stdc++.h>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <vector>
# include <math.h>

using namespace std;

# define INF (~(1<<31))
# define INFLL (~(1ll<<63))
# define pb push_back
# define mp make_pair
# define abs(a) ((a)>0?(a):-(a))
# define lalal puts("*******");
# define s1(x) scanf("%d",&x)
# define Rep(a,b,c) for(int a=(b);a<=(c);a++)
# define Per(a,b,c) for(int a=(b);a>=(c);a--)
# define no puts("NO")

typedef long long int LL ;
typedef unsigned long long int uLL ;

const int MOD = 1e9+7;
const int N = 100000+5;
const double eps = 1e-6;
const double PI = acos(-1.0);
void fre()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
}
/*************************************************/
int a[N];
LL sum[N];

struct node
{
int val;
int weizhi;
int l,r;
} des[N];
int ind;

int main()
{
int n;
while(~s1(n))
{
sum[0]=0;
Rep(i,1,n) s1(a[i]),sum[i]=sum[i-1]+a[i];

ind = 1;
int l,r;
LL ans = -1,tem = -1;
des[1].val = a[1];
des[1].weizhi = 1;
des[1].l = 1;
des[1].r = 1;

Rep(i,2,n)
{
while(ind && a[i]<=des[ind].val)
{
des[ind].r = i-1;

tem = (sum[des[ind].r]-sum[des[ind].l-1])*des[ind].val;
if(tem > ans)
{
ans = tem ;
l = des[ind].l;
r = des[ind].r;
}

ind--;
if(!ind) break;

}

des[++ind].val = a[i];
des[ind].weizhi = i;
des[ind].l = des[ind-1].weizhi+1;
des[ind].r = i;
}

while(ind)
{
des[ind].r = n;

tem = (sum[des[ind].r]-sum[des[ind].l-1])*des[ind].val;
if(tem > ans)
{
ans = tem ;
l = des[ind].l;
r = des[ind].r;
}
ind--;
}
printf("%lld\n%d %d\n",ans,l,r);
}
return 0;
}
/**
Posted by flywarrior at 2012-11-01 20:09:53 on Problem 2796
input
1
0
3
1 2 1
4
1 2 2 1
9
1 1 1 1 1 1 1 2 2
10
102 58 149 52 66 52 105 28 161 111
6
97 111 154 93 72 109
16
1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
6
0 0 0 0 0 0
答案:
0
1 1
4
2 2
8
2 3
11
1 9
30368
1 7
45792
1 6
17
1 16
0
1 1
*/