poj 3348 Cows
来源:互联网 发布:淘宝医药商城 编辑:程序博客网 时间:2024/05/16 01:06
题意:给出若干点的位置,计算由这些点组成凸包的面积S(农场面积),每头牛至少要50平方米,
所以输出S/50的整数部分即可
计算凸包面积
n边形可以划成n-2个三角形
已知三角形顶点坐标,求三角形面积最直接的公式设A(x1,y1),B(x2,y2),C(x3,y3)在坐标系中中顺序为三点按逆时针排列
S=1/2[(x1y2-x2y1)+(x2y3-x3y2)+(x3y1-x1y3)]
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MAXN = 10005;struct node{ double x,y;}p[MAXN];int res[MAXN],top;int n;bool cmp(const node &a, const node &b){ if(a.y == b.y) return a.x < b.x; return a.y < b.y;}double multi(node &a, node &b, node &c){ return (a.x - c.x)*(b.y - c.y) - (b.x - c.x)*(a.y - c.y);}double square(node &op, node &sp, node &ep){ return (op.x*sp.y - sp.x*op.y + sp.x*ep.y - ep.x*sp.y + ep.x*op.y - op.x*ep.y)/2;}void Graham(){ int len; top = 1; sort(p,p+n,cmp); for(int i = 0; i < 3; i++) res[i] = i; for (int i = 2; i < n; ++i) { while(top && multi(p[i], p[res[top]], p[res[top-1]]) >= 0) top--; res[++top] = i; } len = top; res[++top] = n-2; for(int i = n-3; i >= 0; i--) { while(top != len && multi(p[i], p[res[top]], p[res[top-1]]) >= 0) top--; res[++top] = i; }}int main(int argc, char const *argv[]){ double ans; while(~scanf("%d", &n)) { for(int i = 0; i < n; i++) scanf("%lf %lf", &p[i].x, &p[i].y); Graham(); ans = 0; for(int i = 1; i < top-1; i++) { ans += square(p[res[i]], p[res[i+1]], p[res[0]]);//开始求n-2个三角形的面积 } ans /= 50; int hub = (int)ans; printf("%d\n", hub); } return 0;}
0 0
- poj 3348 Cows
- POJ 3348 Cows
- poj 3348 Cows
- poj 3348 Cows
- POJ 3348 Cows
- poj 3348 Cows
- POJ 3348 Cows
- POJ 3348 Cows
- POJ 3348 Cows
- poj 3348 Cows
- POJ 3348 Cows
- POJ-3348:Cows
- poj 3348 Cows(求凸包面积)
- POJ 3348 Cows(凸包)
- POJ 3348 Cows(凸包求面积)
- POJ 3348 Cows 求凸包面积
- POJ 3348 || Cows(求凸包面积
- POJ 3348 Cows (凸包)
- SGU 223 little kings BSOJ2772 状压DP
- leetcode 167 Two Sum II - Input array is sorted C++
- struts令牌机制
- Java中反射知识的理解介绍
- Scala10
- poj 3348 Cows
- Hive安装
- transaction简介
- 2016年之年中总结
- Best Time to Buy and Sell Stock II
- 用Java语言写的万年历的小程序
- 《学术研究,你的成功之道》 读书笔记
- 删除rman备份
- 创建虚拟sdcard.img + 读取sdcard卡中的文件路径有效