题目链接:

NanoApe Loves Sequence

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 262144/131072 K (Java/Others)

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.

1≤T≤10, 3≤n≤100000, 1≤Ai≤109

 
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
 
题意:
 
问去掉一个数,相邻数的差最大的那个的期望是多少?
 
思路:
 
本来很简单的,我傻傻的写了线段树;
 
AC代码:
 
/************************************************
┆ ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓   ┏━┛ ┆
┆  ┃   ┃  ┆      
┆  ┃   ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃    ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
//#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=2e3+14;
const double eps=1e-12; int a[N],b[N];
struct node
{
int l,r,s;
}tr[4*N]; void build(int o,int L,int R)
{
tr[o].l=L;tr[o].r=R;
if(L>=R)
{
tr[o].s=b[L];
return ;
}
int mid=(L+R)>>1;
build(2*o,L,mid);
build(2*o+1,mid+1,R);
tr[o].s=max(tr[2*o].s,tr[2*o+1].s);
} void update(int o,int x,int num)
{
if(tr[o].l==tr[o].r&&tr[o].l==x)
{
tr[o].s=num;
return ;
}
int mid=(tr[o].l+tr[o].r)>>1;
if(x<=mid)update(2*o,x,num);
else update(2*o+1,x,num);
tr[o].s=max(tr[2*o+1].s,tr[2*o].s);
}
int query(int o,int L,int R)
{
if(tr[o].l>=L&&tr[o].r<=R)return tr[o].s;
int mid=(tr[o].r+tr[o].l)>>1;
if(R<=mid)return query(2*o,L,R);
else if(L>mid)return query(2*o+1,L,R);
else return max(query(2*o,L,mid),query(2*o+1,mid+1,R));
} int main()
{
int t;
read(t);
while(t--)
{
int n;
read(n);
For(i,1,n)read(a[i]);
For(i,1,n-1)b[i]=abs(a[i+1]-a[i]);
build(1,1,n-1);
LL ans=0;
For(i,1,n)
{
if(i==1)
{
update(1,1,0);
ans=ans+query(1,1,n-1);
update(1,1,b[1]);
}
else if(i==n)
{
update(1,n-1,0);
ans=ans+query(1,1,n-1);
update(1,n-1,b[n-1]);
}
else
{
update(1,i-1,0);
update(1,i,abs(a[i+1]-a[i-1]));
ans=ans+query(1,1,n-1);
update(1,i,b[i]);
update(1,i-1,b[i-1]);
}
}
cout<<ans<<"\n";
}
return 0;
}

  

 

hdu-5805 NanoApe Loves Sequence(线段树+概率期望)的更多相关文章

  1. HDU 5805 NanoApe Loves Sequence (模拟)

    NanoApe Loves Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5805 Description NanoApe, the ...

  2. HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002

    题目:传送门. 题意:题目说的是求期望,其实翻译过来意思就是:一个长度为 n 的数列(n>=3),按顺序删除其中每一个数,每次删除都是建立在最原始数列的基础上进行的,算出每次操作后得到的新数列的 ...

  3. HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)

    先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 #include <iostream> #inc ...

  4. HDU 5805 NanoApe Loves Sequence

    处理出每个位置左边的最大值和右边的最大值.然后就可以o(1)计算去掉某位置的最大值了. #pragma comment(linker, "/STACK:1024000000,10240000 ...

  5. Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) (C++,Java)

    Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) Hdu 5806 题意:给出一个数组,求区间第k大的数大于等于m的区间个数 #include<queue> # ...

  6. 5805 NanoApe Loves Sequence(想法题)

    传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K ( ...

  7. HDU 5806 NanoApe Loves Sequence Ⅱ (模拟)

    NanoApe Loves Sequence Ⅱ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5806 Description NanoApe, t ...

  8. hdu 5828 Rikka with Sequence 线段树

    Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

  9. hdu 4893 Wow! Such Sequence!(线段树)

    题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...

随机推荐

  1. jquery方法

    $.inArray(被判断的量,ArrayName);  如果存在返回索引值,如果不存在返回-1 $.unique() 数组去重   根据去重前后的长度,判断是否有重复 $.each(被遍历的数组,f ...

  2. Java IDL与javaRMI

    Registry registry = LocateRegistry.getRegistry(); registry.rebind(RemoteService.name, stub); Java 平台 ...

  3. python etree解析xml

    # -*- coding:utf-8 -*- #conding:utf-8 __author__ = 'hdfs' ''' 简洁 高效 明了 ElementTree轻量级的 Python 式的 API ...

  4. netback的tasklet调度问题及网卡丢包的简单分析

    近期在万兆网卡上測试,出现了之前千兆网卡没有出现的一个现象,tasklet版本号的netback下,vm进行发包測试,发现vif的interrupt默认绑定在cpu0上,可是vm发包执行时发现host ...

  5. WinDbg调试分析 net站点 CPU100%问题

    WinDbg调试分析 asp.net站点 CPU100%问题 公司为了节省成本,最近有一批服务器降了配置,CPU从8核降到了2核.本身是小站点,访问量也不高,CPU总是会飙到100%而且可以一直持续几 ...

  6. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  7. 转载 ---资深HR告诉你:我如何筛选简历与选择人员的

    资深HR告诉你:我如何筛选简历与选择人员的   有个公司HR看简历 先直接丢掉一半 理由是不要运气不好的应聘者. 当然这可能只是某些HR面对太多的简历产生了偷懒的情绪,但是不论是Manager,亦或是 ...

  8. Linux 开启端口,操作防火墙

    命令行方式:    添加10002端口方法 1.vi etc/sysconfig/iptables 2. 开放端口命令:-A INPUT -p tcp -m state --state NEW -m ...

  9. Loadrunner - Controller - policy - 设置集合点策略

    控制器中设置集合点策略                                                        我们在Virtual User Generator 中回放脚本无法 ...

  10. windowsphone8.1学习笔记之应用数据(二)

    上一篇说了应用数据的应用设置,这篇说说应用文件,应用文件主要分为三种:本地应用文件.漫游应用文件和临时应用文件. 获取根目录方法如下,都是返回一个StorageFolder对象(稍后介绍这个). // ...