hihoCoder挑战赛16 A—— 王胖浩与三角形
2015-11-02 14:50
246 查看
思路:开始没有思路,想到了用三边乱搞(每条边按照比例增加)然而样例都无法通过。后来想到了海伦公式sqrt((a+b+c)(a+b-c)(b+c-a)(a+c-b))/4,那么这样以来就是让这个三角形趋于正三角形了,即三边的方差最小,那么依次是先补最短,次短,长(贪心的算法)。
code:
code:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <sstream> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; const int INF=0x3fffffff; const int inf=-INF; const int N=1000000; const int M=2005; const int mod=1000000007; const double pi=acos(-1.0); #define cls(x,c) memset(x,c,sizeof(x)) #define cpy(x,a) memcpy(x,a,sizeof(a)) #define fr(i,s,n) for (int i=s;i<=n;i++) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lrt rt<<1 #define rrt rt<<1|1 #define middle int m=(r+l)>>1 #define lowbit(x) (x&-x) #define pii pair<int,int> #define mk make_pair #define IN freopen("in.txt","r",stdin); #define OUT freopen("out.txt","w",stdout); double a[3],l; double sd(double a,double b,double c) { double p=(a+b+c)*0.5; return sqrt(p*(p-a)*(p-b)*(p-c)); } double sol() { double t=min(l,a[1]-a[0]); a[0]+=t,l-=t; if (l>0) { t=min(l*0.5,(a[2]-a[1])); a[1]+=t; a[0]+=t; l-=t*2; //cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl; if (l>0) { fr(i,0,2) a[i]+=1.0*l/3; } } //cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl; return sd(a[0],a[1],a[2]); } int main() { int T; scanf("%d",&T); while (T--) { scanf("%lf %lf %lf %lf",&a[0],&a[1],&a[2],&l); sort(a,a+3); printf("%.10f\n",sol()); } }
相关文章推荐
- 背包问题
- 贪心法删数问题
- Moving Tables
- Tian Ji -- The Horse Racing
- Saving HDU
- 悼念512汶川大地震遇难同胞——老人是真饿了
- 贪心法求解背包问题(物品可分)
- 贪心法求解背包问题(部分背包 物品可分)
- poj 1328:Radar Installation
- POJ 3069
- Best Time to Buy and Sell Stock
- C陷阱与缺陷笔记
- 2008 Round1AA Minimum Scalar Product
- 编程算法 - 硬币问题 代码(C)
- 编程算法 - 区间调度问题 代码(C)
- 编程算法 - 最好牛线(Best Cow Line) 代码(C)
- 编程算法 - 萨鲁曼的军队(Saruman's Army) 代码(C)
- 编程算法 - 篱笆修理(Fence Repair) 代码(C)
- uva 1614——UVA 1614 - Hell on the Markets
- uva 10570——Meeting with Aliens