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

题意:

给定一维上若干点xi,每个点有对应的权值wi,求一个点的位置p,使得sigma  fabs(p-xi)^3*wi最小。

解题:

看了网上现有的所有博客,都是直接说是个凸函数,三分一下就好。求导好像也不直观,也不知道具体是怎么证明的,姑且认为是个开口向上的二次函数(因为求的是最小值,且直观上,p的位置肯定是在中间好),那么三分就可以求解了,需要注意的是,这题C++应该是会T的,交G++才能过,然后向最近位取舍,应该用%.0lf输出。

【三分性质】

将区间三等分,取左等分点为lmid,右等分点为rmid,比如此题,二次函数开口向上,那么就算lmid的函数值为lv,rmid的函数值为rmid,比较lv和rv的值,假设最小值不在lmid和rmid之间,在两侧,那么lv,rv哪个更小,其对应的等分点就更接近最值,故而舍弃大的那边。倘若,最小值在lmid和rmid之间,那么无论舍弃哪一边,都不会丢弃最值,同时达到逼近最值的效果,因为最值在何处,我们是不知道的,故而和第一种情况相统一,舍弃较大的一边。

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#define LL long long
#define eps 1e-8
using namespace std;
double x[50010],w[50010];
int n;
double cal(double y)
{
	double tmp,res=0.0;
	for(int i=0;i<n;i++)
	{
		tmp=fabs(y-x[i]);
		res+=tmp*tmp*tmp*w[i];
	}
	return res;
}
int main()
{
    int t,p;
	scanf("%d",&t);
	LL ans;
	for(int i=1;i<=t;i++)
    {
		scanf("%d",&n);
		for(int j=0;j<n;j++)
			scanf("%lf%lf",&x[j],&w[j]);
		double le=-1e6,ri=1e6;
		while(ri-le>eps)
		{
			double lmid,rmid,lv,rv;
			lmid=(ri-le)/3+le;
			rmid=ri-(ri-le)/3;
            lv=cal(lmid);
			rv=cal(rmid);
			if(lv>rv)
				le=lmid;
			else
				ri=rmid;
		}
		printf("Case #%d: %.0lf\n",i,cal(le));
	}
	return 0;
}
时间: 2024-08-01 03:19:46

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\),然后我们需要求一个位置,使得所有人的花费之和最小. 解题思路 根据上面的公式,我们可以知道这个函数不是一个简单的单调函数,最起码是个凹函数(这里需要一个数学上的知识),对于一般情况我们会使用二分法来进行处理,但是这里不是单调函数了,而是一个凹函数,这样我们就不能用二分了,新的算法应运而生--三分算法. 三分

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 思路:三分法求f(x)极值. f(x)是指位置为x时的愤怒值之和,是一个三次函数,且存在极值点使f(x)最小. code: /* * @author Novicer * language : C++/C */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #i

hdu 4717 The Moving Points(三分)

http://acm.hdu.edu.cn/showproblem.php?pid=4717 大致题意:给出每个点的坐标以及每个点移动的速度和方向.问在那一时刻点集中最远的距离在所有时刻的最远距离中最小. 比赛时一直以为是计算几何,和线段相交什么的有关.赛后队友说这是道三分,仔细想了想确实是三分,试着画画图发现它是一个凸性函数,存在一个最短距离.然后三分时间就可以了. #include <stdio.h> #include <iostream> #include <map&g

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

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 hom

HDU 4355 Party All the Time(三分|二分)

题意:n个人,都要去參加活动,每一个人都有所在位置xi和Wi,每一个人没走S km,就会产生S^3*Wi的"不舒适度",求在何位置举办活动才干使全部人的"不舒适度"之和最小,并求最小值. 思路:首先能够得出最后距离之和的表达式最多仅仅有两个极点, 更进一步仅仅有一个极点,否则无最小值. 那么我们就可用三分法或者二分法求解.即对原函数三分或对导数二分就可以.?? #include<cstdio> #include<cstring> #inclu

HDU 4355 数轴上的点找使Si^3*Wi最小的和-浮点数三分

题意:一个数轴上有n个点,现在要找一个点到所有点的距离Si的立方乘以点的权Wi的和最小 分析:三分.浮点数的二分或者三分可以直接用一个数字来限制查找的次数.TLE了几次,把查找次数从10000改到100就过了,本来以为数据范围比较大,100的精度不够. 代码: #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #def

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 wil