codeforces 846F Random Query
来源:互联网 发布:淘宝详情页html代码 编辑:程序博客网 时间:2024/05/29 14:20
F. Random Query
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n positive integers. You pick two integer numbers l and r from 1 to n, inclusive (numbers are picked randomly, equiprobably and independently). If l > r, then you swap values of l and r. You have to calculate the expected value of the number of unique elements in segment of the array from index l to index r, inclusive (1-indexed).
Input
The first line contains one integer number n (1 ≤ n ≤ 106). The second line contains n integer numbers a1, a2, ... an (1 ≤ ai ≤ 106) — elements of the array.
Output
Print one number — the expected number of unique elements in chosen segment.
Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 4 — formally, the answer is correct if , where x is jury's answer, and y is your answer.
Examples
input
2
1 2
output
1.500000
input
2
2 2
output
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n positive integers. You pick two integer numbers l and r from 1 to n, inclusive (numbers are picked randomly, equiprobably and independently). If l > r, then you swap values of l and r. You have to calculate the expected value of the number of unique elements in segment of the array from index l to index r, inclusive (1-indexed).
Input
The first line contains one integer number n (1 ≤ n ≤ 106). The second line contains n integer numbers a1, a2, ... an (1 ≤ ai ≤ 106) — elements of the array.
Output
Print one number — the expected number of unique elements in chosen segment.
Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 4 — formally, the answer is correct if , where x is jury's answer, and y is your answer.
Examples
input
2
1 2
output
1.500000
input
2
2 2
output
1.000000
题意:给定一个序列a,求任意选择两个数L,R 求区间(L,R) (L>R则交换) 中出现数的数的种类个数 的期望。
分析: 每一个数ai对答案的贡献值就是 作为ai他是在那些区间是第一次出现的 , 对于 一个数 ai 求 他对那些L,R是有贡献的。我们只要 求出 前一个值为ai的出现位置L[ai],则 他的贡献 有( i-L[ai] ) * ( n-i+1 )*2 - 1
#include <iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<time.h>#include<vector>const int maxn = 1e6;typedef long long ll;using namespace std;ll n;int a[maxn+5];int l[maxn+5];void solve(){ ll cnt; double tot=0; memset(l,0,sizeof(l)); for(int i=1;i<=n;i++) { cnt = (i-l[a[i]])*(n-i+1)*2-1; tot+=cnt*1.0; l[a[i]] = i; } double ans = tot*1.0/n/n; printf("%.6lf\n",ans);}int main(){ while(~scanf("%I64d",&n)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } solve(); } return 0;}
阅读全文
0 0
- codeforces 846F Random Query
- Educational Codeforces Round 28 F. Random Query 期望(贡献思想)
- Educational Codeforces Round 28 Random Query 思维
- Educational Codeforces Round 33 F. Subtree Minimum Query
- codeforces 893F Subtree Minimum Query 线段树合并
- [Codeforces 893F. Subtree Minimum Query]线段树合并
- Codeforces 846F
- PostgreSQL Random Query Tuning
- Codeforces 659F F
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query (线段树)
- 【CODEFORCES】 B. Random Teams
- Codeforces Gym100571A Cursed Query
- Codeforces 234 F. Fence
- 【Codeforces 500F】Dp
- codeforces 234F - Fence
- F-Logo Turtle codeforces
- 【CODEFORCES】 F. Ant colony
- Codeforces 567F
- codeforces 448C 分治
- SGISTL源码探究-第二级配置器
- 解压.tar.xz结尾的文件
- 1对多业务,数据库水平切分架构一次搞定 | 架构师之路
- 批处理实现文本内容居中显示 -shell
- codeforces 846F Random Query
- Codeforces Round #432 (Div. 2) 总结
- 强力推荐!那些你不能错过的 GitHub 插件和工具
- 在Andorid中使用FFmpeg实现YUV解码为BMP
- 准确率,精确率,召回率以及f1
- NYOJ69
- 《UNIX网络编程 卷2》 笔记: 互斥锁与条件变量
- POJ_1015_Jury Compromise
- leetcode 670. Maximum Swap