HDU 5805 NanoApe Loves Sequence 线段树
来源:互联网 发布:access数据库安装包 编辑:程序博客网 时间:2024/06/09 05:34
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5805
题意:
问题描述
退役狗 NanoApe 滚回去学文化课啦!在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 n 的数列,他又根据心情随便删了一个数,这样他得到了一个新的数列,然后他计算出了所有相邻两数的差的绝对值的最大值。他当然知道这个最大值会随着他删了的数改变而改变,所以他想知道假如全部数被删除的概率是相等的话,差的绝对值的最大值的期望是多少。
输入描述
第一行为一个正整数 T,表示数据组数。每组数据的第一行为一个整数 n。第二行为 n 个整数 Ai,表示这个数列。1≤T≤10, 3≤n≤100000, 1≤Ai≤109
输出描述
对于每组数据输出一行一个数表示答案。为防止精度误差,你需要输出答案乘上 n 后的值。
输入样例
141 2 3 4
输出样例
6
思路:题意其实就是求差值的最大和。用线段树维护差值的最大值,当抽掉某个数时,查询除了跟这个数有关的两个差值之外的差值最大值,再跟抽掉位置的前后位置差的绝对值比较大小,注意特别处理第一个和最后一个
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>#include <cmath>using namespace std;typedef long long ll;const int N = 100010;int n, arr[N], brr[N];struct node{ int l, r, val;}s[N*4];void push_up(int k){ s[k].val = max(s[k<<1].val, s[k<<1|1].val);}void build(int l, int r, int k){ s[k].l = l, s[k].r = r; if(l == r) { s[k].val = brr[l]; return; } int mid = (l + r) >> 1; build(l, mid, k << 1); build(mid + 1, r, k << 1|1); push_up(k);}void update(int l, int r, int val, int k){ if(l <= s[k].l && s[k].r <= r) { s[k].val = val; return; } int mid = (s[k].l + s[k].r) >> 1; if(l <= mid) update(l, r, val, k << 1); if(r > mid) update(l, r, val, k << 1|1); push_up(k);}int query(int l, int r, int k){ if(l <= s[k].l && s[k].r <= r) return s[k].val; int mid = (s[k].l + s[k].r) >> 1; int ans = -1; if(l <= mid) ans = max(ans, query(l, r, k << 1)); if(r > mid) ans = max(ans, query(l, r, k << 1|1)); return ans;}int main(){ int t; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &arr[i]); for(int i = 2; i <= n; i++) brr[i-1] = abs(arr[i] - arr[i-1]); build(1, n - 1, 1); ll sum = 0; for(int i = 1; i <= n; i++) { if(i == 1) { int tmp = query(1, 1, 1); update(1, 1, -1, 1); sum += s[1].val; //printf("%d\n", s[1].val); update(1, 1, tmp, 1); } else if(i == n) { int tmp = query(i-1, i-1, 1); update(i-1, i-1, -1, 1); sum += s[1].val; //printf("%d\n", s[1].val); update(i-1, i-1, tmp, 1); } else { int tmp1 = -1, tmp2 = -1, tmp3 = -1; tmp1 = abs(arr[i+1] - arr[i-1]); if(i - 2 >= 1) tmp2 = query(1, max(1, i-2), 1); if(i + 1 <= n - 1) tmp3 = query(min(i+1, n-1), n - 1, 1); sum += max(tmp3, max(tmp1, tmp2)); } } printf("%lld\n", sum); } return 0;}
0 0
- HDU 5805 NanoApe Loves Sequence 线段树
- (模拟/线段树)HDU - 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805:NanoApe Loves Sequence
- hdu 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 - NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- hdu NanoApe Loves Sequence
- hdu NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence [Ad Hoc]
- Hdu 5805 NanoApe Loves Sequence【思维题】
- HDU 5805 NanoApe Loves Sequence(思维)
- 【HDU】5805 - NanoApe Loves Sequence(模拟)
- HDU 5805 NanoApe Loves Sequence(水~)
- C#中Socket用法,多个聊天和单一聊天。
- 结构体struct 名称定义详解
- switch----java
- 蓝队网络虚拟主机如何设置301重定向
- Android 调去照相程序拍照
- HDU 5805 NanoApe Loves Sequence 线段树
- SpringMVC的maven工程搭建
- dijkstra
- UFLDL multi_classifier_accuracy.m
- poj1511
- Linux 下 DNS 污染解决办法
- [LeetCode]--47. Permutations II
- 转:Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式总结
- 数据库范式详解