poj 2549 3SUM直接枚举
来源:互联网 发布:淘宝的苹果手机靠谱吗 编辑:程序博客网 时间:2024/06/05 17:09
题意:
给一个n(1000)个元素的set(元素不重复)。
找到4个值,使得 a + b + c = d。
问d最大为多少,若找不到,输出无解。
解析:
wiki上有一种n方的算法来解决a+b+c==0的方法:
https://en.wikipedia.org/wiki/3SUM
如果放到这题来的话,枚举d,然后按照以上的伪代码来做就行了。
唯独要注意的就是要加一句判断,所有的元素都要不相同。
然后本来的方法是先将a+b的值枚举出来,保存下来,然后再去枚举d-c,二分a+b的值,找出最大的d,不知道为何一直wa。
代码(ac):
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long longusing namespace std;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = 4 * atan(1.0);const double ee = exp(1.0);const int maxn = 1000 + 10;LL num[maxn];int main(){#ifdef LOCAL freopen("in.txt", "r", stdin);#endif // LOCAL int n; while (~scanf("%d", &n) && n) { for (int i = 0; i < n; i++) { scanf("%lld", &num[i]); } sort(num, num + n); int flag = false; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= n - 3; j++) { int a = num[j]; int st = j + 1; int ed = n - 1; while (st < ed) { int b = num[st]; int c = num[ed]; if (a + b + c == num[i] && a != num[i] && b != num[i] && c != num[i]) { printf("%lld\n", num[i]); flag = true; break; } else if (a + b + c > num[i]) { ed--; } else { st++; } } if (flag) break; } if (flag) break; } if (!flag) puts("no solution"); } return 0;}
代码(wa):
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long longusing namespace std;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = 4 * atan(1.0);const double ee = exp(1.0);const int maxn = 1000000 + 10;struct Node{ LL a, b; LL sum; bool operator < (const Node& other) const { return sum < other.sum; }} p[maxn];LL num[maxn];int main(){#ifdef LOCAL freopen("in.txt", "r", stdin);#endif // LOCAL int n; while (~scanf("%d", &n) && n) { for (int i = 0; i < n; i++) { scanf("%lld", &num[i]); } sort(num, num + n); int cnt = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { p[cnt].a = num[i]; p[cnt].b = num[j]; p[cnt++].sum = num[i] + num[j]; } } } sort(p, p + cnt); LL ans = -inf;// cout << ans << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { LL t = num[i] - num[j]; int lo = 0, hi = cnt; for (int k = 0; k < 50; k++) { int mi = (lo + hi) >> 1; if (p[mi].sum < t) lo = mi; else hi = mi; } if (p[lo].sum == t) { if (num[i] >= ans && p[lo].a != num[i] && p[lo].b != num[j]) { ans = num[i]; } } } } } if (ans == -inf) puts("no solution"); else printf("%lld\n", ans); } return 0;}
0 0
- poj 2549 3SUM直接枚举
- poj 2549 3SUM
- poj 3080 Blue Jeans 直接枚举
- poj 1775 Sum of Factorials 枚举子集
- 折半枚举+3SUM--poj2549
- poj 2785 4 Values whose Sum is 0 (二分+枚举)
- poj 2785--4 Values whose Sum is 0(折半枚举)
- POJ 2785 4 VALUES WHOSE SUM IS 0 【折半枚举】
- [折半枚举] poj 2785 4 Values whose Sum is 0
- POJ 2785 4 Values whose Sum is 0(折半枚举)
- POJ 2785 4 Values whose Sum is 0 折半枚举
- POJ - 2785 4 Values whose Sum is 0(二分枚举)
- POJ-Values whose Sum is 0-折半枚举
- 4 Values whose Sum is 0 (POJ-2785) (折半枚举)
- POJ 2785 4 Values whose Sum is 0【折半枚举】
- 【poj 2785】4 Values whose Sum is 0 【折半枚举】
- Poj 2549 Sumsets (3Sum问题)
- leetcode 15. 3Sum 暴力枚举
- HTML5纯div饼图
- Openjudge2984 十六进制转十进制(模拟)
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- AppScan使用
- 【Lua】【3】Lua之使用表
- poj 2549 3SUM直接枚举
- POJ--1321棋盘问题
- 随笔(2015.7)
- 制作网页雪花飘飘效果
- 算法——常用排序算法介绍
- EmguCV的配置和获取摄像头画面
- Android camera预览参数以及实际图像大小设置
- Leetcode40 Combination Sum II
- python多线程编程(5): 条件变量同步