This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
DavitMarg
In a honky-tonk,
Down in Mexico
*/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <random>
#include <chrono>
#define mod 998244353ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 500005;
int n, d;
int a[N];
int par[N], sz[N], mn[N];
void init_dsu()
{
for (int i = 1; i <= n; i++)
{
par[i] = i;
mn[i] = i;
sz[i] = 1;
}
}
int get_par(int u)
{
if (par[u] == u)
return u;
return par[u] = get_par(par[u]);
}
void merge(int u, int v)
{
u = get_par(u);
v = get_par(v);
if (u == v)
return;
if (sz[u] < sz[v])
swap(u, v);
par[v] = u;
sz[u] += sz[v];
mn[u] = min(mn[u], mn[v]);
}
int dp[N], lst[N];
void init()
{
init_dsu();
vector<pair<int, int>> v;
v.push_back(MP(-1, 0));
for (int i = 1; i <= n; i++)
v.push_back(MP(a[i], i));
sort(all(v));
set<int> s;
s.insert(0);
s.insert(n + 1);
for (int i = 1; i <= n; i++)
{
int j = v[i].second;
auto it = s.lower_bound(j);
int r = *it;
it--;
int l = *it;
if (j - l <= d && l > 0)
merge(j, l);
if (r - j <= d && r <= n)
merge(j, r);
lst[j] = mn[get_par(j)];
s.insert(j);
}
}
int stress_solve()
{
int best = 0;
for (int mask = 0; mask < (1 << n); mask++)
{
vector<int> v;
for (int i = 0; i < n; i++)
{
if(((1 << i) & mask) == 0)
continue;
v.push_back(i + 1);
}
int mx = -mod;
int res = 0;
for (int i = 0; i < v.size(); i++)
{
if (a[v[i]] > mx)
{
mx = a[v[i]];
res++;
}
if (i && v[i] - v[i - 1] > d)
{
res = 0;
break;
}
}
if (res == 6)
res = res * 1;
best = max(best, res);
}
return best;
}
int t[4 * N];
void build(int v, int l, int r)
{
t[v] = 0;
if(l == r)
return;
int mid = (l + r) / 2;
build(2 * v, l, mid);
build(2 * v + 1, mid + 1, r);
}
void upd(int v, int l, int r, int pos, int val)
{
if (l == r)
{
t[v] = val;
return;
}
int mid = (l + r) / 2;
if(pos <= mid)
upd(2 * v, l, mid, pos, val);
else
upd(2 * v + 1, mid + 1, r, pos, val);
t[v] = max(t[2 * v], t[2 * v + 1]);
}
int get(int v, int l, int r, int i, int j)
{
if (i > j)
return 0;
if (l == i && r == j)
return t[v];
int mid = (l + r) / 2;
return max(get(2 * v, l, mid, i, min(j, mid)), get(2 * v + 1, mid + 1, r, max(i, mid + 1), j));
}
void solve()
{
cin >> n >> d;
for (int i = 1; i <= n; i++)
cin >> a[i];
init();
build(1, 1, n);
vector<pair<int, int> > v;
for (int i = 1; i <= n; i++)
v.push_back(MP(a[i], i));
sort(all(v));
reverse(all(v));
while (!v.empty())
{
int val = v.back().first;
vector<int> cur;
while (!v.empty() && v.back().first == val)
{
cur.push_back(v.back().second);
v.pop_back();
}
for (int j = 0; j < cur.size(); j++)
{
int i = cur[j];
dp[i] = get(1, 1, n, lst[i], i) + 1;
}
for (int j = 0; j < cur.size(); j++)
{
int i = cur[j];
upd(1, 1, n, i, dp[i]);
}
}
int ans = 1;
for(int i = 1; i <= n; i++)
ans = max(ans, dp[i]);
cout << ans << endl;
// cout << stress_solve() << endl;
}
int main()
{
fastIO;
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
/*
11 5
1 10 10 10 10 10 10 2 3 4 5
*/
Compilation message (stderr)
Main.cpp: In function 'int stress_solve()':
Main.cpp:121:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
Main.cpp: In function 'void solve()':
Main.cpp:203:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
203 | for (int j = 0; j < cur.size(); j++)
| ~~^~~~~~~~~~~~
Main.cpp:210:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
210 | for (int j = 0; j < cur.size(); j++)
| ~~^~~~~~~~~~~~
# | 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... |