poj2826An Easy Problem?!

链接

繁琐细节题。

1、线段无交点时,ans=0;

2、如图 假设过p3.y的水平线与p1p2相交

因为雨是垂直下落的,左图的情况是无法收集到雨水的

  1 #include <iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<stdlib.h>
  6 #include<vector>
  7 #include<cmath>
  8 #include<queue>
  9 #include<set>
 10 using namespace std;
 11 #define N 100000
 12 #define LL long long
 13 #define INF 0xfffffff
 14 const double eps = 1e-8;
 15 const double pi = acos(-1.0);
 16 const double inf = ~0u>>2;
 17 struct point
 18 {
 19     double x,y;
 20     point(double x=0,double y=0):x(x),y(y) {}
 21 } p[10];
 22 typedef point pointt;
 23 pointt operator -(point a,point b)
 24 {
 25     return pointt(a.x-b.x,a.y-b.y);
 26 }
 27 int dcmp(double x)
 28 {
 29     if(fabs(x)<eps) return 0;
 30     return x<0?-1:1;
 31 }
 32 double cross(point a,point b)
 33 {
 34     return a.x*b.y-a.y*b.x;
 35 }
 36 bool cmp(point a,point b)
 37 {
 38     return a.y>b.y;
 39 }
 40
 41 bool intersection1(point p1, point p2, point p3, point p4, point& p)      // 直线相交
 42 {
 43     double a1, b1, c1, a2, b2, c2, d;
 44     a1 = p1.y - p2.y;
 45     b1 = p2.x - p1.x;
 46     c1 = p1.x*p2.y - p2.x*p1.y;
 47     a2 = p3.y - p4.y;
 48     b2 = p4.x - p3.x;
 49     c2 = p3.x*p4.y - p4.x*p3.y;
 50     d = a1*b2 - a2*b1;
 51     if (!dcmp(d))    return false;
 52     p.x = (-c1*b2 + c2*b1) / d;
 53     p.y = (-a1*c2 + a2*c1) / d;
 54     return true;
 55 }
 56 double solve(point p1,point p2,point p3,point p4,point pp,point tp)
 57 {
 58     double ans;
 59     point p11 = point(p1.x,p1.y+1);
 60     if(dcmp(cross(p11-p1,p1-p3))==0)
 61     {
 62         ans = 0;
 63         //cout<<",";
 64     }
 65     else
 66     {
 67         if(dcmp(cross(p11-p1,p1-p3))*dcmp(cross(p1-p2,p3-p4))<0)
 68         {
 69             ans = fabs(cross(p3-pp,tp-pp))/2;
 70             //cout<<",";
 71         }
 72         else ans = 0;
 73     }
 74     return ans;
 75 }
 76 int on_segment( point p1,point p2 ,point p )
 77 {
 78     double max=p1.x > p2.x ? p1.x : p2.x ;
 79     double min =p1.x < p2.x ? p1.x : p2.x ;
 80     double max1=p1.y > p2.y ? p1.y : p2.y ;
 81     double min1=p1.y < p2.y ? p1.y : p2.y ;
 82     if( p.x >=min && p.x <=max &&
 83             p.y >=min1 && p.y <=max1 )
 84         return 1;
 85     else
 86         return 0;
 87 }
 88 int main()
 89 {
 90     int t,i;
 91     cin>>t;
 92     while(t--)
 93     {
 94         for(i = 1; i <= 4 ; i++)
 95             scanf("%lf%lf",&p[i].x,&p[i].y);
 96         sort(p+1,p+3,cmp);
 97         sort(p+3,p+5,cmp);
 98         point pp;
 99         if(intersection1(p[1],p[2],p[3],p[4],pp))
100         {
101             if(!on_segment(p[1],p[2],pp)||!on_segment(p[3],p[4],pp))
102             {
103                 puts("0.00");
104                 continue;
105             }
106         }
107         //cout<<pp.x<<" "<<pp.y<<endl;
108         double ans;
109         point p1 = point(p[3].x-1,p[3].y),p2;
110         point p3 = point(p[1].x-1,p[1].y);
111         if(intersection1(p[1],p[2],p[3],p1,p2)&&on_segment(p[1],p[2],p2))
112         {
113
114             ans = solve(p[1],p[2],p[3],p[4],pp,p2);
115         }
116         else
117         {
118             intersection1(p[3],p[4],p[1],p3,p2);
119            // cout<<p2.x<<" "<<p2.y<<endl;
120             ans = solve(p[3],p[4],p[1],p[2],pp,p2);
121         }
122         printf("%.2f\n",ans+eps);
123
124     }
125     return 0;
126 }

poj2826An Easy Problem?!,布布扣,bubuko.com

时间: 2024-10-05 05:31:27

poj2826An Easy Problem?!的相关文章

an easy problem(贪心)

An Easy Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8333   Accepted: 4986 Description As we known, data stored in the computers is in binary form. The problem we discuss now is about the positive integers and its binary form.

HDU2123 An easy problem【水题】

An easy problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5922    Accepted Submission(s): 4122 Problem Description In this problem you need to make a multiply table of N * N ,just like th

HDU2132 An easy problem【水题】

An easy problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10149    Accepted Submission(s): 2689 Problem Description We once did a lot of recursional problem . I think some of them is easy

Poj 2826 An Easy Problem?!

地址:http://poj.org/problem?id=2826 题目: An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 2003 Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Ben nails tw

FZU 1753-Another Easy Problem(求多个组合数的最大公约数)

Another Easy Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 1753 Appoint description:  xietutu  (2013-03-13)System Crawler  (2015-04-27) Description 小TT最近学习了高斯消元法解方程组,现在他的问题来了,如果是以下的方程,

(DS 《算法入门经典》)UVA 11991 Easy Problem from Rujia Liu?(求第k个v出现的索引)

题目大意: 求第k个v出现的索引 解题思路: 如果能构造出一个数据结构,使得data[v][k]就是第k个v出现的索引值即可求解.data[v]表示数v出现的索引数组, data[v][k]表示第k个v出现的索引. Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 200

fzu 1753 Another Easy Problem

本题题意为求 t (t<150) 个 c (n,m)  (1<=m<=n<=100000)的最大公因子: 本题的难点为优化.主要有两个优化重点.一是每次对单个素因子进行处理,优化每次的数组清零:二是对求阶乘素因子个数的优化 ei=[N/pi^1]+ [N/pi^2]+ …… + [N/pi^n]  其中[]为取整 ei 为数 N!中pi 因子的个数: #include <iostream>#include <cstring>#include <cmat

POJ2826 An Easy Problem?!(线段交点,三角形面积)

题目链接: http://poj.org/problem?id=2826 题目描述: An Easy Problem?! Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Ben nails two wooden boards on the wall of his barn. Shown in the pictures below, the two b

HDU 5475An easy problem 离线set/线段树

An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number X is showed on the screen of calculator. At first