hdu 4355 Party All the Time 初识三分法

原创 2016年05月31日 01:09:00

以前听到三分法的时候总是一脸懵逼,心想三分了怎么继续确定区间,三分的两个点又怎么确定?想破头也想不出来。

今天偷偷看了西工大某美女姐姐(好吧,虽然没见过,就这么叫吧)的博客,发现三分竟然是有特定条件的……

先来说说三分法,据前人的研究,三分法适用于凹凸函数求它的凹凸点。与二分法不同的是三分法将mid点与r点的中间点也并入讨论,设其为mmid点。

显然,mmid>mid.

以凹函数为例子,求它的最大值点。当f(mid)>f(mmid)时,mmid点一定在最大值点的右端,否则mmid将小于mid,矛盾。此时可以将R端点移动到mmid点。

同理,当f(mid)<f(mmid)时,将L端点移动到mid点,可以保证是向最大值点逼近。


Party All the Time

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


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
Enterpaise@UESTC_Goldfinger
 

题意,在一排路上有n个ctr,每个ctr有一个重量,已知将在x[i]点处的ctr移动到x点需要花费(x[i]-x)^3*w[i]的力气,现求出将所有ctr移动到一点的最小费用

首先证明三分的可行性:

这个题目的费用函数可以写为∑fabs(x[i]-x)^3*w[i],(i=1,2……n)考虑(x[i]-x)^3的凹凸性,求二阶导数为-6x-2x[i],显然小于零,此为凸函数,有最小值。


代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int ca,t,i,j,k,m,n;
double x[50004],w[50004];
double f(double x1)
{   double sum=0;
  for(i=1;i<=n;i++)
  { double po=fabs(x[i]-x1);
  	sum+=w[i]*po*po*po;
  }
  	return sum;
}


int main()
{
	
	scanf("%d",&t);
	for(ca=1;ca<=t;ca++)
	{
		scanf("%d",&n);
		memset(x,0,sizeof(x));
		memset(w,0,sizeof(w));
		for(i=1;i<=n;i++)scanf("%lf%lf",&x[i],&w[i]);
		double l=x[1],r=x[n];
		double mid=(l+r)/(2.0);
		double mmid=(r+mid)/(2.0);
		while(r-l>1e-7)
		{
			if(f(mid)<f(mmid))
			{
				r=mmid;
				mid=(l+r)/(2.0);
				mmid=(r+mid)/(2.0);
			}
			else
			{
				l=mid;
				mid=(l+r)/(2.0);
				mmid=(r+mid)/(2.0);
			}
		}
		printf("Case #%d: %.0f\n",ca,min(f(l),f(r)));
	}
	return 0;
}
 


好吧,还是想吐槽一下最近的悲惨生活……先是手机被偷……再是电脑硬盘即将寿终正寝(写这篇博客的时候硬盘又卡住了好几次orz)……今天又听闻家里座机半月前被电信当成无人号码卖给了另外一家人,现在能正确打电话回家都要算概率……数学建模得了个谢谢参与,数学分析什么都不会,初等玄学(高等代数)依然玄学中,物理不看书做题战斗力成0,近代史完全记不下来,昨天又听闻我们这渣学校的人,没有国家级以上的奖项,保研的时候好985看都不看,就算考研进复试也会被刷下来orz……前途一片惨淡啊……以后就靠现在多攒点钱在学校门口支个摊卖炸串把……不过吧,每当感到悲惨的时候总有那什么能让我依然对生活充满希望

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

HDU 4355 - Party All the Time(三分)

Problem Description In the Dark forest, there is a Fairy kingdom where all the spirits will go toge...

HDU - 4355 Party All the Time 三分

题意:每个spirit有一个位置xi一个全中w[i],如果确定聚会地点为s,则i的花费是 fabs(s - x[i]) ^ 3 * w[i],求总花费。 三分 对位置xi进行三分,即把区间分为长度相等...

HDU 4355 Party All the Time(三分)

题目链接:HDU 4355 题面: Party All the Time Time Limit: 6000/2000 MS (Java/Others)    Memory Lim...

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

#include #include #include #include #include #include #include #include #include #...

HDU 4355 Party All the Time (三分算法的应用(含三分算法的讲解))

Party All the Time Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...

三分法:Party all the time

Party All the Time Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...
  • lethic
  • lethic
  • 2012年08月10日 10:50
  • 360

POJ 4355 Party All the Time(三分)

Party All the Time Problem Description In the Dark forest, there is a Fairy kingdom where all t...

hdu 4355 Party All the Time

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4355 题意: 小精灵不愿意走路,走路的话会使他们产生不高兴值,如果小精灵走的路是S体重是W那么他走...

hdu4355(三分法)

Party All the Time Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...

hdu_4355 Party All the Time(简单数学题)

http://acm.hdu.edu.cn/showproblem.php?pid=4355 题意: 直线上n个点Xi,每个点有一个权值Wi,求存在某点S使得(abs(s-xi))^3*wi的和最...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 4355 Party All the Time 初识三分法
举报原因:
原因补充:

(最多只允许输入30个字)