poj2549

排序题

View Code
#include <iostream>
#include
<cstdlib>
#include
<cstring>
#include
<cstdio>
#include
<algorithm>
using namespace std;

#define maxn 1005
#define inf 0x3f3f3f3f

struct A
{
int a, b;
int v;
} add[maxn
* maxn];

struct B
{
int c, d;
int v;
} mns[maxn
* maxn];

int f[maxn];
int n;
int n1, n2;

bool operator <(const A &a, const A &b)
{
return a.v < b.v;
}

bool operator <(const B &a, const B &b)
{
return a.v < b.v;
}

void input()
{
for (int i = 0; i < n; i++)
scanf(
"%d", &f[i]);
}

void make()
{
n1
= n2 = 0;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
{
add[n1].a
= f[i];
add[n1].b
= f[j];
add[n1].v
= f[i] + f[j];
n1
++;
}
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
{
mns[n2].d
= max(f[i], f[j]);
mns[n2].c
= min(f[j], f[i]);
mns[n2].v
= abs(f[i] - f[j]);
n2
++;
}
}

bool ok(int a, int b, int c, int d)
{
return a != b && a != c && a != d && b != c && b != d && c != d;
}

void work()
{
int temp = 0;
int ans = -inf;
for (int i = 0; i < n2; i++)
{
if (mns[i].d < ans)
continue;
while (add[temp].v < mns[i].v)
temp
++;
int j = temp;
while (j < n1 && add[j].v == mns[i].v)
{
if (ok(add[j].a, add[j].b, mns[i].c, mns[i].d))
{
ans
= max(ans, mns[i].d);
break;
}
j
++;
}
}
if (ans == -inf)
printf(
"no solution\n");
else
printf(
"%d\n", ans);
}

int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d", &n), n)
{
input();
make();
sort(add, add
+ n1);
sort(mns, mns
+ n2);
work();
}
return 0;
}

你可能感兴趣的:(poj)