标签:
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4282 Accepted Submission(s): 1355
#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
const int maxn=1e5+200;
int n;
const int Mv=1e6;
const double eps=1e-2;
struct Spirit{
double x;
double w;
}spirits[5*maxn];
double Abs(double xx){
return xx>0?xx:-xx;
}
double Pow(double x,int nn){
double ret=1.0;
for(int i=1;i<=nn;i++)
ret*=x;
return ret;
}
double cal(double xx){
double sum=0;
for(int i=1;i<=n;i++){
sum+=Pow(Abs(spirits[i].x-xx),3)*spirits[i].w;
}
return sum;
}
double three_div(double L,double R){ //三分求最值
double mid=(L+R)/2,mid_L=(L+mid)/2;
while(Abs(cal(mid)-cal(mid_L))>eps){ //条件应该视情况而定
mid=(R+L)/2.0;
mid_L=(L+mid)/2.0;
if(cal(mid)>cal(mid_L)){
R=mid;
}else{
L=mid_L;
}
}
return mid; //得到最值的坐标位置
}
int main(){
int t,cnt=0;
scanf("%d",&t);
double min_v=Mv*(-1.0),max_v=Mv*1.0;
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lf%lf",&spirits[i].x,&spirits[i].w);
}
double xx=three_div(min_v,max_v);
printf("Case #%d: %lld\n",++cnt,(INT)(cal(xx)+0.5));
}
return 0;
}
HDU 4355——Party All the Time——————【三分求最小和】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4767121.html