UVA 10125 Sumsets

Sumsets

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on  PKU. Original ID: 2549
64-bit integer IO format: %lld      Java class name: Main
 
iven 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

5

2 

3 

5 

7 

12

5

2 

16 

64 

256 

1024

0

Sample Output

12

no solution

Source

 
解题:二分好啦
 
UVA 10125 Sumsets
 1 #include <bits/stdc++.h>

 2 using namespace std;

 3 typedef long long LL;

 4 struct Node {

 5     LL val;

 6     int x,y;

 7     Node(LL v,int a,int b):val(v),x(a),y(b) {}

 8     bool operator<(const Node &t) const {

 9         return val < t.val;

10     }

11     bool operator>(const Node &t) const {

12         return val > t.val;

13     }

14     bool operator!=(const Node &t) const {

15         return x != t.x && y != t.y && x != t.y && y != t.x;

16     }

17 };

18 vector<Node>a,b;

19 LL d[1010];

20 int main() {

21     ios::sync_with_stdio(false);

22     int n;

23     while(cin>>n && n) {

24         for(int i = 0; i < n; ++i) cin>>d[i];

25         a.clear();

26         b.clear();

27         for(int i = 0; i < n; ++i)

28             for(int j = i + 1; j < n; ++j) {

29                 a.push_back(Node(d[i] + d[j],i,j));

30                 b.push_back(Node(d[i] - d[j],i,j));

31                 b.push_back(Node(d[j] - d[i],j,i));

32             }

33         sort(a.begin(),a.end());

34         sort(b.begin(),b.end());

35         LL ret;

36         memset(&ret,0x80,sizeof(LL));

37         for(auto it = a.begin(); it != a.end(); ++it){

38             auto lvalue = lower_bound(b.begin(),b.end(),*it);

39             auto rvalue = upper_bound(lvalue,b.end(),*it);

40             for(;lvalue != rvalue; ++lvalue)

41                 if(*lvalue != *it)

42                     ret = max(ret,it->val + d[lvalue->y]);

43         }

44         if (ret < -536870912)

45             cout<<"no solution"<<endl;

46         else cout<<ret<<endl;

47     }

48     return 0;

49 }

50 /*

51 5

52 2

53 3

54 5

55 7

56 12

57 */
View Code

 

你可能感兴趣的:(set)