【Gym - 100482B Farmer 】 思维
2017-08-01 13:18
274 查看
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
Output
题意:给出一个四边形的四个顶点坐标,求出其面积s,使得x/s值最小。
分析:将四边形分为两个三角形的面积计算,但要注意考虑凹四边形的情况,所以需要分为两种情况取面积最大值。
代码如下:
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostrea
4000
m>
#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;
}
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 <iostrea
4000
m>
#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;
}
相关文章推荐
- GYM 101128 A.Promotions【思维+暴力】
- Gym - 101572D Distinctive Character bfs + 思维 好题
- B - Weird Cryptography Gym - 100935B(集合,思维题)
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
- Gym - 101503J Numbers Painting(素数分解+思维)
- Gym 100541 B. Sum 分块的技巧、思维题、Interesting
- Problem D. Dales and Hills - Gym - 101411D 【动态规划经典题 - 思维】
- Gym - 101246H ``North-East''【思维+nlogn LIS】
- Gym 101572D Distinctive Character【思维+Bfs】
- GYM - 100812G Short Path (SPFA+思维)
- 【思维 && 几何】Gym - 101063K Dire, Dire Docks
- Gym - 101061A 思维题
- GYM - 100812A Tale of Debt (思维好题)
- Gym 100851A Adjustment Office (思维)
- Gym 101470J Stange Antennas【思维】
- Gym - 101097I Sticks(思维)
- 思维-Gym - 101341B-好题(5-5-B)
- Gym-100712J 桶排序思想&反向思维
- Gym 100247B Similar Strings(哈希+思维)
- 【思维】Gym - 101090I Painting the natural numbers