From bddf3a2de22a7ed872ea9e00e834874e4c4a3565 Mon Sep 17 00:00:00 2001 From: Ta180m Date: Tue, 13 Aug 2019 19:48:26 -0500 Subject: Create nocross.cpp --- 2017/February/Platinum/nocross.cpp | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 2017/February/Platinum/nocross.cpp diff --git a/2017/February/Platinum/nocross.cpp b/2017/February/Platinum/nocross.cpp new file mode 100644 index 0000000..5ce1b7c --- /dev/null +++ b/2017/February/Platinum/nocross.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long ll; +typedef pair ii; +typedef vector vi; +typedef vector vii; +constexpr auto INF = (int)1e9; +constexpr auto MAXN = 100005; + +int N, A[100005], B[100005], R[100005], seg[4 * MAXN] = { 0 }; + +void update(int x, int v, int l = 0, int r = -1, int n = 1) { + if (r == -1) r = N - 1; + if (l == r) seg[n] = max(v, seg[n]); + else { + int m = (l + r) >> 1; + x <= m ? update(x, v, l, m, n << 1) : update(x, v, m + 1, r, n << 1 | 1); + seg[n] = max(seg[n << 1], seg[n << 1 | 1]); + } +} + +int query(int a, int b, int l = 0, int r = -1, int n = 1) { + if (r == -1) r = N - 1; + if (l > b || r < a) return 0; + if (l >= a && r <= b) return seg[n]; + int m = (l + r) >> 1; + return max(query(a, b, l, m, n << 1), query(a, b, m + 1, r, n << 1 | 1)); +} + +int main() { + ifstream cin("nocross.in"); + ofstream cout("nocross.out"); + + cin >> N; + for (int i = 0; i < N; ++i) cin >> A[i], A[i]--; + for (int i = 0; i < N; ++i) cin >> B[i], B[i]--; + for (int i = 0; i < N; ++i) R[B[i]] = i; + + for (int i = 0; i < N; ++i) { + vii u; + for (int j = max(A[i] - 4, 0); j < min(A[i] + 5, N); ++j) u.emplace_back(R[j], query(0, R[j] - 1) + 1); + for (auto q : u) update(q.first, q.second); + } + + cout << query(0, N - 1) << endl; +} -- cgit v1.2.3-70-g09d2