Codeforces Round #202 C - Mafia__二分搜尋

來源:互聯網
上載者:User

這個題二分做,對於確定x更新次,我們可以確定是否能夠更新,如果滿足任意a[i]<x && ∑(x-a[i]) >=x就滿足情況。對x來說x越大,越快讓每個人更新好,這樣就有個單調性,就可以用二分來做。 這題也沒想到二分做,做題要考慮多方面的解題思路把。說穿了還是題刷的不夠多,理解的不夠深刻。

/*If I get TLE , it is good.If I get AC,it's NICE !*/#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>using namespace std;typedef long long ll;const int MAXN=1e6+100;ll a[MAXN];ll n;ll judge(ll x){    ll sum=0;    for(ll i=1;i<=n;i++)    {        if(a[i]<=x) sum+=x-a[i];        else return 0;    }    if(sum<x) return 0;    return 1;}int main(void){    cin >> n;    for(int i=1;i<=n;i++)        scanf("%d",&a[i]);    ll left=1,right=1e14,mid;    ll ans;    while(left <= right)    {        mid=left+(right-left)/2;        if(judge(mid)) ans=mid,right=mid-1; // 對於可行的mid , 我們往左邊區間去找,找盡量小的可行點並儲存下來        else    left=mid+1;        //printf("%I64d-%I64d-%I64d\n",left,right,mid);    }    //printf("%I64d\n",judge(3));    printf("%I64d\n",ans);}

聯繫我們

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