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