POJ 2549---Sumsets(二分枚举)

2023-10-17 06:10
文章标签 二分 poj 枚举 2549 sumsets

本文主要是介绍POJ 2549---Sumsets(二分枚举),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

传送门:http://poj.org/problem?id=2549
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 都是集合中的数,且满足a + b + c = d,要求出最大的d。

思路:
把a + b + c = d 转换成 a + b = d - c,先遍历每种两个数和的情况,写入结构体。在遍历每种两数相减的所有情况,用lower_bound从结构体中搜索出来,只要其中构成的数不冲突,证明这个等式成立,再用从中选择最大的ans。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <map>using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int MAX = 1e3 + 10;
int N;
ll S[MAX];
struct node
{ll s, x, y;node(){};node(ll a, ll b, ll c) {s = a;x = b;y = c;}friend bool operator < (node a, node k) {return a.s < k.s;}
}sum[MAX * MAX];
bool cmp(ll x, ll y)
{return x > y;
}
int main ()
{while(scanf("%d", &N), N) {for(int i = 0; i < N; i++) {scanf("%lld", &S[i]);}sort(S, S+N, cmp);int cnt = 0;for(int i= 0; i < N; i++) {for(int j= i+1; j < N; j++) {sum[cnt].x = S[i];sum[cnt].y = S[j];sum[cnt].s = S[i] + S[j];cnt++;}}sort(sum, sum+cnt);ll ans = -inf;for(int i= 0; i < N; i++) {for(int j= 0; j < N; j++) {if(i == j) continue;node temp (S[i]-S[j], 0, 0);int pos = lower_bound(sum, sum+cnt, temp) - sum;for(int k = pos; k < cnt && sum[k].s == S[i] - S[j]; k++) {if(sum[k].x != S[i] && sum[k].x != S[j] && sum[k].y != S[i] && sum[k].y != S[j]) {ans = max(ans, S[i]);}}}}if(ans == -inf) cout << "no solution" << endl;else cout << ans << endl;}return 0;
}

这篇关于POJ 2549---Sumsets(二分枚举)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/223447

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问