CS Academy Expected Merge
题目链接:https://csacademy.com/contest/archive/task/expected-merge/statement/
题目大意:Suppose we sort an array of size NN. For each index ii we look at all the function calls that are done for an interval that contain ii. If S_iSi is the sum of lengths of these intervals, find the expected value of all S_iSi.
解题思路:基本上写出来7和9的就能看出来了。可以按照二分一样的方式模拟整个过程然后计算。
代码:
1 const int maxn = 1e5 + 5; 2 int n; 3 typedef vector<double> VD; 4 5 VD cacu(int x){ 6 if(x == 1){ 7 VD ansv; ansv.push_back(1.0); 8 return ansv; 9 } 10 VD vl = cacu(x / 2); 11 VD vr = cacu(x - x / 2); 12 VD va, ansv; 13 for(int i = 0; i < vl.size(); i++) 14 va.push_back(vl[i]); 15 for(int i = 0; i < vr.size(); i++) 16 va.push_back(vr[i]); 17 18 for(int i = 0; i < x; i++){ 19 double tmp = (va[i] + va[x - i - 1]) / 2.0 + x; 20 ansv.push_back(tmp); 21 } 22 return ansv; 23 } 24 void solve(){ 25 VD ansv = cacu(n); 26 for(int i = 0; i < ansv.size(); i++) 27 printf("%.6f ", ansv[i]); 28 puts(""); 29 } 30 int main(){ 31 scanf("%d", &n); 32 solve(); 33 }
题目:
Expected Merge
Memory limit: 256 MB
Consider the merge sort algorithm. It is usually implemented as a recursive function that sorts an interval. We divide the array in two halves, sort them recursively, and then merge the results.
If the interval being sorted has even length, the two halves will have equal size. But if the interval has odd length, then one of the halves will be greater than the other. In this problem we assume we choose uniformly and at random how to make the split.
Suppose we sort an array of size NN. For each index ii we look at all the function calls that are done for an interval that contain ii. If S_iSi is the sum of lengths of these intervals, find the expected value of all S_iSi.
Standard input
The first line contains a single integer NN.
Standard output
Print NN values representing the answer for each index 1 \leq i \leq N1≤i≤N.
Constraints and notes
- 1 \leq N \leq 10^51≤N≤105
- The result is checked with a precision of 10^{-6}10−6
Input | Output | Explanation |
---|---|---|
1 |
1.00 |
|
2 |
3.00 3.00 |
Merge sort only has one way to run. Here are the function calls: (1, 2) (1)(2) Both |
3 |
5.00 6.00 5.00 |
Merge sort function calls will be either: (1, 2, 3) (1, 2)(3) (1)(2) or: (1, 2, 3) (1)(2, 3) (2)(3) |