hdu5805 NanoApe Loves Sequence 线段树 or 直接扫描

13 篇文章 0 订阅
12 篇文章 0 订阅

NanoApe Loves Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 48    Accepted Submission(s): 23


Problem Description
NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entrance Examination!

In math class, NanoApe picked up sequences once again. He wrote down a sequence with  n  numbers on the paper and then randomly deleted a number in the sequence. After that, he calculated the maximum absolute value of the difference of each two adjacent remained numbers, denoted as  F .

Now he wants to know the expected value of  F , if he deleted each number with equal probability.
 

Input
The first line of the input contains an integer  T , denoting the number of test cases.

In each test case, the first line of the input contains an integer  n , denoting the length of the original sequence.

The second line of the input contains  n  integers  A1,A2,...,An , denoting the elements of the sequence.

1T10, 3n100000, 1Ai109
 

Output
For each test case, print a line with one integer, denoting the answer.

In order to prevent using float number, you should print the answer multiplied by  n .
 

Sample Input
  
  
1 4 1 2 3 4
 

Sample Output
  
  
6
 

Source

将绝对值存进b数组,然后根据b数组建立线段树,查询去除这个数之后左右区间最大绝对值(特判两端的),然后和去掉一个数之后这个位置的新的绝对值比较,每次累加较大的


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

using namespace std;

typedef long long ll;
const ll maxn = 100005;
ll sum[maxn << 2], a[maxn], b[maxn], len, cb;
ll T, n;

void PushUP(ll rt) {
	sum[rt] = max(sum[rt << 1], sum[rt << 1 | 1]);
}

void build(ll l, ll r, ll rt) {
	if (l == r) {
		if (cb + 1 <= n) {
			sum[rt] = b[++cb];
		}
		return;
	}
	ll m = (l + r) >> 1;
	build(lson);
	build(rson);
	PushUP(rt);
}

ll query(ll L, ll R, ll l, ll r, ll rt) {
	if (L <= l && r <= R) {
		return sum[rt];
	}
	ll m = (l + r) >> 1;
	ll ret = 0;
	if (L <= m) ret = max(ret, query(L, R, lson));
	if (R > m) ret = max(ret, query(L, R, rson));
	return ret;
}

int main()
{
	cin >> T;
	while (T--) {
		scanf("%I64d", &n);
		for (ll i = 1; i <= n; i++) {
			scanf("%I64d", &a[i]);
		}
		cb = len = 0;
		for (ll i = 2; i <= n; i++) {
			b[++len] = abs(a[i] - a[i - 1]);
		}
		build(1, n - 1, 1);
		ll maxx, ans = 0;
		for (ll i = 1; i <= n; i++) {
			maxx = -1;
			if (i == 1) {
				maxx = query(2, n - 1, 1, n - 1, 1);
			}
			else if (i == 2) {
				maxx = query(i + 1, n - 1, 1, n - 1, 1);
				maxx = max(maxx, abs(a[i + 1] - a[i - 1]));
			}
			else if (i == n) {
				maxx = query(1, n - 2, 1, n - 1, 1);
			}
			else if (i == n - 1) {
				maxx = max(maxx, query(1, i - 2, 1, n - 1, 1));
				maxx = max(maxx, abs(a[i + 1] - a[i - 1]));
			}
			else {
				maxx = max(maxx, query(1, i - 2, 1, n - 1, 1));
				maxx = max(maxx, query(i + 1, n - 1, 1, n - 1, 1));
				maxx = max(maxx, abs(a[i + 1] - a[i - 1]));
			}
			ans += maxx;
		}
		printf("%I64d\n", ans);
	}
	return 0;
}


其实直接分情况扫几遍就行了......傻逼了


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long ll;
const ll maxn = 100000;
ll a[maxn + 5], n, maxx, sum, i1, i2;

int main()
{
	int T;
	cin >> T;
	while (T--) {
		scanf("%I64d", &n);
		for (ll i = 0; i < n; i++) {
			scanf("%I64d", &a[i]);
		}
		maxx = -1;
		for (ll i = 1; i < n; i++) {
			if (abs(a[i] - a[i - 1]) > maxx) {
				i1 = i - 1, i2 = i;
				maxx = abs(a[i] - a[i - 1]);
			}
		}
		sum = 0;
		for (ll i = 0; i < n; i++) {
			ll tmax = -1;
			if (i == 0) {
				for (ll j = 2; j < n; j++) {
					tmax = max(tmax, abs(a[j] - a[j - 1]));
				}
				sum += tmax;
				continue;
			} else if (i == n - 1) {
				for (ll j = 1; j < n - 1; j++) {
					tmax = max(tmax, abs(a[j] - a[j - 1]));
				}
				sum += tmax;
				continue;
			}
			if ((i == i1 && i1 != 0) || (i == i2 && i2 != n - 1)) {
				for (ll j = 1; j < i; j++) {
					tmax = max(tmax, abs(a[j] - a[j - 1]));
				}
				for (ll j = i + 2; j < n; j++) {
					tmax = max(tmax, abs(a[j] - a[j - 1]));
				}
				tmax = max(tmax, abs(a[i + 1] - a[i - 1])); //debug
				sum += tmax;
				continue;
			} 
			tmax = max(maxx, abs(a[i + 1] - a[i - 1]));
			sum += tmax;
		}
		printf("%I64d\n", sum);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值