zoukankan      html  css  js  c++  java
  • BZOJ5008: 方师傅的房子

    BZOJ5008: 方师傅的房子

    https://lydsy.com/JudgeOnline/problem.php?id=5008

    分析:

    • 二分这个点在哪个三角形所在顶角内,然后再判断是否在三角形内部。
    • 用叉积来判断。

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cstdlib>
    #include <vector>
    #include <iostream>
    #include <cmath>
    using namespace std;
    #define N 100050
    typedef long long ll;
    struct Point {
    	ll x,y;
    	Point() {}
    	Point(ll x_,ll y_) {x=x_,y=y_;}
    	Point operator - (const Point &p) const {return Point(x-p.x,y-p.y);}
    }a[N],b[N];
    ll cross(const Point &p1,const Point &p2) {return p1.x*p2.y-p1.y*p2.x;}
    int n;
    int main() {
    	scanf("%d",&n);
    	int i;
    	for(i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].y);
    	for(i=1;i<=n;i++) b[i]=a[i]-a[1];
    	int m,ans=0;
    	scanf("%d",&m);
    	int lstans=1;
    	Point p=Point(0,0);
    	while(m--) {
    		ll dx,dy;
    		scanf("%lld%lld",&dx,&dy);
    		p.x+=lstans*dx,p.y+=lstans*dy;
    		if(cross(p-a[1],a[2]-a[1])>0||cross(p-a[1],a[n]-a[1])<0) {
    			lstans=-1;
    		}else {
    			int l=2,r=n+1;
    			while(l<r) {
    				int mid=(l+r)>>1;
    				if(cross(p-a[1],a[mid]-a[1])<0) l=mid+1;
    				else r=mid;
    			}
    			if(cross(p-a[l-1],a[l]-a[l-1])<=0) {
    				ans++; lstans=1;
    			}else {
    				lstans=-1;
    			}
    		}
    	}
    	printf("%d
    ",ans);
    }
    
  • 相关阅读:
    Python之路Day02
    Python之路Day01
    Python 常用单词
    JS-向数组添加元素
    JS-lambda表达式
    正则表达式问记录
    JDBC学习
    jquery常用选择器
    mongodb学习
    java8 lambda表达式
  • 原文地址:https://www.cnblogs.com/suika/p/10205516.html
Copyright © 2011-2022 走看看