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) ...
随机推荐
- 《UNIX环境高级编程》笔记--chown,fchown和lchown函数
这三个函数都是用来更改文件的用户ID和组ID的,函数的定义如下: #include <unistd.h> int chown(const char *pathname, uid_t own ...
- Linux下redis的安装及用法
1.下载源代码包redis-2.8.21.tar.gz,并将其上传到指定文件夹/urs/src,然后对其进行解压: [root@Slave1pc src]# tar -xvf redis-2.8.21 ...
- Unity目录结构
http://www.cnblogs.com/liudq/p/5540051.htmlUnity中有几个默认目录 Unity5.x Resources 项目中默认的资源路径,会直接打包到游戏包中.即使 ...
- Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6629298 在前面一篇文章浅谈Android系 ...
- 【HDU】 1018 Big Number
大意就是求 : log10(n!) = log10(1 * 2 * 3 * .......*n) = log10(1) + log10(2) + ........+log10(n); 打表的话会ML ...
- linux下git使用记录1 git 提交
linux下git使用记录1 浏览:985 发布日期:2013/08/08 分类:技术分享 在使用github的时候,不可避免的接触到了git,用他来更新项目,做版本控制.这里特别把常用的命令记录 ...
- [HeadFirst-HTMLCSS学习笔记][第十三章表格]
表格 -table 块 tr 行 table row th 表头 table head td 表数据 table data; caption 表格标题 <table> <captio ...
- css解决文字垂直居中
参考链接: http://www.cnblogs.com/lufy/archive/2012/09/12/2681972.html http://zhidao.baidu.com/question ...
- SAR ADC : 逐次逼近寄存器型(SAR)模数转换器(ADC)
1.为实现二进制搜索算法,N位寄存器首先设置在中间刻度(即:100... .00,MSB设置为1).这样,DAC输出(VDAC)被设为VREF/2,VREF是提供给ADC的基准电压.然后,比较判断VI ...
- UVA11361 Investigating Div-Sum Property(数位dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 题目意思:问在区间[A,B]有多少个数不仅满足自身是k的倍数,而且其各个位数上的和 ...