zoukankan      html  css  js  c++  java
  • POJ 3348 Cows | 凸包——童年的回忆(误)

    想当年……还是邱神给我讲的凸包来着……

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #define eps 0.000000001
    #define enter putchar('
    ')
    #define space putchar(' ')
    using namespace std;
    typedef long long ll;
    template <class T>
    void read(T &x){
        char c;
        bool op = 0;
        while(c = getchar(), c < '0' || c > '9')
    	if(c == '-') op = 1;
        x = c - '0';
        while(c = getchar(), c >= '0' && c <= '9')
    	x = x * 10 + c - '0';
        if(op) x = -x;
    }
    template <class T>
    void write(T x){
        if(x < 0) putchar('-'), x = -x;
        if(x >= 10) write(x / 10);
        putchar('0' + x % 10);
    }
    const int N = 10005, INF = 0x3f3f3f3f;
    int n, top, ans;
    struct point {
        int x, y;
        point(){}
        point(int _x, int _y): x(_x), y(_y){}
        point operator - (point b) const{
    	return point(x - b.x, y - b.y);
        }
        int operator * (point b){
    	return x * b.y - y * b.x;
        }
        int norm(){
    	return x * x + y * y;
        }
    } p[N], stk[N], root;
    bool operator < (const point &a, const point &b){
        int det = (a - root) * (b - root);
        if(det) return det > 0;
        return (a - root).norm() < (b - root).norm();
    }
    void graham(){
        int pos = 0;
        root.x = root.y = INF;
        for(int i = 1; i <= n; i++)
    	if(p[i].x < root.x || (p[i].x == root.x && p[i].y < root.y))
    	    root = p[i], pos = i;
        if(pos != 1) swap(p[pos], p[1]);
        sort(p + 2, p + n + 1);
        stk[top = 1] = root;
        for(int i = 2; i <= n; i++){
    	while(top >= 2 && (stk[top] - stk[top - 1]) * (p[i] - stk[top - 1]) < 0) top--;
    	stk[++top] = p[i];
        }
    }
    void getarea(){
        stk[top + 1] = stk[1];
        for(int i = 1; i <= top; i++)
    	ans += stk[i] * stk[i + 1];
    }
    int main(){
        read(n);
        for(int i = 1; i <= n; i++)
    	read(p[i].x), read(p[i].y);
        graham();
        getarea();
        write(ans / 2 / 50), enter;
        return 0;
    }
    
  • 相关阅读:
    分库分表的几种常见形式以及可能遇到的难
    一次非常有意思的 SQL 优化经历
    JavaScript 中 4 种常见的内存泄露陷阱
    专治前端焦虑的学习方案
    专治前端焦虑的学习方案
    [计算机视觉]图像直方图与直方图均衡化
    vector.erase用法注意事项
    [OPENCV]cvHoughLines2使用说明
    MarkDown教程
    GitHub用法
  • 原文地址:https://www.cnblogs.com/RabbitHu/p/POJ3348.html
Copyright © 2011-2022 走看看