UVa - 1619 - Feel Good

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 a1a2,..., 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-24 19:35:23

UVa - 1619 - Feel Good的相关文章

UVA 1619 Feel Good(DP)

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-negat

uva 1619 - Feel Good 一个技巧

1619 - Feel Good Time limit: 3.000 seconds Bill is developing a new mathematical theory for human emotions. His recent investigations are dedi- cated to studying how good or bad days in uent people's memories about some period of life. A new idea Bil

UVA 1619 Feel Good 感觉不错 (扫描法)

Feel Good Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu 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 a

UVA - 1619 Feel Good 标记+枚举

题目大意:给出n个正整数的序列,要求你找出符合sum(a1 + a2 + a3 + - + an) * min(a1,a2,-,an)的最大值,如果有多个符合条件的,输出最短序列 解题思路:从输出中观察得到,输出的答案要有三个,最大值,左端点,右端点. 那就设两个数组,纪录已当前数位最小值所能覆盖的最大区间,然后枚举每个数,求出区间和再乘上当前数求得值再进行比较 设置左右区间时,可以递归比较,假设l[i]纪录的时ai所能覆盖的最左端点,如果aj <= ai (j > i),那么l[j] = l

UVA 1619/POJ2796 滑窗算法/维护一个单调栈

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12409   Accepted: 3484 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated

【习题 8-18 UVA - 1619】Feel Good

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用单调队列求出l[i]和r[i] 分别表示i的左边最近的大于a[i]的数的位置以及i右边最近的大于a[i]的数的位置. 则l[i]+1..r[i]-1就是a[i]这个数作为最小数的最大管辖区间了. 写个前缀和就好. 然后取a[i]*区间l[i]+1..r[i]-1的和 中的最大值. 并不是special judge 多个相同区间. 取区间长度最短的区间. 如果仍有多个答案. 取左端点最小的那个区间. (全都是0的情况要注意,直接

UVA - 1619 Feel Good(扫描法)

题目: 思路: 预处理出a[i]在哪个范围区间内是最小的,然后直接遍历a数组求答案就可以了. 这个预处理的技巧巧妙的用了之前的处理结果.(大佬tql) 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define MAX 1e3 #define FRE() freopen("in.txt","r",stdin) #define FRO() freopen("out.txt",&

HDU 1619 &amp; UVA 116 Unidirectional TSP(树形dp,入门 , 数塔变形)

Unidirectional TSP Description Background Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Travelin

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d