1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <vector>
#define DEBUG(x) std::cerr << #x << " = " << x << std::endl; #define forall(G,u) for (int i_ = 0, __for_siz__ = (int) G[u].size(); i_ < __for_siz__; ++i_) #define all(x) (x).begin(), (x).end()
using std::cin; using std::cout; using std::endl;
inline int read() { int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
std::vector<int> price; std::vector<int> dest;
const int MAXN = 100000 + 10; const long long int INF = 0x3f3f3f3f3f3f3f3f;
struct Node { long long int id, x; Node(int _i = 0, long long int _x = 0) : id(_i), x(_x) {} bool operator < (const Node &th) const { return x < th.x; } } nodes[MAXN * 6]; int cnt = 0;
int n, bm;
namespace SegtTree { long long int minn[MAXN << 2]; #define lson (p << 1) #define rson (p << 1 | 1) void Update(int p) { minn[p] = std::min(minn[lson], minn[rson]); }
void buildTree(int p, int l, int r) { if (l == r) return (void) (minn[p] = -INF); int mid = (l + r) >> 1; buildTree(lson, l, mid); buildTree(rson, mid + 1, r); Update(p); }
void Modify(int p, int l, int r, int pos, long long int k) { if (l == r) return (void) (minn[p] = k); int mid = (l + r) >> 1; if (pos <= mid) Modify(lson, l, mid, pos, k); else Modify(rson, mid + 1, r, pos, k); Update(p); }
long long int Query() { return minn[1]; } }
int main() { bm = 6; for (int i = 1; i <= bm; ++i) price.push_back(read()); n = read(); for (int i = 1; i <= n; ++i) dest.push_back(read()); std::sort(all(price)); price.erase(std::unique(all(price)), price.end()); std::sort(all(dest)); dest.erase(std::unique(all(dest)), dest.end()); n = (int) dest.size(); bm = (int) price.size(); SegtTree::buildTree(1, 1, n); for (int i = 0; i < n; ++i) { for (int j = 0; j < bm; ++j) { nodes[++cnt] = Node(i + 1, dest[i] - price[j]); } } std::sort(nodes + 1, nodes + 1 + cnt); long long int ans = INF; for (int i = 1; i <= cnt; ++i) { SegtTree::Modify(1, 1, n, nodes[i].id, nodes[i].x); ans = std::min(ans, nodes[i].x - SegtTree::Query()); } printf("%lld\n", ans); return 0; }
|