//
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < (k); ++i)
#define per(i, p, k) for(int i(p); i > (k); --i)
#define sz(x) (int)(x).size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fi first
#define se second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
string name{""};
time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
duration<float, std::milli> dur;
Timer() = default;
Timer(string nm): name(nm) {}
~Timer() {
end = high_resolution_clock::now(); dur= end - start;
cout << "@" << name << "> " << dur.count() << " ms" << '\n';
}
};
template <typename T = int> inline T in()
{
static T x;
std::cin >> x;
return x;
}
std::string yn(bool b)
{
if(b) return "YES\n";
else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
for(const auto& i : wek)out << i << ' ';
return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
out << '{'<<par.first<<", "<<par.second<<"}";
return out;
}
#define show(x) cerr << #x << " = " << x << '\n';
constexpr int maxn = 3e5 + 3, pol = 1<<19;
int le[maxn];
int fl(int w){
if(!~le[w])return w;
return le[w] = fl(le[w]);
}
void uni(int a, int b){
a = fl(a); b = fl(b);
if(a > b)swap(a, b);
if(a != b)le[b] = a;
}
int tr[pol*2+3];
void add(int w, int a){
w += pol;
while(w){
tr[w] = max(tr[w], a);
w /= 2;
}
}
int maks(int a, int b){
a += pol-1;
b += pol+1;
int o(0);
while(a+1 != b){
if(a % 2 == 0)o = max(o, tr[a+1]);
if(b % 2 == 1)o = max(o, tr[b-1]);
a /= 2; b /= 2;
}
return o;
}
std::int32_t main()
{
rep(i, 0, maxn)le[i] = -1;
std::cout.tie(nullptr); //for luck
std::cin.tie(nullptr); std::ios_base::sync_with_stdio(0);
int n(in()), d(in());
vector <int> v(n);
rep(i, 0, n)cin >> v[i];
vector <par<int>> p;
rep(i, 0, n)p.pb(v[i], i);
sort(all(p), [](par<int> a, par<int> b){
if(a.first != b.first)return a.first < b.first;
return a.second > b.second;
});
set <int> s;
int o(0);
for(auto [w, i]: p){
auto t(s.lower_bound(i));
if(t != s.end() && *t-i <= d)uni(i, *t);
if(t != s.begin() && i-*prev(t) <= d)uni(*prev(t), i);
s.insert(i);
int a(maks(fl(i), i)+1);
add(i, a);
o = max(o, a);
}
cout << o << '\n';
return 0;
}
# | 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... |