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

题意:一个数轴上有n个点,现在要找一个点到所有点的距离Si的立方乘以点的权Wi的和最小

分析:三分。浮点数的二分或者三分可以直接用一个数字来限制查找的次数。TLE了几次,把查找次数从10000改到100就过了,本来以为数据范围比较大,100的精度不够。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define eps 1e-8
#define eps2 1e-5
#define INF  100000000000007
using namespace std;
int t,n;
double x[100005],w[100005];
double ans;
long long ans1;
double dis(double a,double b)
{
	if(a>b){
		double tmp=a;
		a=b;
		b=tmp;
	}
	return pow((b-a),3);

}
double ll(double a)
{
	double sum=0;
	for(int i=0;i<n;i++){
		sum+=dis(a,x[i])*w[i];
	}
	return sum;
}
double find(double left,double right)
{
	double l=left,r=right;
	double ans1,ans2;
	for(int i=0;i<100;i++){
		double mid1=(l+r)/2.0;
		double mid2=(mid1+r)/2.0;
		ans1=ll(mid1);
		ans2=ll(mid2);
		if(ll(mid1)<ll(mid2))  r=mid2;
		else  l=mid1;
	}
	return ans1;
}
int main()
{
	scanf("%d",&t);
	for(int cas=1;cas<=t;cas++){
		scanf("%d",&n);
		for(int i=0;i<n;i++) scanf("%lf%lf",&x[i],&w[i]);
		ans=find(x[0],x[n-1]);
	    ans1=(long long int)(ans+0.5);
		printf("Case #%d: %I64d\n",cas,ans1);
	}
}

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

时间: 2024-08-09 02:07:07

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

HDU 4337 King Arthur&#39;s Knights 找出一条哈密顿回路

n个点m条无向边 输出一条哈密顿回路 #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 155; int n, m; bool mp[N][N]; int S, T, top, Stack[N]; bool vis[N]; void _reverse(int l,int r) { while (l<r) swap(Stack[l++

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 4738 Caocao&#39;s Bridges ——(找桥,求联通块)

题意:给你一个无向图,给你一个炸弹去炸掉一条边,使得整个图不再联通,你需要派人去安置炸弹,且派去的人至少要比这条边上的人多.问至少要派去多少个,如果没法完成,就输出-1. 分析:如果这个图是已经是多个联通块了,那么一个人都不用去,如果不是,那么只要找出这个无向图上的桥并且哨兵数量最少的那座把它炸了就行(输出这条边上的哨兵数量即可).直接tarjan就可以写. 注意点:1.可能有重边,所以用手写邻接表的方式存图:2.如果一座桥上没有哨兵,那么你也得至少派去一个人去安置炸弹(因为炸弹不会自己飞过去啊

N个未排序的随机数,在线性时间内,求这N个数在数轴上相邻两个数的最大值

1 public class MaxSub 2 { 3 public static void main(String[] args) 4 { 5 int[] a ={5,7,3,1,6,2}; 6 System.out.println(maxSub(a)); 7 8 } 9 10 /** 11 * @用于找出N个随机数在数轴相邻位置的最大差值 12 */ 13 public static int maxSub(int[] a) 14 { 15 int min=a[0];//初始化数组的最小值mi

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 1517 A Multiplication Game (博弈&amp;&amp;找规律)

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3691    Accepted Submission(s): 2097 Problem Description Stan and Ollie play the game of multiplication by multiplying an in

给定数轴上的n个点,求距离最近的两个点的距离

public class MinimumSpacing { //给定平面上的n个点,求距离最近的两个点的距离. //无从下手的话,先分解问题,分解成简单的,逐个分析,然后再合在一起考虑 //这是个2维的数据,那就先降维到1维分析 //先考虑在一条数轴上有n个点,求最近距离的2个点的距离 // // ------*--*------*---*---> //用分治思想处理 // 1.分割 2.处理 3.合并 3个步骤 // // 1.分割: // 将整个数据[先排序]得到数组s,然后将s从中间一份为

HDU 4355 Party All the Time 三分算法

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