"Go" ZOJ problem Set-1101 Gamblers: non-violent enumeration

Source: Internet
Author: User

Ideas:

Sort all the data from small to large, so that each time you find the answer from the back of the sequence, ans, nature ensures maximum gambling.

The next step is to enumerate the other three numbers and then determine if the ANS at this point has the other three numbers and equals ans.

While at least one of the three numbers in the front of the ANS the other two numbers may be behind ans because there are negative numbers.

This allows you to enumerate two bets and then the third bet can be determined by the ANS with two bet spreads and thus the two-point search

If there's a third bet, then ans's bet is the answer.

If enumerating all ans cannot find the corresponding third bet then 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;}

  

"Go" ZOJ problem Set-1101 Gamblers: non-violent enumeration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.