[NOIP-P1125] Calculate probability

Title description

Xiaoming has n small wooden sticks of different lengths. The lengths of these wooden sticks are all positive integers. Xiao Ming's father wanted to play a game with Xiao Ming. He stipulated an integer length l and let Xiaoming take two out of n sticks with his eyes closed. If the sum of the lengths of the two wooden sticks is less than or equal to l, Xiao Ming wins, otherwise Xiao Ming's father wins. Xiaoming wants to know how likely he is to win.

Input

The input contains two lines. The first line is two integers n and l, where n and l do not exceed 100,000. The second line contains n integers, which are the length of n sticks.

Output

The output contains a real number, the probability of Xiaoming winning, and retain two decimal places.

Sample input

4 5
1 2 3 4

Sample output

0.67

prompt

Code

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include<bits/stdc++.h>
#define rep(i,j,k) for(register int i=(j);i<=(k);++i)
#define per(i,j,k) for(register int i=(j);i>=(k);--i)
using namespace std;
template<class T> inline void read(T &x)
{
    x=0;
    register char c=getchar();
    register bool f=0;
    while(!isdigit(c))f^=c=='-',c=getchar();
    while(isdigit(c))x=x*10+c-'0',c=getchar();
    if(f)x=-x;
}
#define ll long long
const int N=100001;
ll n,a[N],t,l,ans;
int main ()
{
    read(n),read(l);
    rep(i,1,n)
        read(a[i]);
    sort(a+1,a+1+n);
    rep(i,1,n)
    {
        t=upper_bound(a+1,a+i,l-a[i])-a;
        years + = t- 1 ;
    }
    printf("%.2lf",(double)(2*ans)/(double)(n*n-n));
    return 0;
}

Guess you like

Origin www.cnblogs.com/LJA001162/p/12724961.html