【O(nlgn)判定點在凸包內】Codeforces Round #113 (Div. 2) B

來源:互聯網
上載者:User

思路參考了這個bloghttp://hi.baidu.com/aekdycoin/blog/item/7abf85026f0d7e85d43f7cfe.html

複雜度是O(nlgn)

#define N 100005struct node{    double x,y;}a[100005];double cross(node a,node b,node c){//>0,ab在ac順時針;<0,ab在ac逆時針    return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);}int n,m;bool chk(node b){    int i,j;    if(cross(a[0],b,a[1]) <= 0)return false;    if(cross(a[0],b,a[n-1]) >= 0)return false;    int l = 0,r = n-1,mid;    int tag = 0;    while(l<=r){        mid = (l+r)>>1;        if(cross(a[0],b,a[mid]) >= 0){            tag = mid;            l = mid+1;        } else r = mid-1;    }    l = tag,r = tag+1;    if(cross(a[l],b,a[r]) <= 0)return false;    return true;}int main(){    cin>>n;    int i,j;    for(i=0;i<n;i++){        cin>>a[i].x>>a[i].y;    }    int ans = 0;    cin>>m;    node b;    for(i=0;i<m;i++){        cin>>b.x>>b.y;        if(chk(b))ans++;    }    if(ans==m)cout<<"YES"<<endl;    else cout<<"NO"<<endl;    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.