D. Drunk Passenger
链接: 2021icpctaiwan.
Due to COVID-19, social distancing is applied in our daily life to prevent the spread of the disease. It changes our living styles a lot, especially the way of traveling. Now, many carriers cancel non-reserved seats and introduce seating rules to ensure that the distance between any two passengers is long enough.
On your trip to the 2022 ICPC World Finals, you take a flight. The airplane provides n reserved seats to n passengers. The passengers must queue up first; then they board the airplane one by one. You are the last passenger to board, since you are at the end of the queue. Unfortunately, the first passenger is drunk. The drunk passenger randomly goes to another passenger’s seat and then sits there. You may assume the following.
The drunk passenger never takes their own seat.
The probability of any other seat taken by the drunk passenger is uniform.
Luckily, all the other passengers are not drunk. However, they don’t want to move any passenger from a taken seat. If a passenger’s seat has been taken by another passenger when boarding, the passenger would randomly take a vacant seat with equal probability. Otherwise, the passengers just take their own seats.
Please write a program to compute the probability that your seat is taken by another passenger.
思路
如果给1规定随便坐,可以到1号桌子,那么n号就有1/2概率做到座位上
现在规定做不到座位上,我们可以从上面的式子知道,每一层的概率
(死活,意义不大)都是1/2,因为是随机嘛
然后第一层虚线箭头走不通,那么第一层到达5的死路就没有到达1的
虚线对冲,这个算特殊,
剩下其他的就是不能选择父节点已经选择的点,情况和null一样,我们
通过随便写两个样例得知,除了一和n之外,每一个的都是对称的,
也就是说醉汉选择了2~n-1 ,n能够活下来的情况是一半,那么总
的为 1/2 * ((n-2) /n-1))
再加上如果醉汉选择了n点,n必坐不上他自己的座位
所以结果就是 1/(n -1) + 1/2 * ((n-2) /n-1)) = n/(2*(n-1)
AC
#include <bits/stdc++.h>
using namespace std;
#define db double
int main()
{
int n;
cin>>n;
db ans = n*1.0/(2*(n-1));
printf("%.8lf",ans);
}