参考链接:
https://blog.csdn.net/Littlewhite520/article/details/70144763
https://blog.csdn.net/u013557725/article/details/38377289
三分简述
注意区分二分和三分,如果求单调函数描述的问题的最优解,可以用二分;但是如果是个不单调函数,就需要用到三分。三分用于凸性函数(凹凸)的最优解问题,说白了就是找那个凸点所在的位置或者它所附加的信息。(二分和三分的模板性都比较强,都是一个while中不断缩小l和r的距离和一个判断某个点是否满足条件的函数组成)
下面这个图就是描述怎么缩小l和r之间的距离

查找方法:以先增后减型为例(找了很多博客,int类型和double类型的查找不太一样)

int sear(int l, int r)
{while (l<r-1){int mid1=(l+r)/2;int mid2=(mid1+r)/2;if (check(mid1)>check(mid2))r=mid2;elsel=mid1;}return check(l)>check(r)? l: r;
}
double sear(double l, double r)
{while (fabs(r-l)>=eps){//大于 还是大于等于都行double mid1=l+fabs(r-l)/3;double mid2=l+fabs(r-l)/3*2;if (check(mid1)>check(mid2))r=mid2;elsel=mid1;}return l;//因为误差很小了,所以返回哪个变量都一样
}

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 S 3*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 : X i,W i, representing the location and the weight of the i-th spirit. ( |x i|<=10 6, 0<w i<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

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;const double eps=1e-5;
double p[50100], w[50100];
int n;
double cal(double x)
{double ans=0;for (int i=1; i<=n; i++){double t=fabs(p[i]-x);ans+=t*t*t*w[i];}return ans;
}
double sear(double l, double r)
{double mid1, mid2;while (fabs(r-l)>eps){mid1=l+(fabs(r-l))/3;mid2=l+(fabs(r-l))/3*2;if (cal(mid1)>cal(mid2))l=mid1;elser=mid2;}return l;
}
int main()
{int T, cas=1;scanf("%d", &T);while (T--){scanf("%d", &n);double l=999999, r=-999999;for (int i=1; i<=n; i++){scanf("%lf%lf", &p[i], &w[i]);l=min(l, p[i]);r=max(r, p[i]);}double point=sear(l, r);double ans=cal(point);printf("Case #%d: %.0f\n", cas++, ans);}return 0;
}

HDU 4355 Party All the Time(三分)相关推荐

  1. Party All the Time HDU - 4355(三分)

    Party All the Time 题目链接:HDU - 4355 题意:在x轴上有n个点,每个点有一个权值w,定义f(x)=(fabs(x-a))^3 * w(a是x轴上一点); 求出一点a使得x ...

  2. hdu 4717 The Moving Points(三分+计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717 说明下为啥满足三分: 设y=f(x) (x>0)表示任意两个点的距离随时间x的增长,距离y ...

  3. hdu 5144 NPY and shot(三分)

    http://acm.hdu.edu.cn/showproblem.php?pid=5144 题意 :给出初速度 v 和高度 h 求最远斜抛距离 思路:根据物理公式推出 距离  s=v*cos(x)* ...

  4. hdu 4355 Party All the Time

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4355 题意: 小精灵不愿意走路,走路的话会使他们产生不高兴值,如果小精灵走的路是S体重是W那么他走S路 ...

  5. HDU - 3694 Fermat Point in Quadrangle(三分套三分/凸包)

    题目链接:点击查看 题目大意:给出四个点,求出该四边形的费马点,费马点是求得一个点,到这四个点的距离最小,最后输出距离, 题目分析:三分套三分,外层三分x,内层三分y即可,不过就是目前来说遇到的所有三 ...

  6. 【HDU - 3714 】Error Curves (三分)

    题干: Josephina is a clever girl and addicted to Machine Learning recently. She  pays much attention t ...

  7. 三分法解决凸(凹)函数极值问题

    二分法只适用与线性函数,当函数脱离线性而呈现凸性或者凹性的时候,三分是很有必要的. 三分过程如下图: 凸函数: 凹函数: 实现方法: double Calc(double p) {/*...*/ }d ...

  8. 第六周 8.23-8.29

    8.23 POJ 3311 Hie with the Pie TSP问题. 先跑一遍Floyd.再状压dp. dp[i][j]表示经过集合i的点最后到达j的最短距离. 转移:取集合i中任意一点j.如果 ...

  9. centos如何清理服务器日志文件,centos设置定时删除文件定时清理网站日志

    1.进入linux系统 2.在任意目录创建一个sh后缀的文件,如: 3.编辑打开该文件,如图: 4.此时按键盘上的"i"键或者"insert"键,进入编辑模式 ...

最新文章

  1. linux的文件压缩与解压缩,linux(文件压缩与解压缩)
  2. Conventions and patterns for multi-platform development
  3. ORACAL去除表中null转化为0
  4. 45号:公钥,私钥和数字签名
  5. 图解基本C语言程序一
  6. ssh不能连接 提示WARNING: POSSIBLE DNS SPOOFING DETECTED!处理方法
  7. OpenCV--实现图像滑动窗口截取子图操作
  8. strtus中action注入spring bean
  9. UESTC 914 方老师的分身I Dijkstra
  10. Google 又又又要“重回中国”了?
  11. DSP技术是利用计算机或,什么是dsp技术?dsp技术有哪些应用?
  12. Python 集合(set)类型的操作——并交差
  13. RobotStudio知识你知多少?
  14. 怎么判断两个多项式互素_多项式互素性质的补充讨论
  15. Write Combining Buffer
  16. 负反馈放大器电路详解
  17. 在Windows 7和Vista中禁用程序兼容性助手
  18. 产品经理应该懂哪些术语?
  19. FPGA数字信号处理(八)Quartus FFT IP核实现
  20. Word、PPT、Visio里面怎么插入latex mathcal的数学字符?

热门文章

  1. linux下 卸载与安装 jdk 的方法
  2. python关于二手房的课程论文_基于python爬取链家二手房信息代码示例
  3. 甘草干姜汤治疗肺虚寒
  4. linux结束后台命令,linux 终端的后台任务结束详解
  5. 移动echarts地图省份名称的位置
  6. 当你在Mac下载东西的时候,有时候因为网络或者其他问题中断了,双击重新下载会提示你“您所请求的资源没有权限”?
  7. office365离线安装
  8. 乐普生物通过港交所聆讯:在研4种核心产品,平安等为股东
  9. JAVA计算机毕业设计桌游店会员管理系统Mybatis+系统+数据库+调试部署
  10. IT“战疫”,企业级远程支持方案集锦