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

81 篇文章 0 订阅
23 篇文章 0 订阅

Description

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

Input

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

(1n100,1Kn,1eps999,|x0|,|y0|,|xi|,|yi|1000)

Output

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

Sample Input

1
1 500
5 5
1 2

Sample Output

3.84257761518762740

Solution

二分 R R ,对于固定的R可以求出第 i i 个目标被摧毁的概率pi,以 dp[i][j] d p [ i ] [ j ] 表示前 i i 个目标有j个目标被摧毁的概率,根据第 i i 个目标是否被摧毁有转移dp[i][j]=pidp[i1][j1]+(1pi)dp[i1][j] O(n2) O ( n 2 ) 完成转移后,摧毁目标期望数量小于 K K 的概率即为i=0K1dp[n][i],如果该值大于 eps e p s 说明 R R 过小要尝试更大的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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值