码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-23 21:11:02      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:

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 spirits 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 : Xi,Wi, representing the location and the weight of the i-th spirit. ( |xi|<=106, 0<wi<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

 

 

 

Author
Enterpaise@UESTC_Goldfinger
 

 

Source
 

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

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

 

技术分享
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 int dirx[]={0,0,-1,1};
17 int diry[]={-1,1,0,0};
18 #define PI acos(-1.0)
19 #define max(a,b) (a) > (b) ? (a) : (b)  
20 #define min(a,b) (a) < (b) ? (a) : (b)
21 #define ll long long
22 #define eps 1e-10
23 #define MOD 1000000007
24 #define N 60000
25 #define inf 1<<26
26 struct Node{
27     double x,w;
28 }node[N];
29 int n;
30 int main()
31 {
32     int t;
33     int ac=0;
34     scanf("%d",&t);
35     while(t--){
36         scanf("%d",&n);
37         double low=inf;
38         double high=-inf;
39         for(int i=1;i<=n;i++){
40             scanf("%lf%lf",&node[i].x,&node[i].w);
41             if(node[i].x<low){
42                 low=node[i].x;
43             }
44             if(node[i].x>high){
45                 high=node[i].x;
46             }
47         }
48         
49         double sum1,sum2;
50         double mid1=(low+high)/2;
51         double mid2;
52         while(fabs(high-low)>=eps){
53             mid1=(low+high)/2;
54             mid2=(mid1+high)/2;
55              sum1=0;
56             sum2=0;
57             for(int i=1;i<=n;i++){
58                 sum1+=fabs(node[i].x-mid1)*fabs(node[i].x-mid1)*fabs(node[i].x-mid1)*node[i].w;
59                 sum2+=fabs(node[i].x-mid2)*fabs(node[i].x-mid2)*fabs(node[i].x-mid2)*node[i].w;
60             }
61             if(sum1+eps<sum2){
62                 high=mid2;
63             }
64             else{
65                 low=mid1;
66             }
67         }
68         printf("Case #%d: ",++ac);
69         printf("%.0lf\n",sum1);
70         
71     }
72     return 0;
73 }
View Code

 

 

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

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4833443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!