J - Indifferent
Time limit : 2sec / Memory limit : 256MB
Score : 100 points
Problem Statement
We have 2N pots. The market price of the i-th pot (1≤i≤2N) is pi yen (the currency of Japan).
Now, you and Lunlun the dachshund will alternately take one pot. You will go first, and we will continue until all the pots are taken by you or Lunlun. Since Lunlun does not know the market prices of the pots, she will always choose a pot randomly from the remaining pots with equal probability. You know this behavior of Lunlun, and the market prices of the pots.
Let the sum of the market prices of the pots you take be S yen. Your objective is to maximize the expected value of S. Find the expected value ofS when the optimal strategy is adopted.
Constraints
- 1≤N≤105
- 1≤pi≤2×105
- All input values are integers.
Input
Input is given from Standard Input in the following format:
N p1 : p2N
Output
Print the expected value of S when the strategy to maximize the expected value of S is adopted. The output is considered correct if its absolute or relative error from the judge's output is at most 10−9.
Sample Input 1
1 150000 108
Sample Output 1
150000.0
Naturally, you should choose the 150000 yen pot.
Sample Input 2
2 50000 50000 100000 100000
Sample Output 2
183333.3333333333
First, you will take one of the 100000 yen pots. The other 100000 yen pot will become yours if it is not taken in Lunlun's next turn, with probability2⁄3. If it is taken, you will have to settle for a 50000 yen pot. Thus, the expected value of S when the optimal strategy is adopted is2⁄3×(100000+100000)+1⁄3×(100000+50000)=183333.3333…
题目大意就是:
一共2n个价格pi
(0<i <2n)。
两个人轮流取。
你每次取最大的,对方每次随机取。
问你取的期望和是多少。
分析:首先我们可以发现自己取到最小的一个的概率是0;
还发现我们取到最大的一个的概率为1。(天大的发现。。。)
然后再看一遍题目。。发现每一个数取到的概率其实与数值本身无关,于是,可以把这些数,进行排序,然后重新编号------假设从小到大编号为1~2n;
这么有规律的题目,概率总会有点规律吧。
于是手算n=1,2,3的例子,发现概率真的是一个等差的数列,
1~2n的概率分别为0,1/(2n-1),2/(2n-1),3/(2n-1)......2n-2/2n-1,1
那么到底是不是对的呢?
我们用数学归纳法证明出来,真的是对了(然而我自己好像不太会证,所以偷个懒,就不写了)
///给出数学归纳法的百度百科:https://baike.baidu.com/item/数学归纳法/5155524?fr=aladdin
然后接下来就比较简单了,我们就会有这样的一个式子:
这就是答案的表达式了。
这是普(shen)通(xian)的思路,
然而高(zheng)端(chang)一点的讲解就是标答的讲解了:(你可以百度翻译)
There may be multiple pots with the same market price, but we can safely assume that thereare extremely small differences of prices between them, and no two pots have the same price. The“optimal strategy” in the statement is merely to choose the most expensive remaining pots in each turn.Let qi be the probability that the i-th most expensive pot (1 ≤ i ≤ 2N) becomes ours, and si be theprice of that pot. Here, the answer is ∑1≤i≤2N qisi.In conclusion, qi =2N−i2N−1holds. This can be shown by induction, using the following fact. Let rn,i bethe value of qi when N = n. Then, for n ≥ 2, rn,1 = 1 and rn,i =2N−i2N−1rn−1,i−1 +i−22N−1rn−1,i−2 (i ≥ 2;let rn,0 = 0 (n ≥ 1) for convenience).
于是就有了代码:
#include <bits/stdc++.h>
using namespace std;
int n, p[200020];
double z;
int main() {
scanf("%d", &n);
n *= 2;
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
sort(p, p + n);
for (int i = 0; i < n; i++) {
z += p[i] / (n - 1.) * i;
}
printf("%.12f\n", z);
return 0;
}
其实这完全是一道数学题和概念题。