John is a successful farmer and he would like to expand his business. For this reason he is going to buy a new plot of land to grow even more crops (and earn even more money). Currently there are T (0 ≤ T ≤ 1000) plots on sale and John wants to find the best deal. He considers deal the best if the price per area unit is the lowest. Can you help him by coding a solution that computes price per area unit?
First line contains the number of plots T (0 ≤ T ≤ 1000). Each plot defined is as a quadrilateral by 4 integer points on the 2D plane (in clockwise or counterclockwise order), meaning there are 8 integers (32-bit) in total which describe geometry and location of the plot. Last number on the line represents the price of plot.
For each plot output a line “Case #tc: x”, where tc is plot’s sequence number (starting from 1) and x is price per unit for the tc-th plot rounded to two decimal places.
1 0 0 0 10 10 10 10 0 100
Case #1: 1.00
题意:给出一个四边形的四个顶点坐标,求出其面积s,使得x/s值最小。
分析:将四边形分为两个三角形的面积计算,但要注意考虑凹四边形的情况,所以需要分为两种情况取面积最大值。
代码如下:
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
double dis(double x1, double y1, double x2, double y2){
return (sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)));
}
int main(){
int t;
scanf("%d", &t);
for(int cas = 1; cas <= t; cas++){
double x1, x2, x3, x4, y1, y2, y3, y4, m, ans;
scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4, &m);
double a = dis(x1, y1, x2, y2);
double b = dis(x2, y2, x3, y3);
double c = dis(x1, y1, x3, y3);
double p = (a+b+c)/2;
double s = sqrt(p*(p-a)*(p-b)*(p-c));
a = dis(x1, y1, x4, y4);
b = dis(x3, y3, x4, y4);
p = (a+b+c)/2;
s += sqrt(p*(p-a)*(p-b)*(p-c));
ans = m/s;
a = dis(x1, y1, x2, y2);
b = dis(x1, y1, x4, y4);
c = dis(x2, y2, x4, y4);
p = (a+b+c)/2;
s = sqrt(p*(p-a)*(p-b)*(p-c));
a = dis(x2, y2, x3, y3);
b = dis(x3, y3, x4, y4);
p = (a+b+c)/2;
s += sqrt(p*(p-a)*(p-b)*(p-c));
ans = max(ans, m/s);
printf("Case #%d: %.2lf\n", cas, ans);
}
return 0;
}