hdu 4355 Party All the Time(三分搜索)
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.
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< )
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.
0.6
3.9
5.1
8.4
Case #:
根据题意如果中心为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(三分搜索)的更多相关文章
- codeforces 782B The Meeting Place Cannot Be Changed+hdu 4355+hdu 2438 (三分)
B. The Meeting Place Cannot Be Change ...
- hdu 4778 Gems Fight! 博弈+状态dp+搜索
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...
- 三分搜索-ZOJ LightBulb
开始算法基础学习的第一天 今天学习的内容是三分搜索 相对来说很基础的内容(还是觉得脑子不够用) 三分搜索主要用于凸函数查找极大值. (盗个图) 如图所示 若要查找该函数的最大值 可以考虑和二分法一样的 ...
- 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 ...
- HDU 3131 One…Two…Five! (暴力搜索)
题目链接:pid=3131">HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6= ...
- HDU 4355 Party All the Time (三分求极值)
题意:给定x轴上有n个点,每一个点都有一个权值,让在x轴上选一个点,求出各点到这个点的距离的三次方乘以权值最小. 析:首先一开始我根本不会三分,也并没有看出来这是一个三分的题目的,学长说这是一个三分的 ...
- HDU 4355——Party All the Time——————【三分求最小和】
Party All the Time Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 4355 Party All the Time(三分|二分)
题意:n个人,都要去參加活动,每一个人都有所在位置xi和Wi,每一个人没走S km,就会产生S^3*Wi的"不舒适度",求在何位置举办活动才干使全部人的"不舒适度&quo ...
- HDU 4355:Party All the Time(三分模板)
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
随机推荐
- Oracle判断语句集合(转载)
SELECT decode(sign(to_date('2008-05-01', 'yyyy-MM-dd') - to_date('2008-03-01', 'yy ...
- Linux CPU 亲和性
在Linux中,我们知道可以通过nice.renice命令改变进程的执行优先级,优先级高的进程优先执行,从而一定程度上保证重要任务的运行. 除了nice.renice外,可以通过CPU affinit ...
- 【转】Alsa音频编程【精华】
一.前序 这里了解一下各个参数的含义以及一些基本概念. 声音是连续模拟量,计算机将它离散化之后用数字表示,就有了以下几个名词术语. 样本长度(sample):样本是记录音频数据最基本的单位,计算机对每 ...
- bootstrap 导航布局
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 归纳下js面向对象的几种常见写法
//定义Circle类,拥有成员变量r,常量PI和计算面积的成员函数area() 1.工厂方式 var Circle = function() { var obj = new Object(); ob ...
- dev grdicontrol 根据条件改变行颜色,字体颜色等
dev grdicontrol 根据条件改变行背景 dev grdicontrol 根据条件改变字体颜色 dev grdicontrol 根据条件改变某个单元格背景 不用写代码 打开gridc ...
- 【COCOS2DX-对28游戏开发】 Cocos2d-x-3c 道路设计 CocosBase CocosNet CocosWidget
原文链接:http://blog.csdn.net/cocosviva/article/details/18970717 另一个比較不错的cocos2dx扩展库:https://github.com/ ...
- Velocity知识点总结
Velocity知识点总结 1. 变量 (1)变量的定义: #set($name = "hello") 说明:velocity中变量是弱类型的. 当使用#set 指令时,括在双引號 ...
- Hibernate PO对象的状态
Hibernate的PO对象有三种状态:临时状态(又称临时态).持久状态(又称为持久态)和脱管状态(又称为脱管态.游离态).处理持久态的对象也称为PO,临时对象和脱管对象也称为VO. 1.临时态: 简 ...
- VS删除未使用的命名空间
把鼠标光变放在Using+命名空间区域,右键——组织 using->移除未使用的 using