이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 3e5 + 100;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll block = 350;
ll nxt[N], a[N], dp[N], st[N][20], tmp[N];
ll n,d;
ll get(ll l, ll r){
ll p = 31 - __builtin_clz(r - l + 1);
return min(st[l][p], st[r - (1 << p) + 1][p]);
}
ll get_nxt(ll l, ll r){
ll p = 31 - __builtin_clz(r - l + 1);
return max(st[l][p], st[r - (1 << p) + 1][p]);
}
void build(){
for(int i = 1; i <= n;i++) st[i][0] = a[i];
for(int j = 1; j <= 19;j++){
for(int i = 1; i + (1 << j) <= n + 1;i++) st[i][j] = min(st[i][j-1], st[i + (1 << (j - 1))][j - 1]);
}
for(int i = 1; i <= n;i++) tmp[i] = (i + d > n ? inf : get(i, i + d));
for(int i = 1; i <= n;i++) st[i][0] = tmp[i];
for(int j = 1; j <= 19;j++){
for(int i = 1; i + (1 << j) <= n + 1;i++) st[i][j] = max(st[i][j-1], st[i + (1 << (j - 1))][j - 1]);
}
for(int i = 1; i + d <= n;i++){
ll l = i + d, r = n;
while(l <= r){
ll mid = (l + r) / 2;
if(get_nxt(i, mid - d) <= a[i]) nxt[i] = mid, l = mid + 1;
else r = mid - 1;
}
}
for(int i = 1; i <= n;i++) if(i + d > n) nxt[i] = n;
}
void to_thic_cau(){
cin >> n >> d;
for(int i = 1; i <= n;i++) cin >> a[i];
build();
ll res = 0;
for(int i = 1; i <= n;i++){
dp[i] = 1;
for(int j = 1; j <= i - 1;j++) if(a[i] > a[j] && nxt[j] >= i) dp[i] = max(dp[i], dp[j] + 1);
res = max(res, dp[i]);
}
cout << res << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
# | 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... |