hdu 4355 简单三分找最小值

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std;
#define exp 1e-6

int n;
double pos[50010],weight[50010];
double min(double a,double b)
{
    return a<b?a:b;
}
double query(double x)
{
    int i;
    double sum=0;
    for(i=1;i<=n;i++)
    {
        double k=pos[i]-x;
        if(k<0) k=-k;
        sum+=k*k*k*weight[i];
    }
    return sum;
}
int main()
{
    int T,i,j,d=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%lf%lf",&pos[i],&weight[i]);
        }
        double left=pos[1];
        double right=pos[n];
        while(right-left>exp)
        {
            double mid=(2*left+right)/3;
            double mmid=(left+2*right)/3;
            if(query(mid)>query(mmid))
            {
                left=mid;
            }
            else right=mmid;
        }
        printf("Case #%d: %.0lf\n",d++,query(left));
    }
    return 0;
}
时间: 2024-08-12 00:31:37

hdu 4355 简单三分找最小值的相关文章

hdu 2899 简单三分

三分就是在二分的基础上的进一步确定区间值      先把区间分为三分            然后更新左右区间值 #include<stdio.h> #include<string.h> #include<iostream> using namespace std; #define exp 1e-6 double y; double pow(double a,int b) { int i; double x=1; for(i=1;i<=b;i++) { x*=a;

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 4355 Party All the Time 三分算法

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

HDU 4355 Party All the Time 三分

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

hdu 4952 Number Transformation (找规律)

题目链接 题意:给你个x,k次操作,对于第i次操作是:要找个nx,使得nx是>=x的最小值,且能整除i,求k次操作后的数 分析: 经过打表找规律,会发现最后的x/i,这个倍数会趋于一个固定的值,求出这个固定的值和K相乘就可以了, 为什么会趋于固定的值呢,因为最后虽然i在不断增长,但是x也是在增长的,每次的倍数会回退一个发现 有余数,然后再加上一个,所以趋于稳定. 官方题解: 1 #include <iostream> 2 #include <cstdio> 3 #includ

HDU 1253 (简单三维广搜) 胜利大逃亡

奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz 而且我也优化过了的啊,尼玛还是一直爆! 先把代码贴上睡觉去了,明天再来弄 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 #include <cmath> 7 using namespace std; 8 9 struct Poin

hdu 1087 简单dp

思路和2391一样的.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int inf=(0x7f7f7f7f); int main() { int a; int s[10005]; int w[10005];

HDU 4925 Apple Tree 找呀找规律

间隔着取_(:зゝ∠)_ #include <iostream> #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; int n, m; int init(int i, int j) { int cnt = 1; if(i-1 >= 1) cnt *= 2; if(i+1 <= n) cnt *= 2; if(j-1 >= 1) cnt *=