This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
using namespace std;
const int N = 3e5 + 1;
int a[N + 1], b[N + 1], dp[N + 1], pos[N + 1], d, n;
vector<int> f[N + 1];
set<int> s;
pair<int, int> p[N + 1];
struct SegmentTree
{
int tree[4 * N + 1];
void upd(int v, int TreeL, int TreeR, int pos, int val)
{
if (TreeL == TreeR)
{
tree[v] = val;
return ;
}
int mid = (TreeL + TreeR)/2;
if (pos <= mid)
{
upd(v * 2, TreeL, mid, pos, val);
}
else
{
upd(v * 2 + 1, mid + 1, TreeR, pos, val);
}
tree[v] = max(tree[2 * v], tree[2 * v + 1]);
}
int get(int v, int TreeL, int TreeR, int L, int R)
{
if (L > R)
{
return 0;
}
if (TreeL == L && TreeR == R)
{
return tree[v];
}
int mid = (TreeL + TreeR)/2;
return max(get(v * 2, TreeL, mid, L, min(R, mid)), get(v * 2 + 1, mid + 1, TreeR, max(L, mid + 1), R));
}
} tree;
void read()
{
cin >> n >> d;
for (int i = 1; i <= n; ++ i)
{
cin >> a[i];
b[i] = a[i];
s.insert(i);
p[i] = {a[i], i};
}
sort(b + 1, b + n + 1);
sort(p + 1, p + n + 1);
for (int i = 1; i <= n; ++ i)
{
a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
f[a[i]].push_back(i);
pos[p[i].second] = i;
}
}
vector<int> del[N + 1];
void solve()
{
for (int i = 1; i <= n; ++ i)
{
while(!f[i].empty())
{
int j = f[i].back();
f[i].pop_back();
while(true)
{
auto it = s.lower_bound(j);
if (it == s.end() || *it > j + d)
{
break;
}
s.erase(it);
}
auto it = s.lower_bound(j);
int nxt = (it == s.end() ? n + 1 : *it);
del[nxt].push_back(j);
}
}
int res = 0;
for (int i = 1; i <= n; ++ i)
{
for (int j : del[i])
{
tree.upd(1, 1, n, pos[j], 0);
}
dp[i] = tree.get(1, 1, n, 1, a[i] - 1) + 1;
tree.upd(1, 1, n, pos[i], dp[i]);
res = max(res, dp[i]);
}
cout << res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |