# Uva 1609 Feel Good

6

3 1 6 4 5 2

60

3 5

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

st表+递归解法

 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 }

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

 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  雾里尘埃  阅读(...)  评论(...编辑  收藏