10125 - Sumsets
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1066
Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S.
Input
Several S, each consisting of a line containing an integer 1 <= n <= 1000 indicating the number of elements in S, followed 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
Output for Sample Input
12no solution
思路見代碼注釋。
複雜度:O(n^2 log n)
/*0.072s*/ #include<bits/stdc++.h> using namespace std; struct node { int a, b, sum; ///注意已經保證了a<b; bool operator < (const node& a) const { return sum < a.sum; } } sum[499505], tmpsum; int a[1005], tmp[4], n, c, ans, low, upp; inline bool notequal()///判斷這4個數是否互不相同 { int i,j; for (i = 0; i < 3; ++i) for (j = i + 1; j < 4; ++j) if (tmp[i] == tmp[j]) return false; return true; } bool solve() { int i,j,k; for (i = n - 1; i >= 0; --i)///從S中最大的枚舉d for (j = 0; j < n; ++j)///枚舉c { if (j == i) continue; tmp[0] = a[i], tmp[1] = a[j];///a[i]是d,a[j]是c tmpsum = (node){a[i], a[j], a[i] - a[j]};///d-c low = lower_bound(sum, sum + c, tmpsum) - sum, upp = upper_bound(sum, sum + c, tmpsum) - sum;///二分找d-c for (k = low; k < upp; ++k) { tmp[2] = sum[k].a, tmp[3] = sum[k].b;///a和b if (notequal()) { ans = a[i]; return true; } } } return false; } int main() { int i,j; while (scanf("%d", &n), n) { for (i = 0; i < n; ++i) scanf("%d", &a[i]); if (n < 4) { puts("no solution"); continue; } sort(a, a + n); c = 0; for (i = 0; i < n - 1; ++i) for (j = i + 1; j < n; ++j) sum[c++] = (node){a[i], a[j], a[i] + a[j]};///枚舉S中任意兩個元素之和,並排序 sort(sum, sum + c); if (solve()) printf("%d\n", ans); else puts("no solution"); } return 0; }
查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/