题意:两人登高望远,所能看到的山的长度。
设两人所处位置为v,则有对于p[i],若其在射线vp[i-1]上方,则线段p[i]p[i-1]都能看见,若在下方或线上,则记录下当起点site = i-1;
然后判断后面的点p[i]与射线vp[site]的位置关系。
PS:貌似以前写的线段相交是错的,不知为啥还能A掉题。。。。。
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <algorithm> #define LL long long #define PI (acos(-1)) #define EPS (1e-4) using namespace std; struct P { double x,y; } p[1100]; double X_Mul(P a1,P a2,P b1,P b2) { P v1 = {a2.x-a1.x,a2.y-a1.y},v2 = {b2.x-b1.x,b2.y-b1.y}; return v1.x*v2.y - v1.y*v2.x; } double Cal_Point_Dis(P p1,P p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y)); } P Cal_Segment_Cross_Point(P a1,P a2,P b1,P b2) { double t = X_Mul(b1,b2,b1,a1) / X_Mul(a1,a2,b1,b2); P cp = {a1.x+(a2.x-a1.x)*t,a1.y+(a2.y-a1.y)*t}; return cp; } double Cal_Visible_Surface(P *p,P v,int n) { int i = 1,site; double len = 0; while(i < n) { for(; i < n && X_Mul(v,p[i],v,p[i-1]) < -EPS; ++i) { len += Cal_Point_Dis(p[i],p[i-1]); } for(site = i-1; i < n; ++i) { if(X_Mul(v,p[site],v,p[i]) > EPS) { len += Cal_Point_Dis(p[i],Cal_Segment_Cross_Point(v,p[site],p[i],p[i-1])); site = i; if((i&1) == 0) { ++i; break; } } } } return len; } int main() { int n,i; P v = {0,0}; while(scanf("%d %lf",&n,&v.y) && (v.y || n)) { for(i = 0; i < n; ++i) { scanf("%lf %lf",&p[i].x,&p[i].y); } printf("%.2lf\n",Cal_Visible_Surface(p,v,n)); } return 0; }