This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
using namespace std ;
const int N = (1 << 19) ;
int n, d, cnt, a[N + 1], dp[N + 1], mx[2 * N + 1] ;
set<int> s ;
map<int, int> mp ;
void update(int l, int r, int ind, int num, int v)
{
if(l > ind || r < ind)
return ;
if(l == r)
{
mx[v] = num ;
return ;
}
int mid = (l + r) >> 1 ;
update(l, mid, ind, num, v * 2) ;
update(mid + 1, r, ind, num, v * 2 + 1) ;
mx[v] = max(mx[v * 2], mx[v * 2 + 1]) ;
}
int get_max(int l, int r,int l1, int r1, int v)
{
if(l > r1 || r < l1)
return 0 ;
if(l1 <= l && r <= r1)
return mx[v] ;
int mid = (l + r) >> 1 ;
return max(get_max(l, mid, l1, r1, v * 2), get_max(mid + 1, r, l1, r1, v * 2 + 1)) ;
}
signed main()
{
ios_base::sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
cin >> n >> d ;
for(int i = 1 ; i <= n ; i++)
{
cin >> a[i] ;
s.insert(a[i]) ;
}
for(int i : s)
{
cnt++ ;
mp[i] = cnt ;
}
for(int i = 1 ; i <= n ; i++)
a[i] = mp[a[i]] ;
if(n <= 7000)
{
int ans = 0 ;
for(int i = n ; i >= 1 ; i--)
{
int mx = 0, now = a[i], mn = 1e9 ;
for(int j = i + 1 ; j <= n ; j++)
{
mn = min(mn, a[j]) ;
if(a[j] > now)
mx = max(mx, dp[j]) ;
if((j - i) % d == 0)
{
now = max(now, mn) ;
mn = 1e9 ;
}
}
dp[i] = mx + 1 ;
ans = max(ans, dp[i]) ;
}
// for(int i = 1 ; i <= n ; i++)
// cout << dp[i] << ' ' ;
cout << ans ;
return 0 ;
}
if(n <= 20)
{
int ans = 0 ;
for(int i = 0 ; i < (1 << n) ; i++)
{
int now = 0, mx = -1, ls = 1e9 ;
for(int j = 0 ; j < n ; j++)
if(j - ls <= d && ((1 << j) & i))
{
ls = j ;
if(mx < a[j + 1])
now++ ;
mx = max(mx, a[j + 1]) ;
}
ans = max(ans, now) ;
}
cout << ans ;
return 0 ;
}
if(d == 1)
{
int ans = 0 ;
for(int i = 1 ; i <= n ; i++)
update(1, N, i, a[i], 1) ;
for(int i = n ; i >= 1 ; i--)
{
int l = i, r = n + 1 ;
while(l + 1 < r)
{
int mid = (l + r) >> 1 ;
if(get_max(1, N, i, mid, 1) > a[i])
r = mid ;
else
l = mid ;
}
dp[i] = dp[r] + 1 ;
ans = max(ans, dp[i]) ;
}
cout << ans ;
return 0 ;
}
if(d == n)
{
int ans = 0 ;
for(int i = 1 ; i <= n ; i++)
{
int num = get_max(1, N, 1, a[i] - 1, 1) ;
ans = max(ans, num + 1) ;
update(1, N, a[i], num + 1, 1) ;
}
cout << ans ;
return 0 ;
}
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... |