# 从一个问题的多个算法看算法的优化

#include <cstdio>

const int N = 100 + 10;
const int inf = 1000000000;

int A[N], B[N];
int n;

inline bool in_range(int left, int right, int x)
{
return x >= left && x <= right;
}

bool check(int left, int right, int& d)
{
int tmp, i;
if (left > right)
{
tmp = left;
left = right;
right = tmp;
}
for (i=0; i<n; i++) if (!in_range(left, right, A[i] + B[i]) && !in_range(left, right, A[i] - B[i])) return false;
d = right - left;
return true;
}

int main()
{
int res, t, d, i, j, k;
scanf("%d", &t);
while (t --)
{
scanf("%d", &n);
for (i=0; i<n; i++) scanf("%d", &A[i]);
for (i=0; i<n; i++) scanf("%d", &B[i]);
if (n == 1)
{
printf("0\n");
continue;
}
res = inf;
for (i=0; i<n-1; i++)
for (j=i+1; j<n; j++)
{
if (check(A[i] + B[i], A[j] + B[j], d) && d < res) res = d;
if (check(A[i] + B[i], A[j] - B[j], d) && d < res) res = d;
if (check(A[i] - B[i], A[j] + B[j], d) && d < res) res = d;
if (check(A[i] - B[i], A[j] - B[j], d) && d < res) res = d;
}
printf("%d\n", res);
}
return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int N = 100 + 10;
const int inf = 1000000000;

int A[N], B[N];
int n;

bool check(int left, int& d)
{
int right, i;
right = left;
for (i=0; i<n; i++)
if (A[i] + B[i] >= left && A[i] - B[i] >= left) right = max(right, min(A[i] + B[i], A[i] - B[i]));
else if (A[i] + B[i] >= left) right = max(right, A[i] + B[i]);
else if (A[i] - B[i] >= left) right = max(right, A[i] - B[i]);
else return false;
d = right - left;
return true;
}

int main()
{
int res, t, d, i, j;
scanf("%d", &t);
while (t --)
{
scanf("%d", &n);
for (i=0; i<n; i++) scanf("%d", &A[i]);
for (i=0; i<n; i++) scanf("%d", &B[i]);
res = inf;
for (i=0; i<n; i++)
{
if (check(A[i] + B[i], d) && d < res) res = d;
if (check(A[i] - B[i], d) && d < res) res = d;
}
printf("%d\n", res);
}
return 0;
} 

#include <cstdio>
#include <algorithm>

using namespace std;

const int N = 100 + 10;
const int inf = 1000000000;

struct ITEM
{
int val, id;
ITEM(int val=0, int id=0):val(val), id(id){}
};

int A[N], B[N], cnt[N];
ITEM C[2*N];
int n;

bool operator < (const ITEM& a, const ITEM& b)
{
return a.val < b.val;
}

int main()
{
int res, t, s, i, j;
scanf("%d", &t);
while (t --)
{
scanf("%d", &n);
for (i=0; i<n; i++) scanf("%d", &A[i]);
for (i=0; i<n; i++) scanf("%d", &B[i]);
for (i=0; i<n; i++)
{
C[i*2] = ITEM(A[i] + B[i], i);
C[i*2+1] = ITEM(A[i] - B[i], i);
cnt[i] = 0;
}
sort(C, C + 2 * n);
s = 0;
for (i=0; i<2*n; i++)
{
cnt[C[i].id] ++;
if (cnt[C[i].id] == 1) s ++;
if (s == n) break;
}
j = 0;
for (; cnt[C[j].id]==2; j++) cnt[C[j].id] --;
res = C[i].val - C[j].val;
for (i++; i<2*n; i++)
{
cnt[C[i].id] ++;
for (; cnt[C[j].id]==2; j++) cnt[C[j].id] --;
res = min(res, C[i].val - C[j].val);
}
printf("%d\n", res);
}
return 0;
} 

#### 几个优化方法

2017-04-01 11:02:55

#### 算法+优化方案（一）

2017-06-01 20:44:48

#### 性能能优化的一般思路

2018-06-28 11:56:40

#### 性能优化的思路和步骤

2017-10-18 12:53:44

#### 递归算法的优化思路和CPS

2016-08-29 21:28:38

#### 机器学习中常见的几种优化方法

2016-06-08 15:11:36

#### 优化算法分析:从q群智能优化到优化技巧

2018-07-02 16:32:13

#### 一个小算法的优化

2017-04-20 19:06:35

#### KERAS各种优化方法总结

2018-03-14 23:42:08