Uva 1609 Feel Good

题面:给出长度为n的数列,然后算出其区间和乘区间最小数所能得到的最大值,并且输出区间

样例输入:

6

3 1 6 4 5 2

 

样例输出:

60

3 5

原题链接:https://vjudge.net/problem/UVA-1619


 

分析:

这里有两种算法,一种是O(nlogn)的,用st表+递归,另一种是O(n)的,用单调栈。

容易知道对于数列中的每一个数作为相应区间最小值时,虽然这个相应区间不一定唯一的,但是这个最大区间和一定是唯一的。

举个栗子:

对于数列{0, 0, 0, 0, 0}来说,我们不管是选择哪一个元素作为区间最小值,其相应的答案区间都有多种可能,但最大区间和肯定都是0(你选取第一个0作为区间最小值, 那区间可以是[1, 1],[1, 2],[1, 3]等等,但是区间和最大值肯定都是0)。

我们看原题的话就会发现原题说明是关于这个区间的输出,我们输出其中合法的任何一个就行了。

但是!但是!其实这是在坑你!表面上是spj,实际上并不是spj!对于每一个给定的数据,其输出的区间也必须也是要跟他给的一毛一样才行orz,具体的潜规则我也不是很清楚,反正用单调栈才能AC,不过本着真理至上的原则,还是两个算法都讲一遍。

================

st表+递归解法

这种做法是枚举区间,然后通过st表O(1)找出区间的最小值,配合前辍和能直接计算出一个可能答案。

那怎么枚举区间呢?

先说下,st表保存的应该是区间最小值的下标

比较直观的一点就是当我们选取这个数列的最小值作为区间的最小值的时候,这个区间[1, n]肯定是合法的,设这个最小值的位置为k,那么我们可以知道[1, k-1], [k+1, n]这两个区间是另外两个该被枚举的区间,然后找出这两个区间最小值的位置,又能将区间再次分割。。。每次所划分的区间所对应的区间最小值所在的位置都是不一样的,通过这个递归过程,如此一来,我们就得到了一个O(n)枚举区间的方法,之所以这个算法是O(nlogn)只是因为建st表要O(nlogn)罢了,其他操作要么O(n)要么O(1)的,只可惜实际上这道题不是spj,这种方法跟单调栈的做法虽然都是对的,但是所得区间不一定一样,不然数据水点的话说不定也能过(x)。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 
 6 const int N = 1e5 + 10;
 7 int Logn[N];
 8 long long a[N];
 9 long long sum[N]; //前辍和
10 int st[N][30]; //记录区间[l, r]中最小元素下标的st表
11 long long ans; //最终答案
12 int L, R; //最终答案区间
13 
14 void pre() {
15     Logn[1] = 0;
16     for (int i = 2; i < N; i++)
17         Logn[i] = Logn[i / 2] + 1;
18 }
19 
20 void Build_st(int n) //建立st表
21 {
22     int logn = Logn[n];
23     for(int j=1; j<=logn ;j++)
24         for (int i = 1; i + (1 << j) - 1 <= n; i++) {
25             if (a[st[i][j - 1]] < a[st[i + (1 << (j - 1))][j - 1]])
26                 st[i][j] = st[i][j - 1];
27             else st[i][j] = st[i + (1 << (j - 1))][j - 1];
28         }
29 }
30 
31 int Query(int l, int r)
32 {
33     int s = Logn[r - l + 1];
34     
35     if (a[st[l][s]] < a[st[r + 1 - (1 << s)][s]]) return st[l][s];
36     else return st[r + 1 - (1 << s)][s];
37 }
38 
39 void solve(int l, int r) //递归处理
40 {
41     if (l > r) return;
42 
43     int m = Query(l, r);
44     long long res = (sum[r] - sum[l-1]) * a[m];
45     if (res > ans) {
46         ans = res;
47         L = l;
48         R = r;
49     }
50 
51     solve(l, m - 1);
52     solve(m + 1, r);
53 }
54 
55 int main()
56 {
57     //freopen("data.txt", "r", stdin);
58     //freopen("WA.txt", "w", stdout);
59     ios::sync_with_stdio(false);
60     pre();
61     int n;
62     while (cin >> n) {
63         ans = -1;
64 
65         for (int i = 1; i <= n; i++) {
66             cin >> a[i];
67             sum[i] = a[i] + sum[i - 1];
68             st[i][0] = i;
69         }
70 
71         Build_st(n);
72         solve(1, n);
73         cout << ans << endl;
74         cout << L << " " << R << endl;
75         cout << endl;
76     }
77 
78     return 0;
79 }

 

================

单调栈解法

单调栈的话做法就很单纯了,用数组l[i], r[i]分别保存元素a[i]作为区间最小值时所能延伸到的最左端跟最右端,用单调栈扫描两次这两个数组的值就能全部出来了,实际写代码的时候博主写惯了不小心写成了单调队列,不过操作都一样的。。。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<queue>
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 const int Maxn = 1e5 + 10;
 8 ll a[Maxn];
 9 ll l[Maxn];
10 ll r[Maxn];
11 ll sum[Maxn];
12 
13 int main()
14 {
15     int n;
16     bool first = true;
17     while (cin >> n) {
18         if (first) first = false;
19         else cout << endl;
20 
21         for (int i = 1; i <= n; i++) {
22             cin >> a[i];
23             sum[i] = sum[i - 1] + a[i];
24         }
25         
26         deque <ll > rq, lq;
27         for (int i = 1; i <= n; i++) {
28             while (!rq.empty() && a[rq.back()] > a[i]) {
29                 r[rq.back()] = i - 1;
30                 rq.pop_back();
31             }
32 
33             rq.push_back(i);
34         }
35         while (!rq.empty()) {
36             r[rq.front()] = n;
37             rq.pop_front();
38         }
39 
40         for (int i = n; i >= 1; i--) {
41             while (!lq.empty() && a[lq.back()] > a[i]) {
42                 l[lq.back()] = i + 1;
43                 lq.pop_back();
44             }
45 
46             lq.push_back(i);
47         }
48         while (!lq.empty()) {
49             l[lq.front()] = 1;
50             lq.pop_front();
51         }
52 
53         ll ans = 0;
54         ll ans_l = 1, ans_r = 1;
55 
56         for(int i=1; i<=n ;i++)
57             if ((sum[r[i]] - sum[l[i] - 1]) * a[i] > ans) {
58                 ans = (sum[r[i]] - sum[l[i] - 1]) * a[i];
59                 ans_l = l[i];
60                 ans_r = r[i];
61             }
62 
63         cout << ans << endl;
64         cout << ans_l << " " << ans_r << endl;
65     }
66 
67     return 0;
68 }

 

posted @ 2020-02-01 18:26  雾里尘埃  阅读(211)  评论(0编辑  收藏  举报