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 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
题解:给一个序列,让找不同的a,b,c,d在集合s中,使得a+b+c=d,如果能找到输出d,否则输出no solution;
乍一看完全没思路,也许不敢动手去写,可以选从大到小排序,枚举d,c;二分a+b等于d-c即可;
extern "C++"{
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
void SI(int &x){scanf("%d",&x);}
void SI(double &x){scanf("%lf",&x);}
void SI(char *x){scanf("%s",x);}
//void SI(LL &x){scanf("%lld",&x);} void PI(int &x){printf("%d",x);}
void PI(double &x){printf("%lf",x);}
void PI(char *x){printf("%s",x);}
//void PI(LL &x){printf("%lld",x);} }
const int MAXN = 1010;
int a[MAXN]; int main(){
int n;
while(scanf("%d",&n),n){
for(int i = 0;i < n;i++)SI(a[i]);
sort(a,a + n);
int ans,flot = 0;
for(int i = n - 1;i >= 0;i--){
if(flot)break;
for(int j = n - 1;j >= 0;j--){
if(flot)break;
if(i == j)continue;
int sum = a[i] - a[j],l = 0,r = j - 1;
while(l < r){
if(a[l] + a[r] == sum && i != l && i != r){
ans = a[i];
flot = 1;
break;
}
if(a[l] + a[r] > sum)
r--;
else
l++;
}
}
}
if(flot)
printf("%d\n",ans);
else
puts("no solution");
}
return 0;
}

  

Sumsets(3sum问题,枚举d,c二分a+b)的更多相关文章

  1. POJ3189二分最大流(枚举下界,二分宽度,最大流判断可行性)

    题意:       有n头猪,m个猪圈,每个猪圈都有一定的容量(就是最多能装多少只猪),然后每只猪对每个猪圈的喜好度不同(就是所有猪圈在每个猪心中都有一个排名),然后要求所有的猪都进猪圈,但是要求所有 ...

  2. POJ 2549 Sumsets(折半枚举+二分)

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11946   Accepted: 3299 Descript ...

  3. 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution

    题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...

  4. 【枚举】【二分答案】【分块答案】【BFS】【最大流】【Dinic】bzoj1189 [HNOI2007]紧急疏散evacuate

    [法一]枚举Time(0~N*M): S->'.'(1); 'D'->T(Time); '.'->'D'(dis(用BFS预处理,注意一旦到达'D',BFS就不能继续扩展了,注意di ...

  5. poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 6851 Description ...

  6. hdu 4282 枚举,非二分

    http://acm.hdu.edu.cn/showproblem.php?pid=4282 对于方程X^Z + Y^Z + XYZ = K,已知K求此方程解的个数,其中要求X<Y,Z>1 ...

  7. Codeforces gym101612 L.Little Difference(枚举+二分)

    传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...

  8. Codeforces J. Sagheer and Nubian Market(二分枚举)

    题目描述: Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes in ...

  9. HDU - 4430 Yukari's Birthday(二分+枚举)

    题意:已知有n个蜡烛,过生日在蛋糕上摆蜡烛,将蜡烛围成同心圆,每圈个数为ki,蛋糕中心最多可摆一个蜡烛,求圈数r和看,条件为r*k尽可能小的情况下,r尽可能小. 分析:n最大为1012,k最少为2,假 ...

随机推荐

  1. tomcat那些事

    Tomcat7.0.22安装配置 1.下载tomcat7.0.22  下载地址:http://tomcat.apache.org/download-70.cgi 2.添加系统环境变量,我的电脑-> ...

  2. LeeCode(Database)-Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  3. ORACLE 中写入txt文本与从Txt文件中读入数据 修改表结构

    --创建一个表 DROP TABLE TEST CASCADE CONSTRAINTS ; CREATE TABLE TEST(A VARCHAR(30),B VARCHAR(30)); --查看具体 ...

  4. 使用 apache ant 轻松实现文件压缩/解压缩(转)

    原文地址:http://blog.csdn.net/irvine007/article/details/6779492 maven配置ant包: <dependency> <grou ...

  5. 用Robocod游戏来学习JAVA

    Robocode(用游戏来学习Java技术还是用Java来玩游戏?)用你的JAVA编程技术来玩游戏吧!不会JAVA?那就用游戏来学习JAVA吧!什么是Robocode? 其实我对机器人一直很感兴趣.我 ...

  6. Unity uGUI 登录界面

    小记:进入冬季,天气确实变冷了,即使这样也不能作为自己不学习的理由!!! 昨天咱们一起学习了UGUI的Button的相关知识,那么今天咱们做一个简单的登录Demo,有些人可能不屑但是多学点总没什么坏处 ...

  7. javascript 控制input

    1.只允许输入数字     <input name="username" type="text" onkeyup="value=this.val ...

  8. REST总结

    REST是Roy Thomas Fielding博士于2000年在他的博士论文中阐述的一种架构风格和设计原则.REST并非一种协议或者标准,事实上它只是阐述了HTTP协议的设计初衷:现在HTTP在网络 ...

  9. c# 异步调用简单例子(转载)

    首先来看一个简单的例子: 小明在烧水,等水烧开以后,将开水灌入热水瓶,然后开始整理家务 小文在烧水,在烧水的过程中整理家务,等水烧开以后,放下手中的家务活,将开水灌入热水瓶,然后继续整理家务 这也是日 ...

  10. 学习asp.net比较完整的流程

    如果你已经有较多的面向对象开发经验,跳过以下这两步: 第一步 掌握一门.NET面向对象语言,C#或VB.NET 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.NE ...