zoukankan      html  css  js  c++  java
  • 【POJ 2318】TOYS 叉积

    用叉积判断左右

    快速读入写错了卡了3小时hhh

    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define N 5003
    #define read(x) x = getint()
    using namespace std;
    inline int getint() {
    	int fh = 1, k = 0; char c = getchar();
    	for(; c < '0' || c > '9'; c = getchar())
    		if (c == '-') fh = -1;
    	for(; c >= '0' && c <= '9'; c = getchar())
    		k = k * 10 + c - '0';
    	return k * fh;
    }
    
    struct Point {
    	int x, y;
    	Point(int _x = 0, int _y = 0) : x(_x), y(_y) {}
    };
    Point operator - (Point a, Point b) {
    	return Point(a.x - b.x, a.y - b.y);
    }
    bool Cross(Point a, Point b) {
    	return a.x * b.y - a.y * b.x > 0;
    }
    
    struct node {
    	Point a, b;
    };
    
    Point toy;
    node line[N];
    int ans[N], n, m, up, left, down, right;
    
    int main() {
    	read(n);
    	while (n) {
    		read(m); read(left); read(up); read(right); read(down);
    		for(int i = 1; i <= n; ++i) {
    			scanf("%d%d", &line[i].a.x, &line[i].b.x);
    			line[i].a.y = up;
    			line[i].b.y = down;
    		}
    		
    		for(int i = 0; i <= n; ++i)
    			ans[i] = 0;
    		
    		while (m--) {
    			read(toy.x); read(toy.y);
    			int l = 1, r = n-1, mid, zuo, you;
    			
    			zuo = Cross(line[1].a - line[1].b, toy - line[1].b);
    			you = Cross(line[n].a - line[n].b, toy - line[n].b);
    			if (zuo) {
    				++ans[0];
    				continue;
    			}
    			if (!you) {
    				++ans[n];
    				continue;
    			}
    			
    			while (l <= r) {
    				mid = (l + r) >> 1;
    				zuo = Cross(line[mid].a - line[mid].b, toy - line[mid].b);
    				you = Cross(line[mid + 1].a - line[mid + 1].b, toy - line[mid + 1].b);
    				if (zuo)
    					r = mid - 1;
    				else if (!you)
    					 	l = mid + 1;
    					 else
    					 	break;
    			}
    			++ans[mid];
    		}
    		
    		for(int i = 0; i <= n; ++i)
    			printf("%d: %d
    ", i, ans[i]);
    		puts("");
    		read(n);
    	}
    	
    	return 0;
    }
    

    无语······

  • 相关阅读:
    负数求余数 C 和 Matlab&Python 处理不一样
    [Matlab] 线性卷积&圆周卷积代码实现
    [Arduino] 驱动RC522 读取 UID例程
    [C++] Nested Radical Constant
    [Arduino] 学习总结小合集(更新ING)
    谐振电路的品质因素总结
    142. Linked List Cycle II
    664. Strange Printer
    188. Best Time to Buy and Sell Stock IV
    50. Pow(x, n)
  • 原文地址:https://www.cnblogs.com/abclzr/p/5345043.html
Copyright © 2011-2022 走看看