HDU 4335 Party All the Time(三分|二分)

來源:互聯網
上載者:User

標籤:hdu   三分   



#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;}

題意:n個人,都要去參加活動,每個人都有所在位置xi和Wi,每個人沒走S km,就會產生S^3*Wi的“不舒適度”,求在何位置舉辦活動才能使所有人的“不舒適度”之和最小,並求最小值。

思路:首先可以得出最後距離之和的運算式最多隻有兩個極點,

更進一步只有一個極點,否則無最小值。

那麼我們就可用三分法或者二分法求解,即對原函數三分或對導數二分即可。


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

HDU 4335 Party All the Time(三分|二分)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.