이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
#pragma GCC target("avx2,fma")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x
constexpr ll inf = 1e18, N = 1e6 + 10, mod = 1e9 + 7, pr = 1000696969;
int n, d, a[N], l[N], r[N], dp[N];
set<int> bad, to;
vector<pair<int, int> > vc;
bool cmp(pair<int, int> p1, pair<int, int> p2) {
if(p1.first != p2.first) return p1.first < p2.first;
return p1.second > p2.second;
}
struct node {
int val;
} x[N << 2];
void change(int l, int r, int ind, int val, int v) {
if(r - l < 2) {
x[v].val = val;
return;
}
int mid = (l + r) >> 1;
if(ind < mid) change(l, mid, ind, val, 2 * v);
else change(mid, r, ind, val, 2 * v + 1);
x[v].val = max(x[2 * v].val, x[2 * v + 1].val);
}
int get(int l, int r, int s, int e, int v) {
if(r <= s || l >= e) return 0;
if(l >= s && r <= e) return x[v].val;
int mid = (l + r) >> 1;
return max(get(l, mid, s, e, 2 * v), get(mid, r, s, e, 2 * v + 1));
}
int main() {
ios :: sync_with_stdio(0), cin.tie(0);
cin >>n >>d;
for(int i = 0; i < n; i++) cin >>a[i], vc.push_back({a[i], i});
sort(All(vc), cmp);
for(int i = 0; i < n; i++) l[i] = -1, r[i] = n;
for(auto i : vc) {
int ind = i.second;
auto it = to.lower_bound(ind);
if(it != to.end()) {
l[*it] = ind, r[ind] = *it;
if(*it - ind <= d) {
bad.erase(*it);
}
}
if(it != to.begin()) {
it--;
l[ind] = *it, r[*it] = ind;
}
to.insert(ind);
if(ind - l[ind] > d) bad.insert(ind);
dp[ind] = 1;
if(ind != 0) {
auto pos = bad.upper_bound(ind);
if(pos == bad.begin()) {
dp[ind] += get(0, n, 0, ind, 1);
}
else {
pos--;
int j = *pos;
if(j != ind)
dp[ind] += get(0, n, j, ind, 1);
}
}
change(0, n, ind, dp[ind], 1);
}
int ans = 0;
for(int i = 0; i < n; i++) ans = max(ans, dp[i]);
cout<<ans;
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... |