UVa - 1619 - Feel Good
来源:互联网 发布:淘宝买家好评率99% 编辑:程序博客网 时间:2024/06/15 00:53
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.
Input
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.
Output
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..
Sample Input
6 3 1 6 4 5 2
Sample Output
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;}
- UVa 1619 Feel Good
- UVa - 1619 - Feel Good
- uva 1619 - Feel Good
- UVA 1619 Feel Good
- Feel Good - UVa 1619 dp
- UVA 1619 Feel Good(DP)
- UVA 1619(p256)----Feel Good
- 【UVA】1619-Feel Good(数据结构-栈)
- UVA - 1619 Feel Good 标记+枚举
- UVa 1619:Feel Good(单调栈)
- Feel Good UVA
- UVA - 1619 Feel Good(高效算法:滑动窗口)
- UVA 1619Feel Good 感觉不错 迭代法 (2005 NE)
- [UVa 1619] Feel Good (单调栈+前缀和)
- Feel Good
- Feel Good
- Feel Good
- Feel Good
- java设计模式之——代理模式
- 【密钥算法】Java加密技术(七)---ECC 数据加密算法
- 开贴搞航模
- 【密钥算法】Java加密技术(八)---Java证书体系
- Xcode and #pragma mark
- UVa - 1619 - Feel Good
- 常用的JQuery数字类型验证正则表达式整理
- 第十六周oj刷题——Problem C: B 求类中数据成员的最大值-类模板
- 图文理解css浮动
- 怎么把一个硬件的驱动程序打包到自己的应用程序的安装程序里
- 防范用户通过篡改 文件后缀名的方式欺骗服务器,使用二进制流的方式读取文件头文件,将头文件转换为16 进制
- 【shell脚本】mysql每日备份shell脚本
- 调用css时,用link 和 @import url 有什么区别
- 为什么接口中的常量必须使用public static final修饰