题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3720

  题意:在一个矩形区域投掷飞镖,每个整点有磁性,每个点的磁性是一样的,因此飞镖只会落在整点上,投到每个点的得分是:Ax+By。矩形区域里面有个多边形,如果飞镖投在多边形里面则得分,求最终的得分期望。

  对于每个点,以它为中心的边长为1的正方形范围内,它都可以把飞镖吸引过来,则最后飞镖能得分的面积就是多边形内以及多边形上所有整点的正方形的面积并,然后期望公式E(X)=p*xi。。

 //STATUS:C++_AC_900MS_188KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
//typedef __int64 LL;
//typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
//const LL LNF=1LL<<60;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Node{
double x,y;
}nod[N]; struct DNode{
double x,y;
}ju[]; double A,B;
int n; int chaji(Node &a,Node &b){
return a.x*b.y-b.x*a.y;
} int ponls(Node &a,Node &b,Node &p)
{
if( (p.x==a.x && p.y==a.y) || (p.x==b.x && p.y==b.y) )return ;
Node r1,r2;
r1.x=a.x-b.x,r1.y=a.y-b.y;
r2.x=p.x-b.x,r2.y=p.y-b.y;
if(!chaji(r1,r2) && p.x>=min(a.x,b.x) && p.x<=max(a.x,b.x)
&& p.y>=min(a.y,b.y) && p.y<=max(a.y,b.y))
return ;
return ;
} int quick(Node &l1,Node &l2,Node &r1,Node &r2)
{ if(min(l1.x,l2.x)>max(r1.x,r2.x)
|| min(l1.y,l2.y)>max(r1.y,r2.y)
|| max(l1.x,l2.x)<min(r1.x,r2.x)
|| max(l1.y,l2.y)<min(r1.y,r2.y))
return ;
return ;
} int las(Node &l1,Node &l2,Node &r1,Node &r2)
{
Node a,b,c;
a.x=l1.x-r1.x;
a.y=l1.y-r1.y;
b.x=r2.x-r1.x;
b.y=r2.y-r1.y;
c.x=l2.x-r1.x;
c.y=l2.y-r1.y;
if( ((a.x*b.y)-(b.x*a.y))*((c.x*b.y)-(b.x*c.y))<)return ;
else return ;
} int pinply(int num_node,Node nod[],Node &p)
{
int i,j,cou=;
Node ray;
ray.x=-,ray.y=p.y;
for(i=;i<num_node;i++){
j=(i+)%num_node;
if(ponls(nod[i],nod[j],p))return ;
if(nod[i].y!=nod[j].y){
if(ponls(p,ray,nod[i]) && nod[i].y==max(nod[i].y,nod[j].y))
cou++;
else if(ponls(p,ray,nod[j]) && nod[j].y==max(nod[i].y,nod[j].y))
cou++;
else if(quick(nod[i],nod[j],p,ray) && las(nod[i],nod[j],p,ray)
&& las(p,ray,nod[i],nod[j]))
cou++;
}
}
return cou&;
} bool isonline(int n,Node nod[],Node &p)
{
int i,j;
for(i=;i<n;i++){
if( (p.y-nod[i].y)*(nod[i+].x-nod[i].x)==(nod[i+].y-nod[i].y)*(p.x-nod[i].x)
&& p.x>=Min(nod[i].x,nod[i+].x) && p.x<=Max(nod[i].x,nod[i+].x)
&& p.y>=Min(nod[i].y,nod[i+].y) && p.y<=Max(nod[i].y,nod[i+].y) )return true;
}
if( (p.y-nod[n].y)*(nod[].x-nod[n].x)==(nod[].y-nod[n].y)*(p.x-nod[n].x)
&& p.x>=Min(nod[n].x,nod[].x) && p.x<=Max(nod[n].x,nod[].x)
&& p.y>=Min(nod[n].y,nod[].y) && p.y<=Max(nod[n].y,nod[].y) )return true;
return false;
} double gets(Node &t)
{
double w,h;
w=Min(t.x+0.5,ju[].x)-Max(t.x-0.5,ju[].x);
h=Min(t.y+0.5,ju[].y)-Max(t.y-0.5,ju[].y);
// printf(" %lf %lf\n",w,h);
return h*w;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j;
double ans,S;
Node t;
int min_x,max_x,min_y,max_y;
while(~scanf("%lf%lf%lf%lf",&ju[].x,&ju[].y,&ju[].x,&ju[].y))
{
scanf("%d%lf%lf",&n,&A,&B);
min_x=,max_x=,min_y=,max_y=;
for(i=;i<n;i++){
scanf("%lf%lf",&nod[i].x,&nod[i].y);
min_x=Min(min_x,(int)nod[i].x);
max_x=Max(max_x,(int)nod[i].x);
min_y=Min(min_y,(int)nod[i].y);
max_y=Max(max_y,(int)nod[i].y); }
S=(ju[].x-ju[].x)*(ju[].y-ju[].y); ans=;
for(i=min_x;i<=max_x;i++){
for(j=min_y;j<=max_y;j++){
t.x=i,t.y=j;
if(pinply(n,nod,t) || isonline(n-,nod,t)){
ans+=(A*i+B*j)*gets(t);
}
}
} printf("%.3lf\n",ans/S);
}
return ;
}
05-07 15:06