This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define X first
#define Y second
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
for (; l != r; ++l)
cout << *l << " \n"[l + 1 == r];
}
#ifdef Doludu
#define test(x...) abc("[" + string(#x) + "]", x);
#define owo freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#else
#define test(x...) void(0);
#define owo ios::sync_with_stdio(false), cin.tie(0)
#endif
const int N = 400087;
struct BIT {
int val[N];
void init() {
fill(val, val + N, 0);
}
void add(int p, int v) {
p += 5;
for (int i = p; i < N; i += i & (-i))
val[i] = max(val[i], v);
}
int query(int p) {
p += 5;
int ans = 0;
for (int i = p; i > 0; i -= i & (-i))
ans = max(ans, val[i]);
return ans;
}
} bit1, bit2;
vector <int> a;
int n;
int ask(int x) {
vector <vector <int>> dp(n, vector <int> (3, 0));
vector <int> v;
for (int i : a)
v.pb(i), v.pb(i + x);
sort(all(v)), v.resize(unique(all(v)) - v.begin());
bit1.init(), bit2.init();
dp[0][0] = 1, dp[0][1] = 1, dp[0][2] = 0;
int ans = 1;
for (int i = 0; i < n; ++i) {
int p = lower_bound(all(v), a[i]) - v.begin();
int q = lower_bound(all(v), a[i] + x) - v.begin();
if (i) {
dp[i][0] = bit1.query(p) + 1;
dp[i][1] = max(bit1.query(q), bit2.query(q)) + 1;
dp[i][2] = bit2.query(p) + 1;
}
bit1.add(p + 1, dp[i][0]);
bit2.add(q + 1, dp[i][1]);
ans = max({ans, dp[i][0], dp[i][1], dp[i][2]});
}
return ans;
}
int main () {
owo;
int d;
cin >> n >> d;
a.resize(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
if (d == 1000000000) {
vector <int> pre(n + 1), suf(n + 1);
{
vector <int> v;
for (int i = 0; i < n; ++i) {
if (v.empty() || v.back() < a[i])
v.pb(a[i]);
else
*lower_bound(all(v), a[i]) = a[i];
pre[i + 1] = v.size();
}
}
for (int &i : a)
i *= -1;
{
vector <int> v;
for (int i = n - 1; ~i; --i) {
if (v.empty() || v.back() < a[i])
v.pb(a[i]);
else
*lower_bound(all(v), a[i]) = a[i];
suf[i] = v.size();
}
}
int ans = 0;
for (int i = 0; i < n; ++i)
ans = max(ans, pre[i] + suf[i]);
cout << ans << endl;
return 0;
}
lli l = 0, r = d + 1;
while (r - l > 10) {
lli m1 = (l + l + r) / 3, m2 = (l + r + r) / 3;
if (ask(m1) < ask(m2))
l = m1;
else
r = m2;
}
int ans = 0;
for (int i = l; i < r; ++i)
ans = max(ans, ask(i));
l = -d, r = 1;
while (r - l > 10) {
lli m1 = (l + l + r) / 3, m2 = (l + r + r) / 3;
if (ask(m1) < ask(m2))
l = m1;
else
r = m2;
}
for (int i = l; i < r; ++i)
ans = max(ans, ask(i));
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |