#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll
using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
/*
Competitive Programming notes that I need to study & fix my dumbass self:
1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you
2. Stress:
- Always try generating big testcases and try if they run
3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time
Time management schedule:
Offline / LAH days (4 problems - 3h):
15' thinking of solution / idea
1. no idea: skip
2. yes idea: continue thinking for <= 15'
+ implementing: <= 20'
+ brute-force: <= 5'
+ test generator: <= 5'
I hate offline because I am dumb
*/
typedef pair<int, int> ii;
const int N = 3e5+5;
const int B = 750;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;}
struct node {
int mx = 0, st = 0, ed = 0, cnt = 0, len = 0;
node(){}
node(int mx, int st, int ed, int cnt, int len): mx(mx), st(st), ed(ed), cnt(cnt), len(len) {}
};
node st[4*N];
node merge(node a, node b)
{
node c;
c.st = a.st;
if (a.cnt == a.len) c.st = max(c.st, a.cnt + b.st);
c.ed = b.ed;
if (b.cnt == b.len) c.ed = max(c.ed, b.cnt + a.ed);
c.mx = max({a.mx, b.mx, a.ed + b.st, c.st, c.ed});
c.cnt = a.cnt + b.cnt;
c.len = a.len + b.len;
return c;
}
void buildst(int id, int l, int r)
{
if (l == r)
{
st[id] = node(0,0,0,0,1);
return;
}
int mid = l+r>>1;
buildst(id*2, l, mid); buildst(id*2+1, mid+1, r);
st[id] = merge(st[id*2], st[id*2+1]);
}
void turn(int id, int l, int r, int i)
{
if (l > i || r < i) return;
if (l == r)
{
st[id] = node(1,1,1,1,1);
return;
}
int mid = l+r>>1;
turn(id*2, l, mid, i); turn(id*2+1, mid+1, r, i);
st[id] = merge(st[id*2], st[id*2+1]);
}
node getmax(int id, int l, int r, int u, int v)
{
if (l > v || r < u) return node(0,0,0,0,0);
if (u <= l && r <= v) return st[id];
int mid = l+r>>1;
return merge(getmax(id*2, l, mid, u, v), getmax(id*2+1, mid+1, r, u, v));
}
int stmx[4*N];
void build(int id, int l, int r)
{
if (l == r)
{
stmx[id] = -inf;
return;
}
int mid = l+r>>1;
build(id*2, l, mid); build(id*2+1, mid+1, r);
stmx[id] = -inf;
}
void update(int id, int l, int r, int i, int val)
{
if (l > i || r < i) return;
if (l == r)
{
stmx[id] = val;
return;
}
int mid = l+r>>1;
update(id*2, l, mid, i, val); update(id*2+1, mid+1, r, i, val);
stmx[id] = max(stmx[id*2], stmx[id*2+1]);
}
int get(int id, int l, int r, int u, int v)
{
if (v < u) return -inf;
if (l > v || r < u) return -inf;
if (u <= l && r <= v) return stmx[id];
int mid = l+r>>1;
return max(get(id*2, l, mid, u, v), get(id*2+1, mid+1, r, u, v));
}
int n,d;
int a[N], dp[N], ri[N];
bool cmp(int i, int j)
{
if (a[i] == a[j]) return i > j;
return a[i] < a[j];
}
bool cmp1(int i, int j)
{
if (a[i] == a[j]) return i < j;
return a[i] > a[j];
}
void solve()
{
cin>>n>>d;
vector<int> ind;
for (int i = 1; i <= n; i++)
{
cin>>a[i];
ind.pb(i);
}
sort(all(ind), cmp);
// for (auto i : ind) cout<<i<<" ";
// cout<<endl;
int ans = 0;
buildst(1,1,n);
for (int i = n-1; i >= 0; i--)
{
int pos = ind[i];
int l = pos+1, r = n, res = -1;
while (l <= r)
{
int mid = l+r>>1;
node ret = getmax(1,1,n,pos+1,mid);
if (ret.mx < d) l = mid+1;
else r = mid-1, res = mid;
}
// cout<<pos<<endl;
// for (int j = pos+1; j <= n; j++) cout<<getmax(1,1,n,pos+1,j).mx<<" ";
// cout<<endl;
// cout<<res<<endl;
if (res == -1) ri[pos] = n;
else ri[pos] = res-getmax(1,1,n,pos+1,res).mx+d;
turn(1,1,n,pos);
}
sort(all(ind), cmp1);
for (int i = 0; i < n; i++)
{
int pos = ind[i];
dp[pos] = max(get(1,1,n,pos+1,ri[pos]), 0ll) + 1;
// cout<<pos<<" "<<ri[pos]<<" "<<dp[pos]<<endl;
ans = max(ans, dp[pos]);
update(1,1,n,pos,dp[pos]);
}
cout<<ans;
// for (int i = 1; i <= n; i++) cout<<ri[i]<<" ";
// cout<<endl;
}
/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/
signed main()
{
bruh
//freopen("input.inp","r",stdin);
//freopen("output.inp","w",stdout);
int t = 1;
// cin>>t;
while (t--)
{
solve();
cout<<"\n";
}
}
# | 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... |