uva 10125 - Sumsets

本文介绍了解决UVA10125-Sumsets问题的一种有效算法。该算法通过预处理所有可能的两数之和,并使用二分查找来寻找满足a+b+c=d条件的四数组合,最终找到最大的d值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:给定一个集合,然后选择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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值