poj2549--Sumsets (sum)

Sumsets

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9983   Accepted: 2731

Description

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

Waterloo local 2001.06.02

转化成 a+b=c-d;

#include <cstdio>
#include <algorithm>
#define M 1010
using namespace std;
int num[M];
int main()
{
    int n;
    while(scanf("%d", &n) != EOF &&n!=0)
    {
        for(int i=0; i<n; i++)
            scanf("%d", &num[i]);
        sort(num, num+n);
        int re; bool flag=1;
        for(int i=n-1; i>=0; i--)
        {
            for(int j=n-1; j>=0; j--)
            {
                if(i==j)
                    continue;
                int multiply=num[i]-num[j];
                int rear=0, top= j-1;   //注意 top取值范围 ;
                while(rear<top)
                {
                    if(multiply==num[rear]+num[top])
                    {
                        re=num[i];    flag=0;
                    }
                    if(multiply>num[rear]+num[top])
                        rear++;
                    if(multiply<num[rear]+num[top])
                        top--;
                    if(!flag) break;
                }
                if(!flag) break;
            }
            if(!flag) break;
        }
        if(flag)
            printf("no solution\n");
        else
            printf("%d\n", re);
    }
    return 0;
} 
时间: 2024-12-14 07:36:16

poj2549--Sumsets (sum)的相关文章

POJ2549 Sumsets 折半枚举

题目大意是,一个集合中有N个元素,找出最大的S,满足条件A+B+C=S,并且这四个数都属于该集合,N不超过1000. 如果直接枚举O(n^4)显然复杂度太高,将等式转化一下A+B=S-C,此时分别对左右两边的值进行枚举,这一步复杂度为O(n ^ 2),接着就用二分法查找满足该等式的最大S值, 复杂度为O(n^2*log(n)). #include <stdio.h> #include <vector> #include <math.h> #include <str

Hash小结

Poj1480Eqs a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 ->a1x13+ a2x23+ a3x3=-(a4x43+ a5x53   问有多少个满足等式的非零x1,x2,x3,x4,x5组.) 中途相遇法,枚举x1,x2,x3得到左边式子的值插入hash表,然后枚举x4,x5找到对应的值就行了. #include <cstdio> #include <cstring> #include <algorithm> #include

poj 折半搜索

poj2549 Sumsets 题目链接: http://poj.org/problem?id=2549 题意:给你一个含有n(n<=1000)个数的数列,问这个数列中是否存在四个不同的数a,b,c,d,使a+b+c=d;若存在则输出最大的d 思路:完全暴力的话O(n^4),会T,可以考虑双向搜索,公式变形为a+b=d-c:分别枚举a+b和c-d,将值和下标存在结构体中,再二分查找即可 #include<cstdio> #include<iostream> #include&

POJ2229 Sumsets 基礎DP

Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 14344   Accepted: 5709 Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer

1677: [Usaco2005 Jan]Sumsets 求和

1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 626  Solved: 348[Submit][Status] Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers th

[BZOJ1677][Usaco2005 Jan]Sumsets 求和

1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1031  Solved: 603 [Submit][Status][Discuss] Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use onl

hdu 2709 Sumsets

Sumsets Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2159    Accepted Submission(s): 875 Problem Description Farmer John commanded his cows to search for different sets of numbers that sum t

Sumsets(3sum问题,枚举d,c二分a+b)

Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9997   Accepted: 2736 Description 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 consi

HDU 2709 Sumsets(递推)

Sumsets Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3094    Accepted Submission(s): 1225 Problem Description Farmer John commanded his cows to search for different sets of numbers that sum t

poj 2229 Sumsets 完全背包求方案总数

Sumsets Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7: 1) 1+1+1+1+1+1+1 2)