HDU 4355

View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #define EPS 1e-4
 6 #define MAXN 50004
 7 using namespace std;
 8 double x[MAXN],w[MAXN];
 9 
10 inline double get_f1(double xx, int start, int n)
11 {
12      double sum(0);
13      for (int i(start); i<n; ++i) {
14           sum += (xx - x[i])*(xx - x[i])*w[i];   
15      }       
16      return sum;
17 }
18 
19 inline double get_res(double xx, int n)
20 {
21      double sum(0);
22      for (int i(0); i<n; ++i) {
23           sum += fabs((xx - x[i])*(xx - x[i])*(xx - x[i]))*w[i];
24      }
25      return sum;       
26 }
27 
28 inline int cmp(double x)
29 {
30     if (x > EPS)return 1;
31     if (x < -EPS)return -1;
32     return 0;    
33 } 
34 
35 int main()
36 {
37     int n;
38     int t;
39     scanf("%d",&t);
40     for (int cnt(1); cnt<=t; ++cnt) {
41         cout<<"Case #"<<cnt<<": ";
42         int n;
43         scanf("%d",&n);
44         for (int i(0); i<n; ++i) {
45             scanf("%lf%lf",&x[i],&w[i]);    
46         }    
47         
48         double l(x[0]),r(x[n-1]),mid(0);
49         while (true) {
50             mid = (l + r)/2.0;
51             int midx = upper_bound(x,x+n,mid) - x;
52             double fir(get_f1(mid,0,midx)),sec(get_f1(mid,midx,n));
53             if (cmp(fir - sec) > 0)r = mid;
54             else if (cmp(fir - sec) < 0)l = mid;
55             else break;      
56         }
57         __int64 ans = get_res(mid,n) + 0.5;
58         printf("%I64d\n",ans);
59     }
60     return 0;    
61 }
posted on 2012-08-11 22:26  Dev-T  阅读(253)  评论(0编辑  收藏  举报