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", &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)