https://community.topcoder.com/stat?c=problem_statement&pm=10726
https://apps.topcoder.com/forums/?module=Thread&threadID=671150&start=0
Now let's solve some problems that use these techniques. Let's solve BouncingBalls. For this problem, the number of balls (n<=12) is sufficiently small that we can generate all possible starting states. Each ball can be rolled either left or right, so we have 2^n possible starting states. Let dir[i] be the initial direction given to the i-th ball, 1 for right and -1 for left. A collision will occur between balls k and m if ball k is to the left of ball m (x[k]<x[m]), ball k is rolled right (dir[k]==1), ball m is rolled left (dir[m]==-1) and the distance that each has to roll is not more than the given time T (x[m]-x[k]><=2*T). For each possible starting state, we count the number of possible bounces for every pair of balls k and m. To compute the expected number of bounces we divide this value by the total number of starting states (1<<n).
>
Note that this problem can be solved more easily without iterating over all the subsets. It can be shown that it is enough to add 0.25 for each pair of balls that are close enough. See Egor's solution that implements this idea.
http://vawait.com/2014/03/srm-458/
250:x轴上有N <= 12个点,每个点等概率向正反方向以单位速度移动,所有碰撞都是完全弹性碰撞,即球相撞后各自沿反方向移动,速度大小不变。现在给定时间T,问T秒内发生了几次碰撞。
枚舉方向,然後枚舉兩個點是否能在2T秒內相遇。碰撞之後的狀態相當於擦肩而過。其實只要枚舉兩個點是否相撞最後答案除4就可以了。
https://community.topcoder.com/stat?c=problem_solution&cr=14970299&rd=14180&pm=10726
public double expectedBounces(int[] x, int T) {
int n = x.length;
double ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (Math.abs(x[i] - x[j]) <= 2 * T)
ans += .25;
}
}
return ans;
}
(By changing the order of summation by summing over "i" inside the "k" and "m" loops, it is easy to show that it is enough to add 0.25 for each pair of balls which are close enough.)
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
https://apps.topcoder.com/forums/?module=Thread&threadID=671150&start=0
Now let's solve some problems that use these techniques. Let's solve BouncingBalls. For this problem, the number of balls (n<=12) is sufficiently small that we can generate all possible starting states. Each ball can be rolled either left or right, so we have 2^n possible starting states. Let dir[i] be the initial direction given to the i-th ball, 1 for right and -1 for left. A collision will occur between balls k and m if ball k is to the left of ball m (x[k]<x[m]), ball k is rolled right (dir[k]==1), ball m is rolled left (dir[m]==-1) and the distance that each has to roll is not more than the given time T (x[m]-x[k]><=2*T). For each possible starting state, we count the number of possible bounces for every pair of balls k and m. To compute the expected number of bounces we divide this value by the total number of starting states (1<<n).
>
public class BouncingBalls { public double expectedBounces(int[] x, int T) { int n=x.length; int bounces=0; //iterate over all possible starting states for (int i=0; i<(1<<n); i++) { //construct the current starting state of directions int[] dir=new int[n]; for (int k=0; k<n; k++) { if ((i&(1<<k)) > 0) dir[k]=+1; //roll ball k right else dir[k]=-1; //roll ball k left } for (int k=0; k<n; k++) //left ball for (int m=0; m<n; m++) //right ball if (x[k]<x[m] && dir[k]==1 && dir[m]==-1 && x[m]-x[k] <= 2*T) bounces++; } return bounces*1.0/(1<<n); } }
Note that this problem can be solved more easily without iterating over all the subsets. It can be shown that it is enough to add 0.25 for each pair of balls that are close enough. See Egor's solution that implements this idea.
http://vawait.com/2014/03/srm-458/
250:x轴上有N <= 12个点,每个点等概率向正反方向以单位速度移动,所有碰撞都是完全弹性碰撞,即球相撞后各自沿反方向移动,速度大小不变。现在给定时间T,问T秒内发生了几次碰撞。
枚舉方向,然後枚舉兩個點是否能在2T秒內相遇。碰撞之後的狀態相當於擦肩而過。其實只要枚舉兩個點是否相撞最後答案除4就可以了。
double expectedBounces(vector <int> x, int T) {
int ret = 0;
sort(x.begin(), x.end());
for (int i = 0; i < x.size(); ++i) {
for (int j = i + 1; j < x.size(); ++j) {
if (x[j] - x[i] <= 2 * T)
++ret;
}
}
return ret * 0.25;
}
https://community.topcoder.com/stat?c=problem_solution&cr=14970299&rd=14180&pm=10726
public double expectedBounces(int[] x, int T) {
int n = x.length;
double ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (Math.abs(x[i] - x[j]) <= 2 * T)
ans += .25;
}
}
return ans;
}
(By changing the order of summation by summing over "i" inside the "k" and "m" loops, it is easy to show that it is enough to add 0.25 for each pair of balls which are close enough.)