【Gym - 100482B Farmer 】 思维

这篇博客探讨了Gym中的编号为100482B的农夫问题,重点在于分析问题背后的思维策略和解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Farmer


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?

Input

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.

Output

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.

Example
Input
1
0 0 0 10 10 10 10 0 100
Output
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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值