이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
int n, d;
vector <int> a;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> d;
a.resize(n);
for(auto &i : a) cin >> i;
vector <pair <int, int> > all;
for(int i = 0 ; i < n ; i++){
all.push_back(make_pair(a[i], i));
}
sort(all.begin(), all.end(), [&](pair <int, int> &l, pair <int, int> &r){
return make_pair(l.first, -l.second) < make_pair(r.first, -r.second);
});
set <pair <int, int> > s;
vector <int> f(n);
for(auto &i : all){
int idx = i.second;
auto cur = make_pair(idx, idx);
auto it = s.lower_bound(make_pair(idx, -1));
vector <pair <int, int> > to_erase;
if(it != s.end() && idx + d >= it->first){
cur.second = it->second;
to_erase.push_back(*it);
}
if(it != s.begin()){
it--;
if(idx - d <= it->second){
cur.first = it->first;
cur.second = max(cur.second, it->second);
to_erase.push_back(*it);
}
}
for(auto &j : to_erase){
s.erase(j);
}
f[idx] = min(cur.second + d, n - 1);
s.insert(cur);
}
vector <int> dp(n);
sort(all.begin(), all.end(), [&](pair <int, int> &l, pair <int, int> &r){
return make_pair(-l.first, l.second) < make_pair(-r.first, r.second);
});
for(auto &i : all){
int idx = i.second;
dp[idx] = 1;
for(int j = idx + 1 ; j <= f[idx] ; j++){
dp[idx] = max(dp[idx], dp[j] + 1);
}
}
int ans = 0;
for(auto &i : dp) ans = max(ans, i);
cout << ans << endl;
}
# | 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... |