洞察探讨小游戏SDK接入的最佳实践以及对企业跨平台开发的优势
842
2022-08-26
HDU 4609 3-idiots (FFT)
Description
King OMeGa catched three men who had been streaking in the street. Looking as idiots though, the three men insisted that it was a kind of performance art, and begged the king to free them. Out of hatred to the real idiots, the king wanted to check if they were lying. The three men were sent to the king’s forest, and each of them was asked to pick a branch one after another. If the three branches they bring back can form a triangle, their math ability would save them. Otherwise, they would be sent into jail.However, the three men were exactly idiots, and what they would do is only to pick the branches randomly. Certainly, they couldn’t pick the same branch - but the one with the same length as another is available. Given the lengths of all branches in the forest, determine the probability that they would be saved.
Input
An integer T(T≤100) will exist in the first line of input, indicating the number of test cases.Each test case begins with the number of branches N(3≤N≤10^5).The following line contains N integers a_i (1≤a_i≤10^5), which denotes the length of each branch, respectively.
Output
Output the probability that their branches can form a triangle, in accuracy of 7 decimal places.
Sample Input
241 3 3 442 3 3 4
Sample Output
0.50000001.0000000
题意
给定一些数字表示线段的长度,求任意选取三个可以组成三角形的概率。
思路
在所有数字中选取三个的总组合数为 C3n
组成三角形需要找两条短边的长度之和大于一条长边,因此我们要先计算选取两条边所能组合成的长度有哪些。
而两条边组合后的长度本质上为其和,因此我们可以用 num[i] 来记录长度为 i 的边的个数。
然后求 num 数组自身对自身的卷积,其结果便是所有两两组合的情况了。
如对于 {1,3,3,4} 来说, num = {0,1,0,2,1} 。
{0,1,0,2,1} * {0,1,0,2,1} 卷积结果为 {0,0,1,0,4,2,4,4,1} 。
卷积的意义其实是增幅,平移再相加。(和乘法的原理一致)
我们知道:时域卷积,频域乘积。
因此我们可以采用 FFT(DFT) 先将 num 转换为另一种表示形式(求值),然后两两做(乘积)以后再使用 FFT(IDFT) 将其转换回来(插值)。
此时我们便求得了 num 自身对自身的卷积结果啦。
结果中的值代表着 num 自身的两两组合。
因为包含某个数本身与本身的组合,所以我们要先去除这样的情况。第一个选取a1 ,第二个选取a2 与第一个选取a2 ,第二个选取a1 我们认为是一样的,因此num 数组需要整体除以2 。
对 num 数组求前缀和,原数组 a 进行排序,接下来我们需要找第三条边,满足另外两条边的和大于这条边且标号都小于这条边。
此时我们假设 a[i] 为三角形中最长的一条边,则长度大于 a[i] 的取法有 sum[len]-sum[a[i]] 种。
但是我们需要满足取到的这两条边的边长都小于 a[i] ,假设这两条边分别为 x,y ,则可能出现以下几种情况:
x,y 中有一个值大于a[i] ,另一个值小于a[i] ,总共有(n-i-1)*i 种情况x,y 的值都大于a[i] ,总共有(n-i-1)*(n-i-2)/2 种情况x,y 中有一点落在i 这一位置,总共有n-1 种情况
于是减掉这三种情况剩下的即为满足题意的组合咯,累加统计即可。
AC 代码
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~