• zoj 1377 Grandpa‘s Estate


    Graham算法求凸包问题

    #include 
    #include
    using namespace std;
    const int maxn = 1001;
    struct node {
    	node () {};
    	node (int _x, int _y): x(_x), y(_y) {};
    	int x, y;
    	node operator -(node nd) {
    		return node(x - nd.x, y - nd.y);
    	}
    
    	int operator *(node nd) {
    		return x * nd.y - y * nd.x;
    	}
    
    } p[maxn], q[maxn];
    int per[maxn], n, l;
    
    inline int dis(node p1, node p2) {
    	return (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y);
    }
    
    inline bool cmp(int v1, int v2) {
    	int det = (p[v1] - p[1]) * (p[v2] - p[1]);
    	if (det != 0)return det > 0;
    	return dis(p[1], p[v1]) < dis(p[1], p[v2]);
    }
    
    void Graham() {
    	int i, id, k;
    	id = 1;
    	for (i = 2; i <= n; i++) {
    		if (p[i].x < p[id].x || (p[i].x == p[id].x && p[i].y < p[id].y))id = i;
    	}
    	if (id != 1)swap(p[1], p[id]);
    	for (i = 1; i <= n; i++) {
    		per[i] = i;
    	}
    	sort(per + 2, per + n + 1, cmp);
    	l = 0;
    	q[++l] = p[1];
    	for (i = 2; i <= n; i++) {
    		k = per[i];
    		while (l >= 2 && (q[l] - q[l - 1]) * (p[k] - q[l - 1]) <= 0)l--;
    		q[++l] = p[k];
    	}
    }
    
    bool judge() {
    	if (l == 2)return 0;
    	int sum;
    	for (int i = 1; i <= l; i++) {
    		sum = 0;
    		for (int j = 1; j <= n; j++) {
    			if ((p[j] - q[i]) * (q[i % l + 1] - p[j]) == 0)sum++;
    		}
    		if (sum < 3)return 0;
    	}
    	return 1;
    }
    
    int main() {
    	int t, i;
    	scanf("%d", &t);
    	while (t--) {
    		scanf("%d", &n);
    		for (i = 1; i <= n; i++) {
    			scanf("%d%d", &p[i].x, &p[i].y);
    		}
    		Graham();
    		if (judge())puts("YES");
    		else puts("NO");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
  • 相关阅读:
    LLM 技术图谱(LLM Tech Map)& Kubernetes (K8s) 与AIGC的结合应用
    《机械原理》上 学后感
    SUSE Linux文件系统在线扩容
    云部署家里的服务器
    完美修复google翻译失效的问题
    2022年11月高项案例分析答案解析
    Spring Boot(一)
    elasticsearch 7.7.0 单节点配置x-pack
    写代码,必须要优雅...
    嵌入式Linux八股(三)——计算机基础
  • 原文地址:https://blog.csdn.net/ZekiDai/article/details/126582798