Sumsets (3sum problem, enumeration d,c a+b)

Source: Internet
Author: User

Sumsets
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9997 Accepted: 2736

Description

Given S, a set of integers, find the largest d such that A + B + c = d where a, B, C, and D is distinct elements of S.

Input

Several S, each consisting of a line containing an integer 1 <= n <=-indicating the number of elements in S, fo Llowed by the elements of S, one per line. Each element of S is a distinct integer between-536870912 and +536870911 inclusive. The last line of input contains 0.

Output

For each S, a single line containing d, or a single line containing "no solution".

Sample Input

52 3 5 7 1252 16 64 256 10240

Sample Output

12NO Solution
Puzzle: Give a sequence, let find different a,b,c,d in set S, make A+b+c=d, if can find output D, otherwise output no solution;
At first glance completely no idea, perhaps dare not to write, can choose from big to small sort, enumerate d,c; a+b equals d-c;
extern "C + +" {#include <iostream> #include <algorithm> #include <cstdio> #include <cstring># Include<cmath> #include <queue>using namespace std;typedef long long ll;void SI (int &x) {scanf ("%d", &AMP;X);} void SI (double &x) {scanf ("%lf", &x);} void SI (char *x) {scanf ("%s", x);} void SI (LL &x) {scanf ("%lld", &x);} void PI (int &x) {printf ("%d", x);} void PI (double &x) {printf ("%lf", x);} void PI (char *x) {printf ("%s", x);} void PI (LL &x) {printf ("%lld", x);}} const int MAXN = 1010;int A[maxn];int main () {int n;while (scanf ("%d", &n), N) {for (int i = 0;i < n;i++) SI (A[i]); sort (A,  A + n); int ans,flot = 0;for (int i = N-1;i >= 0;i--) {if (flot) break;for (int j = n-1;j >= 0;j--) {if (flot) break;if (i = = j) Continue;int sum = a[i]-a[j],l = 0,r = J-1;while (L < R) {if (A[l] + a[r] = = Sum && i! = L && I ! = r) {ans = A[i];flot = 1;break;} if (A[l] + a[r] > Sum) r--;elsel++;}}} if (flot) printf ("%d\n", ans); Elseputs ("no Solution");} return 0;}

  

Sumsets (3sum problem, enumeration d,c a+b)

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.