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<=), which is the number of cases followed. The first line of each case consists of one integer N(<=N<=), indicating the number of spirits. Then comes N lines in the order that x[i]<=x[i+] for all i(<=i<N). The i-th line contains two real number : Xi,Wi, representing the location and the weight of the i-th spirit. ( |xi|<=, <wi< )
 
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

0.6
3.9
5.1
8.4
 
Sample Output
Case #: 
 
Author
Enterpaise@UESTC_Goldfinger
 
Source
 

根据题意如果中心为X0,Y0,那么结果为sigma(|Xi-X0|^3*W)。

这是一个凸函数,因为它的二次导大于0,适用于三分搜索

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={,,-,};
int diry[]={-,,,};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 60000
#define inf 1<<26
struct Node{
double x,w;
}node[N];
int n;
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
double low=inf;
double high=-inf;
for(int i=;i<=n;i++){
scanf("%lf%lf",&node[i].x,&node[i].w);
if(node[i].x<low){
low=node[i].x;
}
if(node[i].x>high){
high=node[i].x;
}
} double sum1,sum2;
double mid1=(low+high)/;
double mid2;
while(fabs(high-low)>=eps){
mid1=(low+high)/;
mid2=(mid1+high)/;
sum1=;
sum2=;
for(int i=;i<=n;i++){
sum1+=fabs(node[i].x-mid1)*fabs(node[i].x-mid1)*fabs(node[i].x-mid1)*node[i].w;
sum2+=fabs(node[i].x-mid2)*fabs(node[i].x-mid2)*fabs(node[i].x-mid2)*node[i].w;
}
if(sum1+eps<sum2){
high=mid2;
}
else{
low=mid1;
}
}
printf("Case #%d: ",++ac);
printf("%.0lf\n",sum1); }
return ;
}

hdu 4355 Party All the Time(三分搜索)的更多相关文章

  1. codeforces 782B The Meeting Place Cannot Be Changed+hdu 4355+hdu 2438 (三分)

                                                                   B. The Meeting Place Cannot Be Change ...

  2. hdu 4778 Gems Fight! 博弈+状态dp+搜索

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...

  3. 三分搜索-ZOJ LightBulb

    开始算法基础学习的第一天 今天学习的内容是三分搜索 相对来说很基础的内容(还是觉得脑子不够用) 三分搜索主要用于凸函数查找极大值. (盗个图) 如图所示 若要查找该函数的最大值 可以考虑和二分法一样的 ...

  4. Weakness and Poorness CodeForces - 578C 三分搜索 (精度!)

    You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weak ...

  5. HDU 3131 One…Two…Five! (暴力搜索)

    题目链接:pid=3131">HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6= ...

  6. HDU 4355 Party All the Time (三分求极值)

    题意:给定x轴上有n个点,每一个点都有一个权值,让在x轴上选一个点,求出各点到这个点的距离的三次方乘以权值最小. 析:首先一开始我根本不会三分,也并没有看出来这是一个三分的题目的,学长说这是一个三分的 ...

  7. HDU 4355——Party All the Time——————【三分求最小和】

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

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

    题意:n个人,都要去參加活动,每一个人都有所在位置xi和Wi,每一个人没走S km,就会产生S^3*Wi的"不舒适度",求在何位置举办活动才干使全部人的"不舒适度&quo ...

  9. HDU 4355:Party All the Time(三分模板)

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

随机推荐

  1. 2016"百度之星" - 资格赛(Astar Round1) 1004

    思路:题目很简单,直接用map记录每个字符串的个数就可以了.记得对每个字符串先sort(). AC代码: #include <cstdio> #include <stdlib.h&g ...

  2. codevs 1001 舒适的路线 (并查集)

    题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光. Z小镇附近共有 N(<N≤)个景点(编号为1,,,…,N),这些景点被M(<M≤)条道路连 ...

  3. Ajax_post发送

    $('#img_file_del_3').click(function() { var data={name:$('#img_file_del_3').attr('name')}; var url=' ...

  4. C# 二叉堆

    二叉堆数据结构讲解: http://www.cnblogs.com/yc_sunniwell/archive/2010/06/28/1766751.html   C#代码实现 using System ...

  5. android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法

    最近项目开发中用到了android:launchMode="singleTask" 和 onNewIntent(Intent intent)两个特性,现总结一下经验: androi ...

  6. configure交叉编译

    今天在交叉编译时犯了一个错误,纠结了好久,曾经交叉编译器的前缀基本上都是用arm-linux-,这次换了一个新环境是arm-none-linux-gnueabi-,于是想当然的把configure中的 ...

  7. Android Studio 导入Eclipse工程

    eclipse:workspace对应多个project:而android studio是project对应多个module:故,在android studio中的工程project实际上是eclip ...

  8. CSS---------------之文本颜色

    CSS2支持如下名字的颜色 注意点: 你的浏览器有可能支持更多名字,但是在实际用的过程中尽量少使用名字的,因为各个浏览器对颜色的会存在差异:查看颜色可以参考 对于更多地颜色,你可以使用代表红,绿,蓝三 ...

  9. 利用PHP/MYSQL实现的简易微型博客(转)

    数据库:ly_php_base 表:ly_micro_blog(仅仅有一个表)字段:id,title,date,content,hits 文件: 文件 描述 default.php 默认主页.显示博文 ...

  10. class创建单击事件

    $(function () {            $(".search-button").click(function () {                $(" ...