[Computational Geometry] Piece of Cake

Title Description

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.

 

Entry

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.

 

Export

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 .

 

Sample input

4 3
0 0
1 1
2 1
1 0

Sample Output

0.50000000

 Ideas:

= Desired area of ​​the sum of k-edge shape / C (n, k)

The Principle of polygon area is:

 It is possible to enumerate all points adjacent counterclockwise, the sum of the contributions calculated k-edge areas of its shape: Suppose these two points P1 and P2 enumeration, and is provided to ensure that the edge P1P2 as P1, P2 arranged in counterclockwise k Sides k-gon with m, the sum of these two contributions area ( OP1 X OP2 ) * m

 AC Code:

#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

 

Guess you like

Origin www.cnblogs.com/lllxq/p/11469523.html