UVA 1619 Feel Good 线段树二分

题目链接:UVA 1619 Feel Good

题目

Description

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 (1 ≤ n ≤ 100000). 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 caseswill 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 any one of them.

Sample Input

6
3 1 6 4 5 2

Sample Output

60
3 5

解析

首先,题目里有个很好的条件:数组均不为负。这就带来了一个很好的性质:区间 A \mathbf A A ⊂ \subset 区间 B \mathbf B B ⇒ s u m ( A ) ≤ s u m ( B ) \Rightarrow sum(A)\leq sum(B) sum(A)sum(B)
其次,假设区间 A \mathbf A A的最小值在下标 d d d处,那么 ∀ X ⊂ A ⇒ m i n ( X ) = n u m [ d ] \forall \mathbf X \subset \mathbf A \Rightarrow min(X) = num[d] XAmin(X)=num[d]
有了以上两个性质,我们就可以得出以下结论,对于最小值下标相同的两个区间,一定是包含另一段的区间和比较大。那么我们可以每次找到最小值的位置 d d d,然后对整个区间求和,即为最大值。那么然后呢?毕竟不一定选择整个区间最优。那在第二个推论下我们知道,当且仅当选择的区间不包含 d d d时最小值可能会发生变化,那么我们可以以 d d d为轴,将区间分成左右两段,这两段都不包含 d d d,然后对两个区间重复上述操作,并与结果进行比较即可。
上述过程需要查询区间最小值所在下标以及区间和,所以开一个线段树维护一下即可。
AC代码:

#include <cstdio>
#include <algorithm>
using namespace std;

#define ll long long
const int maxn = 1e5 + 10;
struct node{
	int dex;
	ll sm;
} tree[maxn*8];
int e[maxn], n;
int rl, rr;
ll res;

void build(int l, int r, int dex)
{
	if(l == r) tree[dex].dex = l, tree[dex].sm = (ll)e[l];
	else{
		int mid = (l+r) >> 1;
		build(l, mid, dex*2), build(mid+1, r, dex*2+1);
		if(e[tree[2*dex].dex] > e[tree[2*dex+1].dex]) tree[dex].dex = tree[2*dex+1].dex;
		else tree[dex].dex = tree[dex*2].dex;
		tree[dex].sm = tree[dex*2].sm + tree[dex*2+1].sm;
	}
}
ll sm_query(int l, int r, int dex, int x, int y)
{
	if(l >= x && r <= y) return tree[dex].sm;
	else if(l > y || r < x) return 0;
	else{
		int mid = (l+r) >> 1;
		return sm_query(l, mid, dex*2, x, y)+sm_query(mid+1, r, dex*2+1, x, y);
	}
}
int m_query(int l, int r, int dex, int x, int y)
{
	if(l >= x && r <= y) return tree[dex].dex;
	else if(l > y || r < x) return 0;
	else{
		int mid = (l+r) >> 1;
		int a = m_query(l, mid, dex*2, x, y), b = m_query(mid+1, r, dex*2+1, x, y);
		if(e[a] > e[b]) return b;
		else return a;
	}
}
void query(int l, int r)
{
	int m = m_query(1, n, 1, l, r);
	ll tmp = (ll)e[m]*sm_query(1, n, 1, l, r);
	if(tmp > res) rl = l, rr = r, res = tmp;
	if(l < m) query(l, m-1);
	if(r > m) query(m+1, r);
}

int main()
{
	int cnt = 0;
	while(scanf("%d", &n) != EOF){
		for(int i = 1; i <= n; i++) scanf("%d", &e[i]);
		build(1, n, 1);
		e[0] = 1e9, res = 0, rl = rr = 1;
		query(1, n);
		if(cnt++) printf("\n");
		printf("%lld\n%d %d\n", res, rl, rr);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值