poj 2074

哎怎么说,感觉现在处理平面上点线的题已经比较熟练了。

这题就离散化然后搞个前缀和就没了。

准备开始进一步的自闭了。

下面是disguss的一些样例。。。

其实是我自己写错了个地方,本来能1A的。

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cmath>
  4 #include <iostream>
  5 #include <iomanip>
  6 #include <vector>
  7 #include <algorithm>
  8 using namespace std;
  9 typedef double db;
 10 const db eps=1e-6;
 11 const db pi=acos(-1);
 12 int sign(db k){
 13     if (k>eps) return 1; else if (k<-eps) return -1; return 0;
 14 }
 15 int cmp(db k1,db k2){return sign(k1-k2);}
 16 struct point{
 17     db x,y;
 18     point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
 19     point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
 20     point operator * (db k1) const{return (point){x*k1,y*k1};}
 21     point operator / (db k1) const{return (point){x/k1,y/k1};}
 22     db abs(){ return sqrt(x*x+y*y);}
 23 };
 24 db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;}
 25 db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;}
 26 point getLL(point k1,point k2,point k3,point k4){
 27     db w1=cross(k1-k3,k4-k3),w2=cross(k4-k3,k2-k3); return (k1*w2+k2*w1)/(w1+w2);
 28 }
 29 int intersect(db l1,db r1,db l2,db r2){
 30     if (l1>r1) swap(l1,r1); if (l2>r2) swap(l2,r2); return cmp(r1,l2)!=-1&&cmp(r2,l1)!=-1;
 31 }
 32 int checkSS(point k1,point k2,point k3,point k4){
 33     return intersect(k1.x,k2.x,k3.x,k4.x)&&intersect(k1.y,k2.y,k3.y,k4.y)&&
 34            sign(cross(k3-k1,k4-k1))*sign(cross(k3-k2,k4-k2))<=0&&
 35            sign(cross(k1-k3,k2-k3))*sign(cross(k1-k4,k2-k4))<=0;
 36 }
 37 struct line{
 38     point p[2];
 39     line(point k1,point k2){p[0]=k1; p[1]=k2;}
 40 };
 41 double l,r,y;
 42 point p[10005];
 43 int n;
 44 point s1,t1,s2,t2;
 45 //离散化+前缀和。
 46 vector<db> v,a,b;
 47 int pre[100005];
 48 int main(){
 49     ios::sync_with_stdio(false);
 50     cout<<fixed<<setprecision(2);
 51     while (cin>>l>>r>>y&&(l||r||y)){
 52         s1={l,y};t1={r,y};
 53         cin>>l>>r>>y;
 54         s2={l,y};t2={r,y};
 55         v.push_back(l);
 56         v.push_back(r);
 57         cin>>n;
 58         for(int i=1;i<=2*n;i+=2){
 59             cin>>l>>r>>y;
 60             p[i]={l,y};
 61             p[i+1]={r,y};
 62         }
 63         for(int i=1;i<=2*n;i+=2){
 64             if(cmp(s1.y,p[i].y)==1&&cmp(p[i].y,s2.y)==1){//在中间
 65                 point tmp = getLL(t1,p[i],s2,t2);
 66                 v.push_back(tmp.x);//-1
 67                 a.push_back(tmp.x);
 68                 tmp = getLL(s1,p[i+1],s2,t2);
 69                 v.push_back(tmp.x);//+1
 70                 b.push_back(tmp.x);
 71             }
 72         }
 73         sort(v.begin(),v.end());
 74         v.erase(unique(v.begin(),v.end()),v.end());
 75         for(int i=0;i<a.size();i++){
 76             int id = lower_bound(v.begin(),v.end(),a[i])-v.begin();
 77             pre[id]--;
 78         }
 79         for(int i=0;i<b.size();i++){
 80             int id = lower_bound(v.begin(),v.end(),b[i])-v.begin();
 81             pre[id]++;
 82         }
 83         int id1 = lower_bound(v.begin(),v.end(),s2.x)-v.begin();
 84         int id2 = lower_bound(v.begin(),v.end(),t2.x)-v.begin();
 85         for(int i=1;i<v.size();i++){
 86             pre[i]+=pre[i-1];
 87         }
 88         db ans = 0;
 89         for(int l=id1,r;l<=id2;l=r+1){
 90             r=l;
 91             while (r<id2&&pre[r]>=0)
 92                 r++;
 93             ans=max(ans,v[r]-v[l]);
 94         }
 95         if(cmp(ans,0.00)==0){
 96             cout<<"No View"<<endl;
 97         } else {
 98             cout << ans << endl;
 99         }
100         v.clear();
101         a.clear();
102         b.clear();
103         memset(pre,0, sizeof(pre));
104     }
105 }
106 /**
107  2 6 6
108  0 15 0
109  1
110  2 6 7
111 
112  2 6 6
113  0 15 0
114  1
115  2 6 7
116  2 6 6
117  0 15 0
118  1
119  4 4.5 5.5
120  2 6 6
121  0 15 0
122  16
123  0 1 3
124  1.5 2 3
125  2.5 3 3
126  3.5 4 3
127  4.5 5 3
128  5.5 6 3
129  6.5 7 3
130  7.5 8 3
131  8.5 9 3
132  9.5 10 3
133  10.5 11 3
134  11.5 12 3
135  12.5 13 3
136  13.5 14 3
137  14.5 15 3
138  15.5 16 3
139  2 6 6
140  0 15 0
141  16
142  0 1 .1
143  1.5 2 .1
144  2.5 3 .1
145  3.5 4 .1
146  4.5 5 .1
147  5.5 6 .1
148  6.5 7 .1
149  7.5 8 .1
150  8.5 9 .1
151  9.5 10 .1
152  10.5 11 .1
153  11.5 12 .1
154  12.5 13 .1
155  13.5 14 .1
156  14.5 15 .1
157  15.5 16 .1
158  2 6 6
159  0 15 0
160  14
161  0 1 3
162  1.5 2 3
163  2.5 3 3
164  3.5 4 3
165  4.5 5 3
166  5.5 6 3
167  8.5 9 3
168  9.5 10 3
169  10.5 11 3
170  11.5 12 3
171  12.5 13 3
172  13.5 14 3
173  14.5 15 3
174  15.5 16 3
175 
176 
177  2 6 6
178  0 15 1
179  5
180  1 1.5 6
181  17 18 1
182  3 5 3
183  0 20 10
184  0 20 0.5
185  */
View Code

猜你喜欢

转载自www.cnblogs.com/MXang/p/10444588.html