hdu 4353 Finding Mine
这个题目的小bug没有想到,最后还是看了别人的代码才知道那里错了。。 这个真的还是蛮好的题的!
下面是本人的搓代码,仅供自己参考。
1 #include <iostream> 2 #include <cmath> 3 #include <cstring> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 using namespace std; 8 #define eps 1e-8 9 #define pi acos(-1.0) 10 #define maxn 205 11 12 int sign( double x ) 13 { 14 if( x < -eps ) return -1; 15 return x > eps; 16 } 17 18 int f[maxn][maxn]; 19 struct point 20 { 21 double x,y; 22 point() {} 23 point(double x,double y):x(x),y(y) {} 24 void in() 25 { 26 scanf("%lf%lf",&x,&y); 27 } 28 } p[maxn],v[maxn*3]; 29 30 double xmult( point a, point b, point o) 31 { 32 return ( a.x- o.x)*( b.y - o.y) - ( a.y- o.y)*( b.x - o.x ); 33 } 34 35 int main() 36 { 37 int t,n,m; 38 scanf("%d",&t); 39 int MM = 0; 40 while( t --) 41 { 42 scanf("%d%d",&n,&m); 43 for(int i = 0; i < n; i ++ ) 44 p[i].in(); 45 for(int j = 0; j < m; j ++ ) 46 v[j].in(); 47 memset( f, 0, sizeof f ); 48 for( int i = 0; i < n; i ++ ) 49 for( int j = i + 1; j < n; j ++ ) 50 { 51 int num = 0 ,a = i, b = j ; 52 if( p[a].x == p[b].x) continue; 53 if( p[a].x > p[b].x ) swap( a, b); 54 //下面比较的必须是一开一闭的 因为下面的相减的是两个相加的 55 //如果两边都算的话 会重复计算这个点的个数的. 56 for( int l = 0; l < m; l ++ ) 57 { 58 if( v[l].x >= p[a].x && v[l].x < p[b].x && sign(xmult(v[l],p[b],p[a])) < 0 ) f[i][j] ++, f[j][i] ++; 59 } 60 } 61 double cnt = -1.0 ; 62 int flag = 0 , ans ; 63 for( int i = 0; i < n; i ++ ) 64 { 65 for( int j = i + 1; j < n; j ++ ) 66 { 67 for( int l = j + 1; l < n; l ++ ) 68 { 69 double s = xmult( p[i], p[j], p[l]); //面积的两倍 70 if( sign(s) < 0 ) s = - s ; 71 int a , b , c ; 72 if( p[i].x <= p[j].x && p[i].x <= p[l].x ) a = i ; 73 else if( p[j].x <= p[l].x && p[j].x <= p[i].x ) a = j; 74 else a = l; 75 if( p[i].x >= p[j].x && p[i].x >= p[l].x ) b = i; 76 else if( p[j].x >= p[i].x && p[j].x >= p[l].x ) b = j; 77 else b = l; 78 c = i + j + l - a - b; 79 if( sign(xmult( p[c], p[b], p[a])) > 0 ) 80 { 81 ans = f[c][a] + f[c][b] - f[a][b]; 82 } 83 else 84 { 85 ans = f[a][b] - f[a][c] - f[c][b]; 86 } 87 if( ans == 0 ) continue; 88 flag = 1; 89 if( sign( cnt + 1.0 ) == 0 ) 90 { 91 cnt = s * 0.5 / ans; 92 continue; 93 } 94 if( sign( cnt - s*0.5/ans ) > 0 ) cnt = s * 0.5 / ans ; 95 } 96 } 97 } 98 printf("Case #%d: ",++MM); 99 if( !flag ) printf("-1\n"); 100 else printf("%.6lf\n",cnt); 101 } 102 return 0; 103 }

浙公网安备 33010602011771号