Party All the Time
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3101 Accepted Submission(s): 978
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 S
3*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.
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<=20), which is the number of cases followed. The first line of each case consists of one integer N(1<=N<=50000), indicating the number of spirits. Then comes N lines in the order that x
[i]<=x
[i+1] for all i(1<=i<N). The i-th line contains two real number : X
i,W
i, representing the location and the weight of the i-th spirit. ( |x
i|<=10
6, 0<w
i<15 )
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
1 4 0.6 5 3.9 10 5.1 7 8.4 10
Sample Output
Case #1: 832
题意:n个人,都要去参加活动,每个人都有所在位置xi和Wi,每个人没走S km,就会产生S^3*Wi的“不舒适度”,求在何位置举办活动才能使所有人的“不舒适度”之和最小,并求最小值。
分析:显然,举办活动的位置在最左侧的那个人和最右侧那个人之间,而且从最左侧到最右侧,所有人的Si^3*Wi之和先减小后增大,故可对位置从最左侧的那个人位置到最右侧那个人位置三分。
还是要注意精度,精度很容易出问题。这里如果怕精度容易错的话,也可以用for循环来进行三分,只要循环三分一定的次数即可。二分也同样如此。
AC代码:
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
const double eps = 1e-12;
struct Node
{
double x;
double w;
}node[50010];
int n;
double calc(double xx)
{
double res=0;
for(int i=1;i<=n;i++)
{
double d=xx-node[i].x;
if(d<0) d*=-1;
res+=d*d*d*node[i].w;
}
return res;
}
int main()
{
int t;
scanf("%d",&t);
int count=1;
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf%lf",&node[i].x,&node[i].w);
double l=node[1].x;
double r=node[n].x;
double mid,midmid;
double mid_area;
double midmid_area;
while(l+eps<r)
{
mid=(l+r)/2;
midmid=(mid+r)/2;
mid_area=calc(mid);
midmid_area=calc(midmid);
if(mid_area<midmid_area)
r=midmid;
else
l=mid;
}
double g=calc(l);
double h=calc(r);
if(g<h)
printf("Case #%d: %.0lf\n",count++,g);
else
printf("Case #%d: %.0lf\n",count++,h);
}
return 0;
}