CodeForces 50 D.Bombing(二分+概率DP)

Description

给出 n 个目标的二维坐标 ( x i , y i ) 以及炸弹的爆炸位置 ( x 0 , y 0 ) ,对于一个爆炸半径 R ,一个距离爆炸位置 D 的目标被摧毁的概率为 P ( D ) = e 1 D 2 R 2 , D > R , P ( D ) = 1 , D R ,先要最小化 R 使得被摧毁目标的期望数量小于 K 的概率小于 e p s 1000

Input

第一行一整数 n 表示目标个数,之后输入两个整数 K , e p s ,然后输入爆炸位置 ( x 0 , y 0 ) ,最后 n 行每行输入两个整数 x i , y i 表示第 i 个目标的坐标

( 1 n 100 , 1 K n , 1 e p s 999 , | x 0 | , | y 0 | , | x i | , | y i | 1000 )

Output

输出满足条件的 R 的最小值,误差不超过 10 6

Sample Input

1
1 500
5 5
1 2

Sample Output

3.84257761518762740

Solution

二分 R ,对于固定的 R 可以求出第 i 个目标被摧毁的概率 p i ,以 d p [ i ] [ j ] 表示前 i 个目标有 j 个目标被摧毁的概率,根据第 i 个目标是否被摧毁有转移 d p [ i ] [ j ] = p i d p [ i 1 ] [ j 1 ] + ( 1 p i ) d p [ i 1 ] [ j ] O ( n 2 ) 完成转移后,摧毁目标期望数量小于 K 的概率即为 i = 0 K 1 d p [ n ] [ i ] ,如果该值大于 e p s 说明 R 过小要尝试更大的 R ,否则说明 R 合法可以进一步尝试更小的 R

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
#define x first
#define y second 
const int INF=0x3f3f3f3f,maxn=105;
int n,K;
double eps,dp[maxn][maxn];
P s,p[maxn];
double dis(P a,P b)
{
    return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y)); 
}
bool check(double r)
{
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(int i=1;i<=n;i++)
        for(int j=0;j<=i;j++)
        {
            double q,d=dis(s,p[i]);
            if(d<=r)q=1;
            else q=exp(1.0-d*d/(r*r));
            if(j==0)dp[i][j]=(1.0-q)*dp[i-1][j];
            else dp[i][j]=q*dp[i-1][j-1]+(1.0-q)*dp[i-1][j];
        }
    double res=0;
    for(int i=0;i<K;i++)res+=dp[n][i];
    if(res>eps)return 0;
    return 1;
}
int main()
{
    scanf("%d%d%lf",&n,&K,&eps);
    eps/=1000;
    scanf("%d%d",&s.x,&s.y);
    double l=0,r=0,mid,ans=0;
    for(int i=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].y),r=max(r,dis(s,p[i]));
    int T=100;
    while(T--)
    {
        mid=0.5*(l+r);
        if(check(mid))ans=mid,r=mid;
        else l=mid;
    }
    printf("%.9f\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/V5ZSQ/article/details/80033603