hdu 4355 Party All the Time(三分搜索)

Problem Description
In the Dark forest, there is a Fairy kingdom where all the spirits will go together and Celebrate the harvest every year. But there is one thing you may not know that they hate walking so much that they would prefer to stay at home if they need to walk a long way.According to our observation,a spirit weighing W will increase its unhappyness for S3*W units if it walks a distance of S kilometers.
Now give you every spirit's weight and location,find the best place to celebrate the harvest which make the sum of unhappyness of every spirit the least.
Input
The first line of the input is the number T(T<=), which is the number of cases followed. The first line of each case consists of one integer N(<=N<=), indicating the number of spirits. Then comes N lines in the order that x[i]<=x[i+] for all i(<=i<N). The i-th line contains two real number : Xi,Wi, representing the location and the weight of the i-th spirit. ( |xi|<=, <wi< )
 
Output
For each test case, please output a line which is "Case #X: Y", X means the number of the test case and Y means the minimum sum of unhappyness which is rounded to the nearest integer.
 
Sample Input

0.6
3.9
5.1
8.4
 
Sample Output
Case #: 
 
Author
Enterpaise@UESTC_Goldfinger
 
Source
 

根据题意如果中心为X0,Y0,那么结果为sigma(|Xi-X0|^3*W)。

这是一个凸函数,因为它的二次导大于0,适用于三分搜索

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={,,-,};
int diry[]={-,,,};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 60000
#define inf 1<<26
struct Node{
double x,w;
}node[N];
int n;
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
double low=inf;
double high=-inf;
for(int i=;i<=n;i++){
scanf("%lf%lf",&node[i].x,&node[i].w);
if(node[i].x<low){
low=node[i].x;
}
if(node[i].x>high){
high=node[i].x;
}
} double sum1,sum2;
double mid1=(low+high)/;
double mid2;
while(fabs(high-low)>=eps){
mid1=(low+high)/;
mid2=(mid1+high)/;
sum1=;
sum2=;
for(int i=;i<=n;i++){
sum1+=fabs(node[i].x-mid1)*fabs(node[i].x-mid1)*fabs(node[i].x-mid1)*node[i].w;
sum2+=fabs(node[i].x-mid2)*fabs(node[i].x-mid2)*fabs(node[i].x-mid2)*node[i].w;
}
if(sum1+eps<sum2){
high=mid2;
}
else{
low=mid1;
}
}
printf("Case #%d: ",++ac);
printf("%.0lf\n",sum1); }
return ;
}
上一篇:js排序的方法


下一篇:javascript 高级程序设计 一