計算幾何

平面上有有條線段,判斷這兩條線段是否會相交。

【line.in】
0 0 1 1 0 1 1 0
【line.out】
1

判斷線段相交可以用跨立實驗+快速排斥
這裏寫圖片描述
如果是直線,只需要快速排斥就可以判斷了(?)。線段有長度,因此還要跨立實驗。

快速排斥

 if ((max(a.x,b.x)<min(c.x,d.x)||max(a.y,b.y)<min(c.y,d.y)||
 max(c.x,d.y)<min(a.x,b.x)||max(c,y,d.y)<min(a.y,c.y))
 return false;

跨立實驗:

   const double eps =  1e-6;
   double h, i, j, k;   
   h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);   
   i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);   
   j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);   
   k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);   
   return h * i <= eps && j * k <= eps;   
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
const double eps = 1e-6;
using namespace std;

struct point {
    double x;double y;
};
inline bool judge(point a, point b, point c, point d)  
{   
    if (max(a.x,b.x)<min(c.x,d.x)||max(a.y,b.y)<min(c.y,d.y)||max(c.x,d.y)<min(a.x,b.x)||max(c.y,d.y)<min(a.y,c.y))
    return false;
    double h,i,j,k;
    h = (b.x - a.x)*(c.y - a.y) - (b.y - a.y) * (c.x - a.x);
    i = (b.x - a.x)*(d.y - a.y) - (b.y - a.y) * (d.x - a.x);
    j = (d.x - c.x)*(a.y - c.y) - (d.y - c.y) * (a.x - c.x);
    k = (d.x - c.x)*(b.y - c.y) - (d.y - c.y) * (b.x - c.x);
    return (h*i <= eps && j*k <= eps);
}
int main(){
    freopen("input.in","r",stdin);
    point a,b,c,d;
    scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);
    cout<<judge(a,b,c,d);
}

B
【問題描述】
小Q對計算幾何有着濃厚的興趣。他經常對着平面直角座標系發呆,思考一些有趣的問題。今天,他想到了一個十分有意思的題目:
首先,小Q會在x軸正半軸和y軸正半軸分別挑選n個點。隨後,他將x軸的點與y軸的點一一連接,形成n條線段,並保證任意兩條線段不相交。小Q確定這種連接方式有且僅有一種。最後,小Q會給出m個詢問。對於每個詢問,將會給定一個點p(px,py),請回答線段OP與n條線段會產生多少個交點?
小Q找到了正在鑽研數據結構的你,希望你可以幫他解決這道難題。
【輸入格式】
第1行包含一個正整數n,表示線段的數量;
第2行包含n個正整數,表示小Q在x軸選取的點的橫座標;
第3行包含n個正整數,表示小Q在y軸選取的點的縱座標;
第4行包含一個正整數m,表示詢問數量;
隨後m行,每行包含兩個正整數px,py,表示詢問中給定的點的橫、縱座標。
【輸出格式】
共m行,每行包含一個非負整數,表示你對這條詢問給出的答案。
【樣例輸入】
3
4 5 3
3 5 4
2
1 1
3 3
【樣例輸出】
0
3
【數據範圍】
對於50%的數據,1≤n,m,≤2×103。
對於100%的數據,1≤n,m≤2×105,座標範圍≤108。
3 4 5
3 4 5

由於情況具有單調性,用二分可優化時間複雜度。利用上面得到的定理,可以用二分+判斷來解決這個題目。

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<list>
#define N 200001
using namespace std;
const double eps = 1e-6;
int px,py;
int a[N + 10], b[N + 10];
struct point {
    double x;double y;
}o,p,f,g;
inline bool judge(point a, point b, point c, point d)  
{   
    if (max(a.x,b.x)<min(c.x,d.x)||max(a.y,b.y)<min(c.y,d.y)||max(c.x,d.y)<min(a.x,b.x)||max(c.y,d.y)<min(a.y,c.y))
    return false;
    double h,i,j,k;
    h = (b.x - a.x)*(c.y - a.y) - (b.y - a.y) * (c.x - a.x);
    i = (b.x - a.x)*(d.y - a.y) - (b.y - a.y) * (d.x - a.x);
    j = (d.x - c.x)*(a.y - c.y) - (d.y - c.y) * (a.x - c.x);
    k = (d.x - c.x)*(b.y - c.y) - (d.y - c.y) * (b.x - c.x);
    return (h*i <= eps && j*k <= eps);
}
int n,m;
int main(){
    freopen("b.in","r",stdin);
    freopen("b.out","w",stdout);
    scanf("%d",&n);
    for (int i =1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for (int j=1;j<=n;j++){
        scanf("%d",&b[j]);
    }
    sort(a+1,a+n+1);
    sort(b+1,b+n+1);
    scanf("%d",&m);
    o.x = 0;o.y = 0;
    for (int i =1;i<=m;i++){
        scanf("%lf%lf",&p.x,&p.y);
        int l = 0,r = n;
        while(l<r){
            int mid = ((l+r+1)>>1);
            f.x = 0;f.y = b[mid];//y zhou
            g.x = a[mid];g.y = 0;//x zhou
            if (judge(o,p,f,g)){
                l = mid;
            }else{
                r = mid-1;
            } 
        }
        printf("%d\n",l);
    }

}
//來自http://blog.csdn.net/Donald_TY/article/details/53001702
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
int n,q;
ll x,y;
ll a[100005],b[100005];
int erfen(int l,int r)
{
    if(l>r) return r;
    int mid=(l+r)/2;
    if(a[mid]*y>=x*(-1)*b[mid]+a[mid]*b[mid]) return erfen(mid+1,r); else return erfen(l,mid-1);
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++) scanf("%lld",&b[i]); 
    sort(b+1,b+n+1);  //因爲線段不相交,所以x與y必須遞增
    cin>>q;
    for(int i=1;i<=q;i++) 
    {
        scanf("%lld%lld",&x,&y);
        printf("%d\n",erfen(1,n));
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章