poj3348
来源:互联网 发布:炉石大数据 编辑:程序博客网 时间:2024/06/07 09:29
题目大意:
给你n棵树,可以用这n棵树围一个圈,然后在圈里面可以养牛,每个牛需要50平方米的空间,问最多可以养多少牛?
其实就是求一个凸包,计算凸包面积,然后除以50,然后就得到答案,直接上模板了。
凸包这一类型的题目差不多,可以作为模板使用,时间复杂度是NlogN。
代码:
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <cstdlib>using namespace std;struct POINT {int x,y;double angle;}point[10010],stack[10010];int n, top;typedef struct POINT Point;//欧氏距离double Distance(Point p1, Point p2){return sqrt((double)((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)));}//叉积 op_sp × op_epdouble multily(Point sp, Point ep, Point op){return (double)((sp.x - op.x) * (ep.y - op.y) - (ep.x - op.x) * (sp.y - op.y));}//比较函数从小到大int cmp(const void *a, const void *b){Point *c = (Point *)a;Point *d = (Point *)b;if (c->angle > d->angle){return 1;}else if(c->angle < d->angle){return -1;}elsereturn (c->x * c->x + c->y * c->y) < (d->x * d->x + d->y * d->y) ? -1 : 1;}//求凸包void Graham_scan(){int i;stack[0] = point[0];stack[1] = point[1];stack[2] = point[2];for (i = 3; i < n; ++ i){while (multily(point[i], stack[top], stack[top - 1]) > 0){top --;}stack[++ top] = point[i];}}//求出凸包的面积double getArea1(){double sum = 0.0;int i;for (i = 0; i <= top; ++ i){sum += (double)(stack[i].x * stack[(i + 1) % (top + 1)].y - stack[i].y * stack[(i + 1) % (top + 1)].x);}return sum / 2.0;}int main(){double area1;int i, k;while(scanf("%d", &n) != EOF){top = 2;k = 0;for (i = 0; i < n; ++ i){scanf("%d %d", &point[i].x, &point[i].y);}for (i = 1; i < n; ++ i){if (point[i].y < point[k].y || (point[i].y == point[k].y && point[i].x < point[k].x)){k = i;}}if (k){Point tmp = point[0];point[0] = point[k];point[k] = tmp;}for (i = 1; i < n; ++ i){point[i].angle = atan2((double)(point[i].y - point[0].y), (double)(point[i].x - point[0].x));}qsort(point + 1, n - 1, sizeof(point[0]),cmp);Graham_scan();area1 = fabs(getArea1());printf("%d\n", (int)(area1/50.0));}return 0;}
- poj3348
- poj3348
- poj3348
- poj3348
- POJ3348-Cows
- Poj3348 Cows
- 【poj3348】 Cows
- poj3348 Cow
- poj3348 求凸包面积
- POJ3348--cows--凸包
- 求凸包面积:poj3348
- POJ3348——Cows
- POJ3348-凸包
- POJ3348 凸包面积计算
- [POJ3348]Cows(凸包)
- poj3348 Cows【凸包面积】
- poj3348-Cows 求凸包及其面积
- 【POJ3348】【计算几何】Cow 题解
- 反射:修改对象
- 利用setTimeout和SetInterval构建Javascript计时器
- C#中调用Windows API的要点
- 二叉树最近共同祖先问题
- tony之linux driver_LDD3_scull字符设备驱动编译在新内核编译问题
- poj3348
- C#学习日志(7)
- QT API的继承类图(看QT API的前提)
- [求职交流|转] 微软公司的三道面试题目看了会有意想不到的收获
- 自定义报表开发平台
- 再次向逆向工程挺进
- 黑马程序员_网络编程
- ORA-600错误[kkoipt:invalid join method]
- 我的第一篇博客