HDU4355-Party All the Time-三分法(模板)-2012 Multi-University Training Contest 6
2015-09-09 11:16
459 查看
初看三分法,看一下这个博客挺不错的,我就不详解了。链接:http://www.cnblogs.com/newpanderking/archive/2011/08/25/2153777.html
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4355
必备翻译:给你n个个位置x[i]和灵魂重量w[i],叫你选取一个点,是得s^3*W最小,因为x已经给我们排好序列了,很显然,这是个凸性函数,直接套用模板就可以AC。
AC代码:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4355
必备翻译:给你n个个位置x[i]和灵魂重量w[i],叫你选取一个点,是得s^3*W最小,因为x已经给我们排好序列了,很显然,这是个凸性函数,直接套用模板就可以AC。
AC代码:
#include<iostream> #include<string> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define EPS 1e-7 using namespace std; const int N=50055; int t,n,Case=1; double x ,w ; double cal(double pos) // 计算公式; { double ans=0; for(int i=0;i<n;i++){ double tmp=fabs(pos-x[i]); ans+=tmp*tmp*tmp*w[i]; } return ans; } double Solve() { double l=x[0],r=x[n-1]; double lmid,rmid; double lmid_v,rmid_v; while(l+EPS<r){ lmid=(l+r)/2.0; rmid=(lmid+r)/2.0; lmid_v=cal(lmid); rmid_v=cal(rmid); if(lmid_v<=rmid_v) r=rmid; else l=lmid; } return cal(l); } int main() { scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=0;i<n;i++) scanf("%lf %lf",&x[i],&w[i]); printf("Case #%d: %.0lf\n",Case++,Solve()); } return 0; }
相关文章推荐
- shutdownServer failed: shutdown must run from localhost when running db without auth
- DS | Tree | PAT | Tree Traversals Again
- hashCode和equals重写
- Error: Activity class {package1/package2.mainActivity} does not exist.
- LeetCode_contains-duplicate
- rails db:create错误
- New package not yet registered with the system. Waiting 3 seconds before next attempt
- POJ 1995 Raising Modulo Numbers(快速幂)
- raid write-bakc write-through的解释
- raid组的初始化详解
- Contains Duplicate
- 游戏人工智能开发之6种决策方法
- 游戏人工智能开发之6种决策方法
- oschinaIM/聊天/语音工具
- oschinaIM/聊天/语音工具
- awaitTermination() shutdown()
- mount: failed, reason given by server: Permission denied
- hdoj 1022 Train Problem I
- 1090. Highest Price in Supply Chain (25)
- Explain分析查询语句