[计算几何]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

猜你喜欢

转载自www.cnblogs.com/lllxq/p/11469523.html
今日推荐