HDU 4355——Party All the Time——————【三分求最小和】

Party All the Time

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4282    Accepted Submission(s): 1355

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<=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

[email protected]_Goldfinger

Source

2012 Multi-University Training Contest 6

题目大意:n个人要在某一条线段某位置聚会,这n个人开始有一个坐标xi,一个体重wi,每个人到该位置距离设为S。让你算所有人到该位置的S^3*w的和最小。求出最小和。

解题思路:套用三分求解。

#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
const int maxn=1e5+200;
int n;
const int Mv=1e6;
const double eps=1e-2;
struct Spirit{
    double x;
    double w;
}spirits[5*maxn];
double Abs(double xx){
    return xx>0?xx:-xx;
}
double Pow(double x,int nn){
    double ret=1.0;
    for(int i=1;i<=nn;i++)
        ret*=x;
    return ret;
}
double cal(double xx){
    double sum=0;
    for(int i=1;i<=n;i++){
        sum+=Pow(Abs(spirits[i].x-xx),3)*spirits[i].w;
    }
    return sum;
}
double three_div(double L,double R){    //三分求最值
    double mid=(L+R)/2,mid_L=(L+mid)/2;
    while(Abs(cal(mid)-cal(mid_L))>eps){ //条件应该视情况而定
        mid=(R+L)/2.0;
        mid_L=(L+mid)/2.0;
        if(cal(mid)>cal(mid_L)){
            R=mid;
        }else{
            L=mid_L;
        }
    }
    return mid; //得到最值的坐标位置
}
int main(){
    int t,cnt=0;
    scanf("%d",&t);
    double min_v=Mv*(-1.0),max_v=Mv*1.0;
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%lf%lf",&spirits[i].x,&spirits[i].w);
        }
        double xx=three_div(min_v,max_v);
        printf("Case #%d: %lld\n",++cnt,(INT)(cal(xx)+0.5));
    }
    return 0;
}

  

时间: 2024-10-12 08:33:23

HDU 4355——Party All the Time——————【三分求最小和】的相关文章

HDU 4355 Party All the Time 三分

很长时间都是在学习各位大神的力作,并汲取了不少养料,在此一并谢过各位大神了. 当然了,好东西是要跟大家一起分享的,最近发现了几个非常不错的个人站点,都是介绍IOS开发的,其中有唐巧.破船之长.池建强.王维等各位,其中不乏供职于腾讯和阿里这样的IT巨头,希望大家也能从他们的博客中学习到一些技术之外的东西.就不再啰嗦啦,附上地址:http://www.ityran.com/archives/4647 这几天在学习IOS7 CookBook,因为没有找到中文版,就硬着头皮啃原著吧,还真学到了不少东西,

HDU 4355 Party All the Time 三分算法

HDU 4355 Party All the Time 三分算法 题意 给你N个人的位置x和相应重量w,他们要到达同一个位置p,他们每个人的花费的精力等于\(|s[i]-p|^{3}*w\),然后我们需要求一个位置,使得所有人的花费之和最小. 解题思路 根据上面的公式,我们可以知道这个函数不是一个简单的单调函数,最起码是个凹函数(这里需要一个数学上的知识),对于一般情况我们会使用二分法来进行处理,但是这里不是单调函数了,而是一个凹函数,这样我们就不能用二分了,新的算法应运而生--三分算法. 三分

HDU ACM 2438 -&gt;Turn the corner 三分求最值

分析:主要参考http://m.blog.csdn.net/blog/yinzm520/22721285这里的解题方法. 关键是要找到小车的运动状态,下面是分析和公式推导: 在小车转弯过程中,黄线是不断地变化的,变化规律是先增大再减小.所以抓住这一点,用三分法.先找一个变量,角度sita(就是上图中用红色标记的那个角),之后就是一系列的推导,算出黄线的长度.角度的范围是(0,pi/2). 当三分找出最长的黄线长度之后,使之与Y做比较,当它小于Y时,就说明能够通过了 最终可得到:f(angle)=

HDU 2795——Billboard——————【单点更新、求最小位置】

Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2795 Appoint description:  System Crawler  (2015-04-10) Description At the entrance to the university, there is a huge rectangular billb

(KMP 1.5)hdu 1358 Period(使用next数组来求最小循环节——求到第i个字符的循环节数)

题目: Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3813    Accepted Submission(s): 1862 Problem Description For each prefix of a given string S with N characters (each character has an A

HDU1394 线段树求最小逆序数

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1394 求最小的逆序数,在此贴下逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数.逆序数为偶数的排列称为偶排列:逆序数为奇数的排列称为奇排列.如2431中,21,43,41,31是逆序,逆序数是4,为偶排列. 也是就说,对于n个不同的元素,先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规

codeforces 782B The Meeting Place Cannot Be Changed+hdu 4355+hdu 2438 (三分)

B. The Meeting Place Cannot Be Changed The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction. At some points on the road there are n

HDU 4355 Party All the Time(三分)

题目链接:HDU 4355 题面: Party All the Time Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5266    Accepted Submission(s): 1625 Problem Description In the Dark forest, there is a Fairy kingdom where

Hdu 2899 - Strange fuction 二分/三分求函数极值点

Strange fuction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4527    Accepted Submission(s): 3251 Problem Description Now, here is a fuction: F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=