uva 10125 - Sumsets

UVa 10125 & POJ 2549 - Sumsets 传送门UVa 10125 & POJ 2549 - Sumsets 阅读详情

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


UVa 10125 Sumsets (中途相遇法) UVa 10125 Sumsets题目大意:给一个整数集合S,找出最大的d,使得a+b+c=d,其中a,b,c,d是S中的不同元素. (元素个数1<=n<=1000,元素大小-536870912<=x<=+536870911).题目分析:最直接的手段是直接枚举a,b,c,d,当然时间复杂度很高O(n^4). 但是若将四个数拆分成两两枚举,即先枚举a+b,储存起来,再枚举d-c,查找是否存在a+b 阅读详情

相关推荐

uva10125 Sumsets

https://vjudge.net/problem/UVA-10125 $a+b+c=d\rightarrow a+b=d-c$ 双向枚举即可 #include<bits/stdc++.h> using namespace std; const int N=1005; int n,num[N]; map<int,int> hash1,hash...

weixin_30535043的博客 139

UVA 10125 Sumsets

题意:给出一个集合S,求一个最大的d,满足a+b+c=d,a,b,c,d都是S中的不同的元素。 思路:把a+b先算出来,然后枚举d-c,判断a+b是否存在……我用的map处理的a+b,开始感觉复杂度不太优美,后来实在想不出什么好方法了,只好这么写,就这么过了。。。 代码: #include #include #include #include #include #include #i

绝望的乐园 810

Sumsets UVA - 10125

问题 https://vjudge.net/problem/UVA-10125 分析 使用long long,中途相遇法,要注意,不能使用相同的数字,所以要记录 #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <map> ...

zpf1998的博客 151

UVA - 10125 Sumsets

题目大意:给出一个集合

静静地码 828

uva 10125 Sumsets

简单的哈希表应用,将问题转换为a+b=d-c即可 #include #include #include #include using namespace std; int arr[1005]; struct node { int a, b; }; map > hash_map; bool is_suitable(int d, int c) { bool f; int v

皓首不倦 不负少年 744

UVa 10125 - Sumsets

题目链接: UVa : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1066 poj :   http://poj.org/problem?id=2549 类型: 哈希, 二分查找 原题: G

D_Double's Journey 2682

UVA10125 Sumsets

哈希链表

ACoder 302

UVa 10125 - Sumsets

方法一:可以暴力枚举,其实用枚举方法水过纯属意外。(可以试一下1000个1这组数据,它是跑不出来的。) 方法二:可以将问题转化为 a+b=d-c。这样,只要将a+b的值全部算出来,存至哈希表内,再枚举d,c的值看两边是否相等从而推出d,就可以将复杂度由O(n4)降之O(n2)左右的水准,便可以过了。 代码如下: 方法一(枚举 1.600s): #define test #include

GooMaple 2392

UVa 10125 - Sumsets (中途相遇法 hash)

Sumsets Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status]   Description     Problem D - Find D Users are

zhaosdfa 742

uva10125 - Sumsets

最简单的暴力 会超时 所以我们要想方设法的减少循环层数或者循环次数, a+b+c = d 那么a+b=d-c 这不是简单的等式变形 而是意味着我们循环的次数减少了。 我们对于d和c分别用一层循环, 对于a+b只用一层循环。 很妙的转变,,, 代码如下: #include #include using namespace std; int a[1010]; i

稻草人 1059

UVA 10125 Sumsets

UVA_10125 核心的思想就是先把a+b的值建一个哈希表,然后枚举d-c的值,如果找到和d-c相同的a+b并且4个元素各不相同的话,就更新一下最后结果。 当然,哈希函数的设计可以随意的,如果超时了不妨换一个。 #include<stdio.h>#include<string.h>#include<math.h>#define HAS...

weixin_30909575的博客 68

UVA10125 POJ2549 Sumsets【暴力+二分】

Given 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 el

海岛Blog 546

uva10125 Sumsets

看似简单的题,其实要求考虑的非常严密

cgjiayou 446

UVA 10125 - Sumsets

这个题有两种方法,一种是直接暴力搜索,但是也是有技巧的,先给数组排序,然后从最大的数开始搜索,如果能找到3个数和是这个数,直接结束循环,输出这个数。代码如下:(时间是1.618) #include #include using namespace std; int main() { //freopen("in.txt","r",stdin); int n,num[1010],fl

anqier0468的专栏 548
上一篇: uva 11882 Biggest Number
下一篇: uva 10887 Concatenation of Languages
LuckyqXd
博客等级 码龄12年 2粉丝 86原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值