【轉】ZOJ Problem Set - 1101 Gamblers:非暴力枚舉

來源:互聯網
上載者:User

標籤:

思路:

將所有資料從小到大排序,這樣每次從序列的後面尋找答案ans,自然的確保了最大賭資性.

接下來就要枚舉其他三個數了 進而 判斷 此時的ans是否存在 其他三個數 和 等於ans.

而在三個數中至少有一個數在ans的前面 其他的兩個數可能在ans的後面 因為存在負數.

這樣就可以先枚舉兩個 賭注 進而第三個賭注可以由ans與 兩個賭注 差 確定 從而二分搜尋

如果存在第三個賭注 那麼ans的賭注就是答案

如果枚舉所有ans都找不到對應的第三個賭注則no solution  

#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;const int Size=1000;int a[Size+1];int n;int bsearch(int start, int val){        int L=start+1;        int R=n-1;        while(L<=R)        {                int mid=(L+R)/2;                if( a[mid] == val )                    return mid;                else if(a[mid]<val)                    L=mid+1;                else                    R=mid-1;        }        return 0;}int Enumer(){        for(int i=n-1; i>0; i--)            for(int j=0; j<i; j++)                for(int k=j+1; k<n; k++){                        int tmp=a[i]-a[j]-a[k];                        int pos=bsearch(k, tmp);                        if(pos&&pos!=i)                            return i;                }        return 0;}int main(){        while(cin>>n&&n)        {                for(int i=0; i<n; i++)                    cin>>a[i];                sort(a, a+n);                if(Enumer())                    cout<<a[Enumer()]<<endl;                else                    cout<<"no solution"<<endl;        }        return 0;}

  

【轉】ZOJ Problem Set - 1101 Gamblers:非暴力枚舉

聯繫我們

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