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

xtu summer individual-4 B - Party All the Time

时间:2014-08-05 09:34:18      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   for   

Party All the Time

Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 4355
64-bit integer IO format: %I64d      Java class name: Main
 
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<=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

Source

 
 
 
解题:三分,三分,三分,三分。。。。。。。。
 
 
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f3f
14 using namespace std;
15 const int maxn = 50010;
16 const double exps = 1e-7;
17 int n;
18 double x[maxn],w[maxn];
19 double test(double px){
20     double sum = 0;
21     for(int i = 0; i < n; i++)
22         sum += fabs(px-x[i])*fabs(px-x[i])*fabs(px-x[i])*w[i];
23     return sum;
24 }
25 int main(){
26     int t,i,j,k = 1;
27     double low,high,mid,midd;
28     scanf("%d",&t);
29     while(t--){
30         scanf("%d",&n);
31         low = INF;
32         high = -INF;
33         for(i = 0; i < n; i++){
34             scanf("%lf %lf",x+i,w+i);
35             low = min(low,x[i]);
36             high = max(high,x[i]);
37         }
38         while(fabs(high - low) >= exps){
39             mid = (high+low)/2.0;
40             midd = (high+mid)/2.0;
41             if(test(mid)+exps < test(midd))
42                 high = midd;
43             else low = mid;
44         }
45         printf("Case #%d: %.0f\n",k++,test(low));
46     }
47     return 0;
48 }
View Code

 

xtu summer individual-4 B - Party All the Time,布布扣,bubuko.com

xtu summer individual-4 B - Party All the Time

标签:style   blog   http   color   java   os   io   for   

原文地址:http://www.cnblogs.com/crackpotisback/p/3891470.html

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