Permutation Bo

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5753

Description

There are two sequences h1∼hn and c1∼cn. h1∼hn is a permutation of 1∼n. particularly, h0=hn+1=0.

We define the expression [condition] is 1 when condition is True,is 0 when condition is False.

Define the function f(h)=∑ni=1ci[hi>hi−1 and hi>hi+1]

Bo have gotten the value of c1∼cn, and he wants to know the expected value of f(h).

Input

This problem has multi test cases(no more than 12).

For each test case, the first line contains a non-negative integer n(1≤n≤1000), second line contains n non-negative integer ci(0≤ci≤1000).

Output

For each test cases print a decimal - the expectation of f(h).

If the absolute error between your answer and the standard answer is no more than 10−4, your solution will be accepted.

Sample Input

4

3 2 4 5

5

3 5 99 32 12

Sample Output

6.000000

52.833333

Hint

题意

给你c数组,如果h[i]>h[i-1]和h[i]>h[i+1],答案就加上c。

h是1-n的排列,h[0]=0,h[n+1]=0

然后求最后答案的期望是多少

题解:

根据期望的线性性,我们可以分开考虑每个位置对答案的贡献。

可以发现当ii不在两边的时候和两端有六种大小关系,其中有两种是对答案有贡献的。

那么对答案的贡献就是\(\frac{c_i}{3}\)

在两端的话有两种大小关系,其中有一种对答案有贡献。

那么对答案的贡献就是\(\frac{c_i}{2}\)

复杂度是O(n)。

注意特判n=1的情况。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005; int n;
int c[maxn];
void solve(){
for(int i=1;i<=n;i++)
scanf("%d",&c[i]);
if(n==1){
double ans = c[1];
printf("%.12f\n",ans);
return;
}
if(n==2){
double ans = (c[1]+c[2])/2.0;
printf("%.12f\n",ans);
return;
}
double ans = 0;
for(int i=2;i<n;i++)
ans += (c[i])/3.0;
ans+=c[1]/2.0;
ans+=c[n]/2.0;
printf("%.12f\n",ans);
}
int main(){
while(scanf("%d",&n)!=EOF)solve();
return 0;
}

hdu 5753 Permutation Bo 水题的更多相关文章

  1. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  2. hdu 5753 Permutation Bo

    这里是一个比较简单的问题:考虑每个数对和的贡献.先考虑数列两端的值,两端的摆放的值总计有2种,比如左端:0,大,小:0,小,大:有1/2的贡献度.右端同理. 中间的书总计有6种可能.小,中,大.其中有 ...

  3. 【数学】HDU 5753 Permutation Bo

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 题目大意: 两个序列h和c,h为1~n的乱序.h[0]=h[n+1]=0,[A]表示A为真则为 ...

  4. hdu 5752 Sqrt Bo 水题

    Sqrt Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5752 Description Let's define the function f ...

  5. hdu 1106:排序(水题,字符串处理 + 排序)

    排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  6. HDU 4950 Monster (水题)

    Monster 题目链接: http://acm.hust.edu.cn/vjudge/contest/123554#problem/I Description Teacher Mai has a k ...

  7. HDU 4813 Hard Code 水题

    Hard Code Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.act ...

  8. HDU 4593 H - Robot 水题

    H - RobotTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.act ...

  9. HDOJ/HDU 2560 Buildings(嗯~水题)

    Problem Description We divide the HZNU Campus into N*M grids. As you can see from the picture below, ...

随机推荐

  1. bzoj千题计划201:bzoj1820: [JSOI2010]Express Service 快递服务

    http://www.lydsy.com/JudgeOnline/problem.php?id=1820 很容易想到dp[i][a][b][c] 到第i个收件地点,三个司机分别在a,b,c 收件地点的 ...

  2. 在html5 canvas的destination-atop属性的一些奇怪的问题

    最近在整理canvas的时候发现HTML5 Canvas开发详解一个奇怪的属性解释 目标图形是显示在画布上的位图 而原图形是指要回执在画布上的形状 w3school上面是这样说的 destinatio ...

  3. Java使用WebSocket

    网页端的消息推送,一般有以下方式: 轮询方式:客户端定时向服务端发送ajax请求,服务器接收到请求后马上返回消息并关闭连接. 优点:后端程序编写比较容易. 缺点:TCP的建立和关闭操作浪费时间和带宽, ...

  4. CF359B Permutation (构造)

    CF359B Permutation \(solution:\) 作为一道构造题,这题也十分符合构造的一些通性----(找到一些规律,然后无脑循环). 构造一个长度为 \(2n\) 的排列 \(a\) ...

  5. 使用 jquery-autocomplete插件 完成文本框输入自动填充联想效果 解决兼容IE输入中文问题

    项目中有时会用到ajax自动补全查询,就像Google的搜索框中那样,输入汉字或者字母的首个字母,则包含这个汉字或者字母的相关条目会显示出来供用户选择,该插件就是实现这样的功能的.autocomple ...

  6. 【Python】使用Python将Shellcode转换成汇编

    1.介绍 需要多少行代码转换hex成反汇编呢? 多亏了Python的Capstone库,做这件事只需要五行. 在二进制分析中,进行Exploit开发或逆向工程时,需要快速将十六进制的Shellcode ...

  7. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

  8. Tomcat优化步骤【转】

    一.Tomcat的缺省是多少,怎么修改 Tomcat的缺省端口号是8080.修改Tomcat端口号:1.找到Tomcat目录下的conf文件夹2.进入conf文件夹里面找到server.xml文件3. ...

  9. RHEL7 -- 使用Chrony设置时间与时钟服务器同步

    Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确. 它由两个程序组成:chronyd和chronyc. chronyd是一个后台运行的守护进程,用于调整内核 ...

  10. 【前端vue开发】vue开发总结