Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10593 | Accepted: 2890 |
Description
Input
Output
Sample Input
5 2 3 5 7 12 5 2 16 64 256 1024 0
Sample Output
12 no solution
Source
#include <cstdio> #include <algorithm> using namespace std; const int MAXN = 1000+5; int n; int a[MAXN]; struct S{ int val; int i, j; bool operator < (const S& b)const { return val < b.val; } }; S l[MAXN*MAXN], r[MAXN*MAXN]; bool ok(S& a, S& b) { return a.i != b.i && a.j != b.j && a.i != b.j && a.j != b.i; } void solve() { int lcnt = 0; for(int i = 0; i < n; ++i){ for(int j = i+1; j < n; ++j){ l[lcnt].val = a[i]+a[j]; l[lcnt].i = i; l[lcnt++].j = j; } } int rcnt = 0; for(int i = 0; i < n; ++i){ for(int j = i+1; j < n; ++j){ r[rcnt].val = a[i]-a[j]; r[rcnt].i = i; r[rcnt++].j = j; r[rcnt].val = a[j]-a[i]; r[rcnt].i = j; r[rcnt++].j = i; } } sort(l, l+lcnt); int res = 0xffffffff; for(int i = 0; i < rcnt; ++i){ int d = lower_bound(l, l+lcnt, r[i])-l; if(ok(l[d], r[i]) && l[d].val == r[i].val){ res = max(res, r[i].val+a[r[i].j]); } } if(res == 0xffffffff) printf("no solution\n"); else printf("%d\n", res); } int main() { while(scanf("%d", &n), n){ for(int i = 0; i < n; ++i) scanf("%d", &a[i]); solve(); } return 0; }
原文:http://www.cnblogs.com/inmoonlight/p/5788954.html