博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4355 Party All the Time(三分|二分)
阅读量:7244 次
发布时间:2019-06-29

本文共 1240 字,大约阅读时间需要 4 分钟。

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

思路:首先能够得出最后距离之和的表达式最多仅仅有两个极点,

更进一步仅仅有一个极点,否则无最小值。

那么我们就可用三分法或者二分法求解。即对原函数三分或对导数二分就可以。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define eps 1e-6 #define LL long long #define pii pair
using namespace std; const int maxn = 100000+100;//const int INF = 0x3f3f3f3f;double w[maxn], x[maxn];int n;double fx(double x0) { double ans = 0; for(int i = 0; i < n; i++) { ans += pow(fabs(x[i]-x0), 3)*w[i]; } return ans;}double find(double L, double R) { for(int i = 0; i < 30; i++) { double midl = L+(R-L)/3, midr = L+(R-L)*2/3; if(fx(midl) <= fx(midr)) R = midr; else L = midl; } return R;}int main() {// freopen("input.txt", "r", stdin); int T; cin >> T; int kase = 0; while(T--) { cin >> n; double minp = 1000000, maxp = -1000000; for(int i = 0; i < n; i++) { scanf("%lf%lf", &x[i], &w[i]); minp = min(minp, x[i]); maxp = max(maxp, x[i]); }// cout << fx(0) << endl; printf("Case #%d: %d\n", ++kase, (int)(fx(find(minp, maxp))+0.5)); } return 0;}



转载地址:http://xqybm.baihongyu.com/

你可能感兴趣的文章
JS编写日历控件(支持单日历 双日历 甚至多日历等)
查看>>
400操作 示例
查看>>
交换机、集线器、路由器区别(转)
查看>>
UITextField,UITextView字数限制
查看>>
Spring 循环依赖
查看>>
sencha touch 在线实战培训 第一期 第二节
查看>>
Mirror--使用证书配置镜像模板
查看>>
ArcGIS 10 安装程序及破解文件
查看>>
C#中读写JSON风格的配置信息
查看>>
Spring-Context之一:一个简单的例子
查看>>
(转)S5PV210 三个Camera Interface/CAMIF/FIMC的区别
查看>>
struct和typedef struct
查看>>
9.5 在 C# 中使用 F# 库
查看>>
2016第6周六
查看>>
Windows 下 绿化 Oracle
查看>>
利用京东云擎架设免费Wordpress 博客(git方式)
查看>>
Linux开发环境搭建与使用——ubuntu更新设置
查看>>
POJ 3740 Dancing Links
查看>>
iOS开发--使用NSMutableAttributedString 实现富文本
查看>>
十一、jdk命令之Jstatd命令(Java Statistics Monitoring Daemon)
查看>>