2015/04/13

【UVa】10125-Sumsets (qsort+binary search)



題目連結

題目大意:
給一set S, 有多個instinct integers, 求最大的d使得a+b+c = d

解題思路
將算式變作a+b = c-d。開一陣列x記錄所有a+b,用qsort排列x的index。再算出每一種可能的c-d,用binary search搜尋x,若value相同且c, d不等於a, b,則紀錄d,最後找出最大d,無解則輸出no solution。

Code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <cstdio>
#include <stdlib.h>
#include <algorithm>
 
using namespace std;
int elements[1000];
 
struct a_plus_b{
    int a;
    int b;
    int sum;
};
 
struct a_plus_b x[1000000];
 
int search_comp(int d, int c, int x_index, int x_index_tb[]) {
    int i,lower_bound=0, upper_bound=x_index-1, mid;
    struct a_plus_b value;
    int y = d-c;
    while(lower_bound <= upper_bound) {
        mid = (lower_bound+upper_bound)/2;
        value = x[x_index_tb[mid]];
        if(value.sum > y) upper_bound = mid-1;
        else if(value.sum < y) lower_bound = mid+1;
        else {
            for(i = mid; i <= upper_bound; i++) {
                if(value.sum != y)
                    break;
                if(d!=value.a && d!=value.b && c!=value.a && c!=value.b)
                    return 1;
            }
             
            for(i = mid-1; i >= lower_bound; i--) {
                if(value.sum != y)
                    break;
                if(d!=value.a && d!=value.b && c!=value.a && c!=value.b)
                    return 1;
            }
            return 0;
        }
    }
    return 0;
}
 
 
int comp_func(const void *a, const void *b){
    int c = x[*(int*)a].sum;
    int d = x[*(int*)b].sum;
    if (c>d) return 1;
    else if (c< d) return -1;
    else return 0;
}
 
int main()
{
    int i, j, element_num;
    while(scanf("%d",&element_num)==1 && element_num){
        int x_index = 0;
        int x_index_tb[element_num*(element_num-1)/2];
        for (i=0; i< element_num; i++)
            scanf("%d", &elements[i]);
        for (i=0; i< element_num; i++){
            for (j=i+1; j< element_num; j++){
                x[x_index].sum = elements[i]+elements[j];
                x[x_index].a = elements[i];
                x[x_index].b = elements[j];
                x_index_tb[x_index] = x_index;
                x_index++;
            }
        }
        qsort(x_index_tb, x_index, sizeof(int), comp_func);
        int y1bs, max=0, flag=0;
        for (i=element_num-1; i>=0; i--){
            for (j=0; j< element_num; j++){
                if (i==j) continue;
                y1bs = search_comp(elements[i], elements[j], x_index, x_index_tb);
                if(y1bs == 1) {
                    if (flag==0){
                        max = elements[i];
                        flag = 1;
                    }
                    else if (elements[i]>max) max = elements[i];
                }
            }
        }
        if (flag==1)
            printf("%d\n",max);
        else printf("no solution\n");
    }
    return 0;
}