POJ 1064 Cable master (二分答案,G++不過,C++就過了)

來源:互聯網
上載者:User

標籤:.com   二分答案   求和   map   else   ges   精度   class   bsp   

題目:

 

 

 

這題有點坑,G++過不了,C++能過。

 

 

條件:n個資料a[],分成k段,結果精度要求兩位小數。

問題:每段最長為多少?

 

思路:因為精度要求為兩位小數,我先把所有的長度a[]*100。

   我們對答案二分搜尋,把l設定為0,r設定為1000*10000*100+1(資料量*每個資料最大的大小*精度+1)。

   這樣我們搜尋的數就不用處理精度了,我們可以二分算出結果然後除以100。

 

代碼:

#include <iostream>#include <algorithm>#include <map>#include <vector>#include <set>#include <math.h>#include <queue> #include <assert.h>#include <stdio.h>#include <stdlib.h>using namespace std;typedef long long ll;#define INF 2147483647//輸入int n, k;    double a[10010];    //返回分成k段最大的段長double solve(ll sum) {    ll l = 0, r = sum+1;    //二分的左右端    ll mx = 0;        //儲存結果        while (l < r) {        ll mid = (l + r) / 2;        ll sum = 0;        //段數求和        for (int i = 0; i < n; i++) sum += a[i] / mid;                    //每段長mid,可以分成k段        if (sum >= k) {            mx = max(mx, mid);    //更新答案            l = mid + 1;            }        else {            r = mid;        }    }    return 1.0*mx/100;}int main() {    cin >> n >> k;    ll sum = 0;    for (int i = 0; i < n; i++) {        cin >> a[i];        a[i] *= 100;    //將段長乘以100        sum += a[i];    //對段長求和    }    printf("%.2lf",solve(sum));    getchar(); getchar();    return 0;}

 

POJ 1064 Cable master (二分答案,G++不過,C++就過了)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.