uva 10125 - Sumsets

来源:互联网 发布:淘宝网店贷款 编辑:程序博客网 时间:2024/06/08 01:08

题目大意:给定一个集合,然后选择4个不同的数,使得 a + b + c = d ,问最大的d是多少。


此题集合中数据的最大个数为一千。

首先想到的是 暴力法。但是不能使用4重循环,复杂度 为n4次方。

我们变换一下 等式 得到   a + b = d - c =t ;   对于一个t我们如何找到是否存在a+b使得a+b = t呢?显然要用到二分查找,主要log(n * n)就可以了 而其最大为6次

到这里,算法就出来了,枚举 d 和c ,预处理a+ b(也就是先求出所有a+b的和使用快速排序,sum数组)

         对与 d 和c  ,t = d - c ;在sum中使用二分查找看sum中是否含有t,如果没有则进行下一个d和c , 若存在 且t值有num个

                                            若果 num > 2 ;则肯定满足题意

                                            若d +c = d-c ,num > 1;也满足题意

                                            若 d+ t1 = d-c,c +t2 = d - c , num > 3;满足题意。   

                                             若d + t1 = d- c 和c +t2 = d - c只有一个是成立的,num >1;满足题意

                                            若两个等式都不满足,则满足题意。

////  main.cpp//  uva 10125 - Sumsets////  Created by XD on 15/8/6.//  Copyright (c) 2015年 XD. All rights reserved.//#include <iostream>#include <string>#include <queue>#include <stack>#include <stdio.h>#include <stdlib.h>#include <math.h>#include<vector>#include <string.h>#include <string>#include <algorithm>#include <set>#include <map>#include <cstdio>using namespace std ;long long  s[1010] ;int n ;const int hashsize = 100003 ;int head[hashsize] ;int mnext[hashsize] ;long long sum[600000] ;void  solved(){    int t = 0;    for (int i = 0; i < n; i++) {        for (int j = i+1; j < n ; j++) {            sum[t++] = s[i] + s[j] ;        }    }    sort(sum, sum  +t) ;    sort(s, s + n ) ;    for (int d = n-1; d >=0; d--) {        for (int c = n - 1; c >=0; c--) {            if (d ==c) {                continue ;            }            else{                long long  minus = s[d] - s[c] ;                int   start =  (int)(lower_bound(sum, sum + t, minus) -sum) ;                if (start >= t || start <= 0  ) {                    continue ;                }                long long  t1 = sum[start] ;                if (t1 != minus) {                    continue ;                }                else{                    int num = 1;                    for (long long  j = start +1; j  < t; j++) {                        if (sum[j] == minus) {                            num++  ;                            if (num >2) {                                 printf("%lld\n" , s[d]) ;return;                            }                        }                    }                    if (s[c]+s[d] == minus) {                        if(num > 1) { printf("%lld\n" , s[d]) ;return;}                        else{                            continue ;                        }                    }                    else{                        int s1 =   (int)(lower_bound(s, s + n, minus-s[c]) -s) ;                        int s2 =   (int)(lower_bound(s, s + n, minus-s[d]) -s) ;                        if (s1<n&&s1>=0 && s[s1] == minus - s[c]) {                            num-- ;                        }                        if (s2<n &&s2>=0&& s[s2] == minus - s[d]) {                            num-- ;                        }                        if (num>0 ) {                            printf("%lld\n" , s[d]) ;                            return  ;                        }                    }                }                            }        }    }    printf("no solution\n") ;    }int main() {    while (scanf("%d" ,&n)==1 && n!= 0  ) {        for (int i = 0; i  < n ; i++) {            scanf("%lld" ,&s[i]) ;        }        if (n <4 ) {            printf("no solution\n") ;        }        else{            solved() ;        }    }        return 0;}


0 0
原创粉丝点击