[计算几何]Piece of Cake

题目描述
Alice received a cake for her birthday! Her cake can be described by a convex polygon with n vertices. No three vertices are collinear.
Alice will now choose exactly k random vertices (k≥3) from her cake and cut a piece, the shape of which is the convex polygon defined by those vertices. Compute the expected area of this piece of cake.

 

输入
Each test case will begin with a line with two space-separated integers n and k (3≤k≤n≤2500),where n is the number of vertices of the cake, and k is the number of vertices of the piece that Alice cuts.
Each of the next n lines will contain two space-separated real numbers x and y (-10.0≤x,y≤10.0),where (x,y) is a vertex of the cake. The vertices will be listed in clockwise order. No three vertices will be collinear. All real numbers have at most 6 digits after the decimal point.

 

输出
Output a single real number, which is the expected area of the piece of cake that Alice cuts out.
Your answer will be accepted if it is within an absolute error of 10−6 .

 

样例输入

复制样例数据

4 3
0 0
1 1
2 1
1 0
样例输出
0.50000000

 思路:

期望=k边形面积总和/C(n,k)

求多边形面积的原理为:

 因此可以枚举所有逆时针相邻点,计算它对k边形面积总和的贡献:假设枚举P1与P2这两点,设以P1P2为边且保证P1,P2在k边形中逆时针排列的k边形有m个,则这两点对面积总和的贡献为(OP1xOP2)*m

 AC代码:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

struct Point{
  double x,y;
}a[2505];
double det(Point a,Point b){
  return a.x*b.y-a.y*b.x;
}

double c[2505][2505];
void init(){
    c[0][0]=0.0;
    for(int i=1;i<=2500;i++){
        for(int j=0;j<=i;j++){
            if(j==0||j==i) c[i][j]=1.0;
            else c[i][j]=c[i-1][j-1]+c[i-1][j];
        }
    }
}

int main()
{
    init();
    int n,k;scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
    double ans=0.0;
    for(int i=0;i<n;i++){
        for(int j=k-1;j<=n-1;j++){
            ans+=det(a[i],a[(i+j)%n])*0.5*c[j-1][k-2]*1.0;
        }
    }
    ans=ans*1.0/(c[n][k]*1.0);
    printf("%.8f\n",ans);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/lllxq/p/11469523.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值