poj 2459 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
Output
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=,r=tol-,m;
while(l<=r){
m=(l+r)/;
if(node[m].val==x){
return m;
}else if(node[m].val<x){
l=m+;
}else{
r=m-;
}
}
return -;
}
void init(){
tol=;
res=;
} int main(){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(scanf("%d",&n)!=EOF&&n){
init();
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
for(int i=;i<n;i++){//n^2的时间处理一下
for(int j=i+;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-;i>=;i--){
for(int j=;j<n;j++){
if(j==i) continue;
int cnt=a[i]-a[j];
int pos=findx(cnt);
if(pos==-) 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 ;
}
poj 2459 Sumsets的更多相关文章
- POJ 2229 Sumsets
Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 11892 Accepted: 4782 Descrip ...
- poj -2229 Sumsets (dp)
http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...
- POJ 2549 Sumsets
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10593 Accepted: 2890 Descript ...
- poj 2220 Sumsets
Sum ...
- poj 2229 Sumsets(dp)
Sumsets Time Limit : 4000/2000ms (Java/Other) Memory Limit : 400000/200000K (Java/Other) Total Sub ...
- poj 2229 Sumsets 完全背包求方案总数
Sumsets Description Farmer John commanded his cows to search for different sets of numbers that sum ...
- 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 2229 Sumsets DP
题意:给定一个整数N (1<= N <= 1000000),求出以 N为和 的式子有多少个,式子中的加数只能有2的幂次方组成 如5 : 1+1+1+1+1.1+1+1+2.1+2+2.1+ ...
随机推荐
- UEditor1.4.3.3整合Spring MVC和七牛
[前言] 项目中涉及将UEditor上传服务器整合进已有的基于Spring MVC的服务中,并且将上传到本地改为上传到七牛,看似简单的一个需求,实际做起来还是遇到了一些困难.在这里分享一下经验-- 七 ...
- Java中增强for循环的用法
此方法在jdk1.5之后才出现. 1:遍历数组 语法: for (Type value : array) { expression value; } 例子: void Sum() { int[] ar ...
- 在 macOS High Sierra 10.13 搭建 PHP 开发环境
2017 年 9 月 26 日,苹果公司正式发布了新一代 macOS,版本为 High Sierra (11.13). macOS High Sierra 预装了 Ruby(2.3.3).PHP(7. ...
- 已被.NET基金会认可的弹性和瞬态故障处理库Polly介绍
前言 本节我们来介绍一款强大的库Polly,Polly是一种.NET弹性和瞬态故障处理库,允许我们以非常顺畅和线程安全的方式来执诸如行重试,断路,超时,故障恢复等策略. Polly针对对.NET 4. ...
- easyui动态生成列
需求:一个id对应多个key value 将id作为标识列 key值作为表头 value作为值显示.数据表可分为两张表 param数据表: 下表一个id对应上表多个key及value 如下图 id_p ...
- Linux学习——shell编程之环境变量配置文件
小白学习,在学习中总结! shell编程之环境变量配置文件 一:环境变量配置文件 1 shell编程之环境变量配置 变量类型: 用户自定义变量(本地变量) 环境变量 :定义每个用户的操作环境,如pat ...
- Quartz源码——scheduler.start()启动源码分析(二)
scheduler.start()是Quartz的启动方式!下面进行分析,方便自己查看! 我都是分析的jobStore 方式为jdbc的SimpleTrigger!RAM的方式类似分析方式! Quar ...
- hdu3756三分基础题
Dome of Circus Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 使用SQLite做本地数据缓存的思考
前言 在一个分布式缓存遍地都是的环境下,还讲本地缓存,感觉有点out了啊!可能大家看到标题,就没有想继续看下去的欲望了吧.但是,本地缓存的重要性也是有的! 本地缓存相比分布式缓存确实是比较out和比较 ...
- sql的存储过程使用详解--基本语法
存储过程简介 SQL语句需要先编译然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给定参数(如果该存储 ...