题解:
给n个数,求 a + b + c = d成立的最大的d,没有输出no solution
理解:
开始做的时候都把折半枚举给忘了;
然后看了一下别人的思路,忽然发现是这样枚举的;
使a + b = d - c成立的最大值d;
可以枚举a + b再二分找d - c;
于是我就这样写了...重点是a, b, c, d不同,并且d是最大的;
听他们说用3sum的算法可以做,但我不造怎么弄得,虽然他们给了链接;
但链接过去是英文的,英语是硬伤....
代码如下:
代码太丑了....
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef pair<int , pii> pip;
#define x first
#define y second
bool cmp(pip p1, pip p2)
{
if (p1.x == p2.x) return p1.y.x > p2.y.x;
else return p1.x < p2.x;
}
void sum_sub(vector<pip> &S_1, vector<int> &S, int m)
{
for (int i = 0; i < S.size(); ++i)
{
for (int j = 0; j < S.size(); ++j)
{
if (i == j) continue;
S_1.push_back(pip(S[i] + S[j] * m, pii(i, j)));
}
}
sort(S_1.begin(), S_1.end(), cmp);
}
bool OK(pip p1, pip p2)
{
return p1.x == p2.x
&& p1.y.x != p2.y.x
&& p1.y.x != p2.y.y
&& p1.y.y != p2.y.x
&& p1.y.y != p2.y.y;
}
int solve(vector<pip> &S_1, vector<pip> &S_2, vector<int> S)
{
int mx = -(0x7fffffff - 100);
for (int i = 0; i < S_1.size(); ++i)
{
int id = lower_bound(S_2.begin(), S_2.end(), pip(S_1[i].x, pii(0x7fffffff, 0x7fffffff)), cmp) - S_2.begin();
if (id != S_1.size() && OK(S_1[i], S_2[id]))
{
mx = (mx < S[S_2[id].y.x] ? S[S_2[id].y.x] : mx);
}
}
return mx;
}
int main()
{
int n;
while (scanf("%d", &n) && n)
{
vector<int> S(n);
for (int i = 0; i < n; ++i)
{
scanf("%d", &S[i]);
}
vector<pip> S_1, S_2;
sum_sub(S_1, S, 1);
sum_sub(S_2, S, -1);
int ans = solve(S_1, S_2, S);
ans == -(0x7fffffff - 100) ? printf("no solution\n") : printf("%d\n", ans);
}
return 0;
}