POJ 2549 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
Output
Sample Input
5
2
3
5
7
12
5
2
16
64
256
1024
0
Sample Output
12
no solution
Source
#include <cstdio>
#include <algorithm>
using namespace std; const int MAXN = 1000+5;
int n;
int a[MAXN]; struct S{
int val;
int i, j;
bool operator < (const S& b)const
{
return val < b.val;
}
};
S l[MAXN*MAXN], r[MAXN*MAXN]; bool ok(S& a, S& b)
{
return a.i != b.i && a.j != b.j && a.i != b.j && a.j != b.i;
} void solve()
{
int lcnt = 0;
for(int i = 0; i < n; ++i){
for(int j = i+1; j < n; ++j){
l[lcnt].val = a[i]+a[j];
l[lcnt].i = i;
l[lcnt++].j = j;
}
}
int rcnt = 0;
for(int i = 0; i < n; ++i){
for(int j = i+1; j < n; ++j){
r[rcnt].val = a[i]-a[j];
r[rcnt].i = i;
r[rcnt++].j = j;
r[rcnt].val = a[j]-a[i];
r[rcnt].i = j;
r[rcnt++].j = i;
}
}
sort(l, l+lcnt);
int res = 0xffffffff;
for(int i = 0; i < rcnt; ++i){
int d = lower_bound(l, l+lcnt, r[i])-l;
if(ok(l[d], r[i]) && l[d].val == r[i].val){
res = max(res, r[i].val+a[r[i].j]);
}
}
if(res == 0xffffffff)
printf("no solution\n");
else
printf("%d\n", res);
} int main()
{
while(scanf("%d", &n), n){
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
solve();
}
return 0;
}
POJ 2549 Sumsets的更多相关文章
- POJ 2549 Sumsets(折半枚举+二分)
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11946 Accepted: 3299 Descript ...
- POJ 2549 Sumsets hash值及下标
题目大意:找到几何中的4个数字使他们能够组成 a+b+c=d , 得到最大的d值 我们很容易想到a+b = d-c 那么将所有a+b的值存入hash表中,然后查找能否在表中找到这样的d-c的值即可 因 ...
- [poj] 2549 Sumsets || 双向bfs
原题 在集合里找到a+b+c=d的最大的d. 显然枚举a,b,c不行,所以将式子移项为a+b=d-c,然后双向bfs,meet int the middle. #include<cstdio&g ...
- UVA 10125 - Sumsets(POJ 2549) hash
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Divide and conquer:Sumsets(POJ 2549)
数集 题目大意:给定一些数的集合,要你求出集合中满足a+b+c=d的最大的d(每个数只能用一次) 这题有两种解法, 第一种就是对分,把a+b的和先求出来,然后再枚举d-c,枚举的时候输入按照降序搜索就 ...
- POJ 2549:Subsets(哈希表)
[题目链接] http://poj.org/problem?id=2549 [题目大意] 给出一个数集,从中选择四个元素,使得a+b+c=d,最小化d [题解] 我们对a+b建立Hash_table, ...
- POJ 2229 Sumsets
Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 11892 Accepted: 4782 Descrip ...
- POJ 2549 二分+HASH
题目链接:http://poj.org/problem?id=2002 题意:给定一个n个数字组成的序列,然后求出4个数使得a+b+c=d,求d的最大值.其中a,b,c,d要求是给定序列的数,并且不能 ...
- POJ 2549
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8235 Accepted: 2260 Descripti ...
随机推荐
- 安卓 unit 测试与 instrument 测试的代码共享
假如你有一款安卓应用,其包含一系列测试类,其中一部分是unit 测试(位于 src/test),其余为instrument 测试(位于 src/androidTest). 那么问题来了:你有一些想在所 ...
- Zabbix 安装及微信短信提醒
Zabbix简介 Zabbix 近几年得到了各大互联网公司的认可,当然第一点归功与它强大的监控功能,第二点免费开源也得到了广大用户的青睐.Zabbix 能将操作系统中的绝大部分指标进行监控,比如(CP ...
- [C++]虚函数-同名访问
首先来看一下派生类和基类成员同名事的处理规则: 派生类内定义了一个与基类同名的成员,该现象称为同名覆盖,此时,无论派生类内部成员函数还是派生类的对象访问同名成员,如果未加任何特殊标识,则访问派生类中重 ...
- C#中 ? 和?? 的用法
C#中 ?? 和? 的意思 1.? 如果直接定义一个 值类型,给负值null:就会提示“无法将 Null转换成‘值类型(比如:int)’,因为他是一种不可为null的值 de类型” 例如 int in ...
- GOOGLE搜索秘籍完全公开
一,GOOGLE简介 Google(www.google.com)是一个搜索引擎,由两个斯坦福大学博士生Larry Page与Sergey Brin于1998年9月发明,Google Inc. 于19 ...
- hdu 4335 What is N?
此题用到的公式:a^b%c=a^(b%phi(c)+phi(c))%c (b>=phi(c)). 1.当n!<phi(p)时,直接暴力掉: 2.当n!>=phi(p) &&a ...
- Android ListView点击失效
item中存在 ImageButton 等可以点击的组件,这会抢先获得ListView的焦点. 从而导致item点击失效
- http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html(重要)
http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html
- hdu1116
http://acm.hdu.edu.cn/showproblem.php?pid=1116 #include<stdio.h> #include<math.h> #inclu ...
- 谈Delphi中SSL协议的应用(好多相关文章)
摘要:本文主要介绍如何在Delphi中使用SSL协议.一共分为七个部分:(1)SSL协议是什么?(2)Delphi中如何使用SSL协议?(3)SSL客户端编程实例.(4)SSL服务端编程实例.(5)S ...