// ~~ 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 "gen"
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);
}
int pos[N], used[N];
void init(void) {
cin >> n >> D;
FOR(i, 1, n) cin >> a[i], compress.pb(a[i]);
sort(all(compress));
FORR(i, n, 1) { // a[i] = a[j] but i < j is not considered
int p = lower_bound(all(compress), a[i]) - compress.begin();
used[p]++;
a[i] = p + used[p];
pos[a[i]] = i;
}
}
void process(void) {
set<int> obs;
obs.insert(0); obs.insert(inf);
FOR(i, 1, n) {
auto it = obs.lower_bound(pos[i]);
auto it2 = it; it2--;
if (*it2 > 0) update(*it2, pos[i] - *it2);
update(pos[i], *it - pos[i]);
last[pos[i]] = walk(1, 1, n, pos[i], n, D);
if (last[pos[i]] == -1) last[pos[i]] = n;
obs.insert(pos[i]);
}
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 << a[i] << ' '; cerr << '\n';
cout << *max_element(f + 1, f + n + 1);
cerr << *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;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | 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... |