Party All the Time

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


 

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

 

解题报告:

     因为凸函数+凸函数=凸函数,又因为单个(|xi-x|)^3 * wi是凸函数,所以整体以后也是凸函数,所以是单峰函数找最小值,所以可以用三分。

AC代码:

#include<bits/stdc++.h>

using namespace std;
const double eps = 1e-6;
double x[50000 + 5];
double w[50000 + 5];
int n;
double cal (double a) {
	double ans=0;
	for(int i = 1; i<=n; i++) {
		ans+= fabs( (x[i]-a) * (x[i]-a) *(x[i]-a) ) * w[i];
	}
	return ans;
}

int main()
{
	int t,iCase = 0;
	cin>>t;
	while(t--) {
		scanf("%d",&n);
		for(int i = 1; i<=n; i++) {
			scanf("%lf %lf",&x[i],&w[i]);
		}
		double l = x[1];
		double r = x[n];
		double mid,midd;///mid是左侧三分,midd是右侧三分 
		while(r-l>eps) {
			mid = (r-l)/3 + l;
			midd = r- (r-l)/3;
			if(cal(mid)>cal(midd) ) l=mid;
			else r=midd;
		}
		printf("Case #%d: %.0f\n",++iCase,cal(l));
	}
	
	return 0 ;
}

 

法2:求导后使用二分:

AC代码:


#include<stdio.h>
 
double list1[50000];
double list2[50000];
 
int main()
{
	int t, n;
	scanf("%d", &t);
	double low, high, mid;
	double sum;
	int temp;
	for (int ii = 0; ii < t;ii++)
	{
		scanf("%d", &n);
		low = 1000000;
		high = -1000000;
		for (int i = 0; i < n; i++)
		{
			scanf("%lf%lf", &list1[i], &list2[i]);
			if (low > list1[i])low = list1[i];
			if (high<list1[i])high = list1[i];
		}
		mid = (low + high) / 2;		//关键
		while (low + 0.0000001 < high)
		{
			mid = (low + high) / 2;
			sum = 0;
			for (int i = 0; i < n; i++)
			{
				if (list1[i] < mid)sum += (list1[i] - mid)*(list1[i] - mid)*list2[i];
				else sum -= (list1[i] - mid)*(list1[i] - mid)*list2[i];
			}
			if (sum>0)high = mid;
			else low = mid;
		}
		sum = 0;
		for (int i = 0; i < n; i++)
		{
			temp = 1;
			if (list1[i] < mid)temp = -1;
			sum += temp*(list1[i] - mid)*(list1[i] - mid)*(list1[i] - mid)*list2[i];
		}
		printf("Case #%d: %.0f\n", ii + 1, sum);
	}
	return 0;
}

总结:

   1.注意写三分的时候,如果你想用mid或者midd,而不是l或r来作为最后的答案(其实都可以啊需要因为你都r-l<eps了),先初始化mid和midd,不然有可能进不去while循环,以至于得出错误的答案。