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 int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 3e5 + 5;
int n, k;
vector<int> a(N), seg(N * 4), par(N), min_lol(N);
bool cmp(pair<int, int> x, pair<int, int> y) {
if(x.first == y.first) {
return x.second > y.second;
}
return x.first < y.first;
}
int Find(int x) {
if(x == par[x]) return x;
return par[x] = Find(par[x]);
}
void Unite(int x, int y) {
x = Find(x), y = Find(y);
min_lol[y] = min(min_lol[x], min_lol[y]);
par[x] = y;
}
int Query(int l, int r, int pos, int ql, int qr) {
if(ql <= l && r <= qr) {
return seg[pos];
}
if(ql > r || l > qr) {
return 0;
}
int mid = (l + r) >> 1;
return max(Query(l, mid, pos * 2, ql, qr), Query(mid + 1, r, pos * 2 + 1, ql, qr));
}
void Update(int l, int r, int pos, int qpos, int qval) {
if(l == r) {
seg[pos] = qval;
return;
}
int mid = (l + r) >> 1;
if(qpos <= mid) {
Update(l, mid, pos * 2, qpos, qval);
}
else {
Update(mid + 1, r, pos * 2 + 1, qpos, qval);
}
seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);
}
void Solve() {
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> a[i];
par[i] = i;
min_lol[i] = i;
}
map<int, int> mp;
for(int i = 1; i <= n; i++) {
mp[a[i]]++;
}
int cnt = 1;
for(auto i: mp) {
mp[i.first] = cnt++;
}
for(int i = 1; i <= n; i++) {
a[i] = mp[a[i]];
}
vector<pair<int, int> > temp;
for(int i = 1; i <= n; i++) {
temp.push_back({a[i], i});
}
sort(temp.begin(), temp.end(), cmp);
for(int i = 0; i < temp.size(); i++) {
//cout << temp[i].first << " " << temp[i].second << "\n";
for(int j = temp[i].second; j > max(0LL, temp[i].second - k); j--) {
if(Find(j) != Find(j - 1)) {
Unite(j - 1, j);
}
}
for(int j = temp[i].second; j < min(n, temp[i].second + k); j++) {
if(Find(j) != Find(j + 1)) {
Unite(j, j + 1);
}
}
int val = Query(1, n, 1, min_lol[temp[i].second], temp[i].second - 1);
Update(1, n, 1, temp[i].second, val + 1);
//cout << "val = " << val << "\n";
}
cout << seg[1] << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void Solve()':
Main.cpp:78:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 0; i < temp.size(); i++) {
| ~~^~~~~~~~~~~~~
# | 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... |