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 day was. 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.
The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.
The first line of the input file contains n - the number of days of Bill‘s life he is planning to investigate (1n100000) . 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.
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
On the first line of the output file print the greatest value of some period of Bill‘s life.
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 the shortest one. If there are still several possibilities, print the one that occurs first..
6 3 1 6 4 5 2
60 3 5
枚举每个值当作区间最小值时求出最大区间,然后枚举每种情况求出最大值。
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <cmath> #include <functional> using namespace std; typedef long long LL; const int maxn = 100005; int n, a[maxn], L[maxn], R[maxn]; // L[i]表示i左边小于第i个元素的第一个元素的位置 // R[i]表示i右边小于第i个元素的第一个元素的位置 LL sum[maxn]; // sum[i]表示前i项和 stack<int> q; void init() { sum[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } a[0] = a[n + 1] = -0x3f3f3f3f; while (!q.empty()) { q.pop(); } q.push(0); for (int i = 1; i <= n; i++) { while (a[q.top()] >= a[i]) { q.pop(); } L[i] = q.top(); q.push(i); } while (!q.empty()) { q.pop(); } q.push(n + 1); for (int i = n; i; i--) { while (a[q.top()] >= a[i]) { q.pop(); } R[i] = q.top(); q.push(i); } } int main() { ios::sync_with_stdio(false); int kase = 0; while (cin >> n) { if (kase++) { cout << endl; } init(); LL ans = 0; int l, r; for (int i = 1; i <= n; i++) { LL t = a[i] * (sum[R[i] - 1] - sum[L[i]]); if (t > ans) { ans = t; l = L[i] + 1; r = R[i] - 1; } else if (t == ans && R[i] - L[i] - 1 < r - l + 1) { l = L[i] + 1; r = R[i] - 1; } } if (ans == 0) { // 特殊情况 cout << "0\n1 1\n"; } else { cout << ans << endl << l << ' ' << r << endl; } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zyq522376829/article/details/46652805