POJ 3348:Cows 凸包+多边形面积
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7739 | Accepted: 3507 |
Description
Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are forced to save money on buying fence posts by using trees as fence posts wherever possible. Given the locations
of some trees, you are to help farmers try to create the largest pasture that is possible. Not all the trees will need to be used.
However, because you will oversee the construction of the pasture yourself, all the farmers want to know is how many cows they can put in the pasture. It is well known that a cow needs at least 50 square metres of pasture to survive.
Input
The first line of input contains a single integer, n (1 ≤ n ≤ 10000), containing the number of trees that grow on the available land. The next n lines contain the integer coordinates of each tree given as two integers x and y separated
by one space (where -1000 ≤ x, y ≤ 1000). The integer coordinates correlate exactly to distance in metres (e.g., the distance between coordinate (10; 11) and (11; 11) is one metre).
Output
You are to output a single integer value, the number of cows that can survive on the largest field you can construct using the available trees.
Sample Input
4
0 0
0 101
75 0
75 101
Sample Output
151
题意是给出各个树的位置,用这些树可以围出一块最大的面积,一头牛需要50平方米的面积,问这些树围出的面积可以养多少头牛。
先构造凸包,在求出凸包围成的面积,最后除以50求整。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct no
{
int x, y;
}node[10005]; int n;
int conbag[10005]; no orign;//原点 int dis(no n1, no n2)
{
return (n1.x - n2.x)*(n1.x - n2.x) + (n1.y - n2.y)*(n1.y - n2.y);
} int xmult(int x1, int y1, int x2, int y2)
{
return x1*y2 - x2*y1;
} int Across(no n1, no n2, no n3, no n4)
{
return xmult(n2.x - n1.x, n2.y - n1.y, n4.x - n3.x, n4.y - n3.y);
} int cmp(const void* n1, const void* n2)
{
int temp = Across(orign, *(no *)n1, orign, *(no *)n2); if (temp > 0)
{
return -1;
}
else if (temp == 0)
{
return (dis(orign, *(no *)n1) - dis(orign, *(no *)n2));
}
else
{
return 1;
}
} int main()
{ int i, j, k, min_x, pos_x;
double sum;
while (scanf("%d", &n) != EOF)
{
min_x = 1005;
for (i = 1; i <= n; i++)
{
cin >> node[i].x >> node[i].y;
if (node[i].x < min_x)
{
min_x = node[i].x;
pos_x = i;
}
else if (min_x == node[i].x&&node[i].y < node[pos_x].y)
{
pos_x = i;
}
}
orign = node[pos_x];
qsort(node + 1, n, sizeof(no), cmp); int pc = 1;
conbag[1] = 1;
conbag[++pc] = 2;
conbag[0] = 2; i = 3;
while (i <= n)
{
if (Across(node[conbag[pc - 1]], node[conbag[pc]], node[conbag[pc]], node[i]) >= 0)
{
conbag[++pc] = i++;
conbag[0]++;
}
else
{
pc--;
conbag[0]--;
}
} if (n < 3 || conbag[0]<3)
{
cout << 0 << endl;
}
else
{
sum = 0;
for (i = 1; i + 1 <= conbag[0]; ++i)
{
sum += abs((node[conbag[i]].x * node[conbag[(i + 1)]].y - node[conbag[i]].y * node[conbag[(i + 1)]].x));
}
sum += abs((node[conbag[i]].x * node[1].y - node[conbag[i]].y * node[1].x));
sum = sum / 2.0;
cout << (int)(sum / 50) << endl;
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3348:Cows 凸包+多边形面积的更多相关文章
- POJ 3348 Cows 凸包 求面积
LINK 题意:给出点集,求凸包的面积 思路:主要是求面积的考察,固定一个点顺序枚举两个点叉积求三角形面积和除2即可 /** @Date : 2017-07-19 16:07:11 * @FileNa ...
- poj3348 Cows 凸包+多边形面积 水题
/* poj3348 Cows 凸包+多边形面积 水题 floor向下取整,返回的是double */ #include<stdio.h> #include<math.h> # ...
- poj 3348 Cows 凸包 求多边形面积 计算几何 难度:0 Source:CCC207
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7038 Accepted: 3242 Description ...
- POJ 3348 - Cows 凸包面积
求凸包面积.求结果后不用加绝对值,这是BBS()排序决定的. //Ps 熟练了template <class T>之后用起来真心方便= = //POJ 3348 //凸包面积 //1A 2 ...
- POJ 3348 Cows (凸包模板+凸包面积)
Description Your friend to the south is interested in building fences and turning plowshares into sw ...
- POJ 3348 Cows [凸包 面积]
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9022 Accepted: 3992 Description ...
- POJ 3348 Cows | 凸包模板题
题目: 给几个点,用绳子圈出最大的面积养牛,输出最大面积/50 题解: Graham凸包算法的模板题 下面给出做法 1.选出x坐标最小(相同情况y最小)的点作为极点(显然他一定在凸包上) 2.其他点进 ...
- POJ 3348 Cows | 凸包——童年的回忆(误)
想当年--还是邱神给我讲的凸包来着-- #include <cstdio> #include <cstring> #include <cmath> #include ...
- poj 3348 Cow 凸包面积
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8122 Accepted: 3674 Description ...
随机推荐
- 关于Java大整数是否是素数
题目描述 请编写程序,从键盘输入两个整数m,n,找出等于或大于m的前n个素数. 输入格式: 第一个整数为m,第二个整数为n:中间使用空格隔开.例如: 103 3 输出格式: 从小到大输出找到的等于或大 ...
- druid监控sql完整版
利用Druid实现应用和SQL监控 一.关于Druid Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. D ...
- HTML中的ul标签
UL格式: <ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li> < ...
- Plastic Sprayer Manufacturer - Sprayer: How Does It Work?
The Plastic Sprayers Manufacturer states that a sprayer is a device that uses a basic pump me ...
- js数组去重解决方案
js数组去重是前端面试中经常被问的题目,考察了面试者对js的掌握,解决问题的思路,以及是否具有专研精神.曾经一位前端界大神告诉我,解决问题的方式有很多种,解决问题时多问问自己还有没有其他的方法,探求最 ...
- python 连接oracle基础环境配置方法
配置基础: 1.python3.7 2.oracle server 11g 64位 3.PLSQL 64位 4.instantclient-basic-windows.x64-11.2.0.4.0这个 ...
- Java基础 -4.2
Switch分支语句 switch是一个开关语句,它主要是根据内容来进行判断的,需要注意的是switch中可以判断的只能够是数据(int.char.枚举.String)而不能够使用逻辑判断 publi ...
- bat批处理下如何像shell一样将命令执行的效果赋值给变量
在bat下如何实现像shell一样,把执行命令行后的结果赋值给变量呐? 刚开始,可真难为到我了.随着对bat批处理知识熟悉的加深. 学习到了!!! 举个 栗子: svnlook uuid C:\R ...
- Atcoder Grand Contest 037C(贪心,优先队列,思维)
#define HAVE_STRUCT_TIMESPEC//编译器中time.h和phread.h头文件中timespec结构体重名,故加此行#include<bits/stdc++.h> ...
- 【转】 android之如何在两个activity之间传递handler_利用broadcast广播机制
原文:http://blog.csdn.net/jason0539/article/details/18075293 这算是如何在两个activity之间传递handler的解决方案二了,解决方案一见 ...