HDU-5753-Permutation Bo(找规律)
2016-07-27 11:08
141 查看
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5753
题意:数列c1~cn,
h1~hn为ci的1~n的排列,求ci[hi>hi-1 and hi>hi+1]的期望和。
题解:
根据期望的线性性,我们可以分开考虑每个位置对答案的贡献。
可以发现当ii不在两边的时候和两端有六种大小关系,其中有两种是对答案有贡献的。
那么对答案的贡献就是\frac{c_i}{3}3ci。
在两端的话有两种大小关系,其中有一种对答案有贡献。
那么对答案的贡献就是\frac{c_i}{2}2ci。
复杂度是O(n)O(n)。
注意特判n=1n=1的情况。
#include <bits/stdc++.h>
using namespace std;
double a[1007];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
if(n==1){printf("%.6lf\n",a[1]);continue;}
double ans=(a[1]+a
)/2,temp=0;
for(int i=2;i<n;i++)temp+=a[i];
ans+=temp/3;
printf("%.6lf\n",ans);
}
return 0;
}
/*
4
3 2 4 5
5
3 5 99 32 12
*/
题意:数列c1~cn,
h1~hn为ci的1~n的排列,求ci[hi>hi-1 and hi>hi+1]的期望和。
题解:
根据期望的线性性,我们可以分开考虑每个位置对答案的贡献。
可以发现当ii不在两边的时候和两端有六种大小关系,其中有两种是对答案有贡献的。
那么对答案的贡献就是\frac{c_i}{3}3ci。
在两端的话有两种大小关系,其中有一种对答案有贡献。
那么对答案的贡献就是\frac{c_i}{2}2ci。
复杂度是O(n)O(n)。
注意特判n=1n=1的情况。
#include <bits/stdc++.h>
using namespace std;
double a[1007];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
if(n==1){printf("%.6lf\n",a[1]);continue;}
double ans=(a[1]+a
)/2,temp=0;
for(int i=2;i<n;i++)temp+=a[i];
ans+=temp/3;
printf("%.6lf\n",ans);
}
return 0;
}
/*
4
3 2 4 5
5
3 5 99 32 12
*/
相关文章推荐
- HDU1384 Intervals (差分约束系统)
- QT的一些注意点与方法积累
- Windows 2008 R2 辅助域控制器强制降级
- CCS中CMD文件详解
- AFN框架 之同时兼容Http和Https通讯 配置
- I - Red packet
- 文件系统
- 顺序表应用6:有序顺序表查询
- Eclips加载不了rt.jar下面的sun.net.ftp.FtpClient类,解决方法
- UVA 11234-Expressions
- servlet
- 1分钟完成:linux定时清理日志
- 1092 A+B for Input-Output Practice (IV)
- Mac上面搭建web服务器
- xpath使用
- hdu 1847 Good Luck in CET-4 Everybody! (SG函数入门)
- js性能优化的小知识
- 活动H5 类型总结 啪啪啪哎哟A股你大爷啊
- 顺序表应用5:有序顺序表归并
- JSONModel源代码解析