HDU 4355 Party All the Time(三分|二分)
来源:互联网 发布:淘宝的muji官方旗舰店 编辑:程序博客网 时间:2024/06/07 23:38
题意:n个人,都要去参加活动,每个人都有所在位置xi和Wi,每个人没走S km,就会产生S^3*Wi的“不舒适度”,求在何位置举办活动才能使所有人的“不舒适度”之和最小,并求最小值。
思路:首先可以得出最后距离之和的表达式最多只有两个极点,
更进一步只有一个极点,否则无最小值。
那么我们就可用三分法或者二分法求解,即对原函数三分或对导数二分即可。
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string>#include<map> #include<set>#define eps 1e-6 #define LL long long #define pii pair<int,int>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;}
0 0
- HDU 4355 Party All the Time(三分|二分)
- HDU 4355 Party All the Time 三分
- hdu 4355 Party All the Time (三分)
- HDU 4355 Party All the Time 三分
- hdu 4355 Party All the Time 三分
- HDU 4355 Party All the Time(三分)
- HDU 4355 Party All the Time(三分)
- HDU 4355 Party All the Time(三分)
- HDU 4355 - Party All the Time(三分)
- HDU 4355 Party All the Time (三分)
- HDU 4355 Party All the Time(三分)
- HDU 4355 Party All the Time(三分精度问题)
- Party All the Time+hdu+三分搜索
- hdu 4355 Party All the Time 典型三分
- Party All the Time(三分)
- POJ 4355 Party All the Time(三分)
- HDU 4355 Party All the Time (三分算法的应用(含三分算法的讲解))
- [HDU4355][三分]Party All The Time
- error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches "***
- 初学C语言
- mongodb初识
- SurfaceView的一些简单记录
- 省市联动JQ封装比较简洁调用的方法
- HDU 4355 Party All the Time(三分|二分)
- 算法设计与分析:第二章 递归 2.3使x^2为一个各位数字互不相同的九位数
- 11g数据库删除数据库节点增加数据库节点
- hdu 2546饭卡
- Linux x64系统上安装 oracle 11g R2 x64
- nyoj 20 吝啬的国度
- 一、IOS程序执行顺序、APPdelegate代理方法执行顺序、ViewController 生命周期
- NPM 镜像设置
- 算法设计与分析:第二章 递归 2.4报数退圈游戏