// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 3e5 + 5;
int n, D, a[N], f[N], last[N], node[N << 2];
vector<int> compress;
void update(int pos, int val) {
int id = 1, l = 1, r = n;
while(l < r) {
int mid = (l + r) >> 1;
if (pos > mid) id = (id << 1 | 1), l = mid + 1;
else id = (id << 1), r = mid;
}
node[id] = val;
while(id > 1) {
id >>= 1;
node[id] = max(node[id << 1], node[id << 1 | 1]);
}
}
int walk(int id, int l, int r, int u, int v, int k) {
if (l > v || r < u || node[id] < k) return -1;
if (l == r) return l;
int mid = (l + r) >> 1;
int ret = walk(id << 1, l, mid, u, v, k);
if (ret != -1) return ret;
return walk(id << 1 | 1, mid + 1, r, u, v, k);
}
vector<int> pos[N];
void init(void) {
cin >> n >> D;
FOR(i, 1, n) cin >> a[i], compress.pb(a[i]);
sort(all(compress));
compress.resize(unique(all(compress)) - compress.begin());
FOR(i, 1, n) a[i] = upper_bound(all(compress), a[i]) - compress.begin(), pos[a[i]].pb(i);
}
void process(void) {
set<int> obs;
obs.insert(0); obs.insert(inf);
FOR(i, 1, n) {
for(int j : pos[i]) {
auto it = obs.lower_bound(j);
auto it2 = it; it2--;
if (*it2 > 0) update(*it2, j - *it2);
update(j, *it - j);
}
for(int j : pos[i]) {
last[j] = walk(1, 1, n, i, n, D);
if (last[j] == -1) last[j] = n;
}
}
FOR(i, 1, n) {
maximize(f[i], 1);
FOR(j, i + 1, min(last[i] + D, n)) if (a[j] > a[i])
maximize(f[j], f[i] + 1);
}
// FOR(i, 1, n) cerr << f[i] << ' ' << last[i] << '\n';
cout << *max_element(f + 1, f + n + 1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |