题解:P10668 BZOJ2720 [Violet 5] 列队春游
[列队春游] 题解
题意
给定整数序列
对于位置
数据范围:
题解
设
这等价于求:
枚举小于
其中
通过古典概型的方法求出
因此:
对于每个数分别求出
算法步骤
- 读入
n 和数组a - 对
a 排序,得到每个a_i 的排名,从而得到s_i (严格小于a_i 的数的个数) - 计算
\sum_{i=1}^n \left(1 + \frac{s_i}{n-s_i+1}\right) - 输出结果
复杂度分析
- 时间复杂度:
O(n \log n) - 空间复杂度:
O(n)
代码实现要点
- 注意处理重复元素的情况
- 使用双精度浮点数计算期望值并且输出时保留两位小数
CODE
#include<bits/stdc++.h> using namespace std; int n,h[305]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for(int i=1;i<=n;i++) cin>>h[i]; sort(h+1,h+1+n); long double ans=0,pre=0; for(int i=1;i<=n;i++){ if(h[i]!=h[i-1]) pre=i-1; ans+=pre/(n-pre+1); ans++; } cout<<fixed<<setprecision(2)<<ans; return 0; }