poj 2459 Sumsets

Sumsets

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11612   Accepted: 3189

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

/*
* @Author: Lyucheng
* @Date:   2017-08-02 22:10:24
* @Last Modified by:   Lyucheng
* @Last Modified time: 2017-08-04 20:32:30
*/
/*
 题意:给你n个数,让你找出最大的d=a+b+c

 思路:3sum问题先转化成2sum问题,先处理出任意两个数的和,然后二分查找d-c的值是不是存在,并且组成d-c的值
    的两个加数是不是d和c,这个算法有个漏洞,就是如果d-c的值有多个,二分只能找到其中的一个,但是数据很水
    所以就水过去了。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define MAXN 1005

using namespace std;

struct Node{
    int x,y;
    int val;
    bool operator < (const Node & other) const {
        return val<other.val;
    }
}node[MAXN*MAXN];//存放两数之和

int n;
int a[MAXN];
int res;
int tol;

inline int findx(int x){
    int l=0,r=tol-1,m;
    while(l<=r){
        m=(l+r)/2;
        if(node[m].val==x){
            return m;
        }else if(node[m].val<x){
            l=m+1;
        }else{
            r=m-1;
        }
    }
    return -1;
}
void init(){
    tol=0;
    res=0;
}

int main(){
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    while(scanf("%d",&n)!=EOF&&n){
        init();
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        for(int i=0;i<n;i++){//n^2的时间处理一下
            for(int j=i+1;j<n;j++){
                node[tol].val=a[i]+a[j];
                node[tol].x=i;
                node[tol].y=j;
                tol++;
            }
        }

        sort(node,node+tol);

        bool flag=false;
        for(int i=n-1;i>=0;i--){
            for(int j=0;j<n;j++){
                if(j==i) continue;
                int cnt=a[i]-a[j];
                int pos=findx(cnt);
                if(pos==-1) continue;
                else {
                    if(min(node[pos].x,node[pos].y)!=min(i,j)&&max(node[pos].x,node[pos].y)!=max(i,j)){
                        printf("%d\n",a[i]);
                        flag=true;
                        break;
                    }
                }
            }
            if(flag==true){
                break;
            }
        }
        if(flag==false){
            puts("no solution");
        }
    }
    return 0;
}
时间: 2024-10-29 05:22:21

poj 2459 Sumsets的相关文章

POJ 2549 Sumsets

Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10593   Accepted: 2890 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 cons

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)

poj 2220 Sumsets

Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 16876   Accepted: 6678 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

POJ 2229 Sumsets (递推&amp;整数划分变形)

http://poj.org/problem?id=2229 思路:假设加数按从小到大的顺序.当n为奇数时,第一个数必须为1,此时f(n)=f(n-1):当n为偶数时,分两种情况讨论,若第一个数为1,则f(n)=f(n-1),若第一个数不为奇数,则所有数都不为奇数,提出一个公因子2出来,就是f(n/2),所以,f(n)=f(n-1)+f(n/2) 完整代码: /*63ms,4300KB*/ #include<cstdio> const int mod = 1e9; const int maxn

POJ 2229 Sumsets(找规律,预处理)

题目 参考了别人找的规律再理解 /* 8=1+1+1+1+1+1+1+1+1 1 8=1+1+1+1+1+1+1+2 2 3 8=1+1+1+1+2+2 8=1+1+1+1+4 4 5 8=1+1+2+2+2 8=1+1+2+4 6 7 8=2+2+2+2 8=2+2+4 8=4+4 8=8 8~9 */ /* 以下引用自博客:http://blog.csdn.net/scorpiocj/article/details/5940456 如果i为奇数,肯定有一个1,把f[i-1]的每一种情况加一个

POJ 2229 Sumsets(简单DP)

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) 1+1+1+1+1+2 3) 1+1+1

POJ 2229 Sumsets(递推,思考)

/* n = 7 1) 1+1+1+1+1+1+1 2) 1+1+1+1+1+2 3) 1+1+1+2+2 4) 1+1+1+4 5) 1+2+2+2 6) 1+2+4 */ /* 若i为偶数 : 若有 1 ,至少有两个 ---->f[i-2]的情况+两个1, 若没有1 , 将偶数分解/2-----> f[i/2]; 则dp[i] = dp[i / 2] + dp[i-2] 若i为奇数: 必定有 1, dp[i] = 减去1的序列,加上1 则dp[i] = dp[i – 1] */ 1 #in

poj 2229 Sumsets

题目大意: 一个数由2的幂次数的和构成,问有几种构成方式? 主要是找规律 代码如下 1 #include <cstdio> 2 #include <cstring> 3 int n; 4 #define M 1000000000 5 int dp[1000002]; 6 7 int main(int argc, char const *argv[]) 8 { 9 dp[1] = 1; 10 dp[2] = 2; 11 for(int i = 3; i <= 1000000;

poj 2229 Sumsets(dp 或 数学)

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) 1+1+1+1+