Sumsets(3sum问题,枚举d,c二分a+b)
| 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
Output
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)的更多相关文章
- POJ3189二分最大流(枚举下界,二分宽度,最大流判断可行性)
题意: 有n头猪,m个猪圈,每个猪圈都有一定的容量(就是最多能装多少只猪),然后每只猪对每个猪圈的喜好度不同(就是所有猪圈在每个猪心中都有一个排名),然后要求所有的猪都进猪圈,但是要求所有 ...
- POJ 2549 Sumsets(折半枚举+二分)
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11946 Accepted: 3299 Descript ...
- 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution
题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...
- 【枚举】【二分答案】【分块答案】【BFS】【最大流】【Dinic】bzoj1189 [HNOI2007]紧急疏散evacuate
[法一]枚举Time(0~N*M): S->'.'(1); 'D'->T(Time); '.'->'D'(dis(用BFS预处理,注意一旦到达'D',BFS就不能继续扩展了,注意di ...
- poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 6851 Description ...
- hdu 4282 枚举,非二分
http://acm.hdu.edu.cn/showproblem.php?pid=4282 对于方程X^Z + Y^Z + XYZ = K,已知K求此方程解的个数,其中要求X<Y,Z>1 ...
- Codeforces gym101612 L.Little Difference(枚举+二分)
传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...
- Codeforces J. Sagheer and Nubian Market(二分枚举)
题目描述: Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes in ...
- HDU - 4430 Yukari's Birthday(二分+枚举)
题意:已知有n个蜡烛,过生日在蛋糕上摆蜡烛,将蜡烛围成同心圆,每圈个数为ki,蛋糕中心最多可摆一个蜡烛,求圈数r和看,条件为r*k尽可能小的情况下,r尽可能小. 分析:n最大为1012,k最少为2,假 ...
随机推荐
- Invert Binary Tree 解答
Quetion Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Solution 1 -- R ...
- Qt Creator编译时:cannot open file 'debug\QtGuiEx.exe' File not found
Qt Creator编译时:cannot open file 'debug\QtGuiEx.exe' File not found 利用Qt Creator编译工程时,出现如题目所示的错误,其中红色部 ...
- HTML5 Canvas绘图系列之一:圆弧等基础图形的实现
之前的一个微信项目已经要结项了,最近整理一下项目中使用较多的canvas画图方面的知识吧,打算写个3,4篇的样子.本篇主要介绍基础操作和弧线画法. 之后再写一下趋势图,直方图,文本图像处理的. 言归正 ...
- structs 拦截器
首先,要跟大家道个歉,前一阵子为给客户个一个DEMO,忙得不可开交,所以很久没有更新Blog.提到这个DEMO我想顺便跟大家分享一下心得——如果大家希望快速开发,一个类似Struts 2这样的简单方便 ...
- 【数学.前左上计数法】【HDU1220】Cube
Cube Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 数据库学习之ADO.NET五大对象
1 [ADO.NET] ado.net 是一种数据访问技术,使得应用程序能够连接到数据存储,并以各种方式操作存储在里面的数据. 2 [ADO.NET五大常用对象] Connec ...
- js操作iframe框架时应该屡清楚的一些概念
1.获取iframe的window对象 存在跨域访问限制. iframeElement.contentWindow 兼容 2.获取iframe的document对象 存在跨域访问限制. chrome: ...
- js判断是否安装flash
<script type="text/javascript"> (function () { var noFlash = "你的浏览器没有安装Flash,会影 ...
- iOS控制器的创建方式
iOS控制器的创建.除了常见的alloc init外还有通过加载storyboard和xib的方式,下边逐一展开: 1.代码alloc init 创建方式 ViewController *vc= [[ ...
- L - 辗转相除法(第二季水)
Description The least common multiple (LCM) of a set of positive integers is the smallest positive i ...