poj 2459 Sumsets
Posted 勿忘初心0924
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
/* * @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; }
以上是关于poj 2459 Sumsets的主要内容,如果未能解决你的问题,请参考以下文章