poj 2549 3SUM直接枚举

题意:

给一个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 long

using 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 long

using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值