제출 #1354158

#제출 시각아이디문제언어결과실행 시간메모리
1354158AtinaRRabbit Carrot (LMIO19_triusis)C++20
0 / 100
4 ms836 KiB
#include <bits/stdc++.h>

using namespace std;
const int MAX=2*1e5+10;
const int INF=(1<<30);
int n,m;
int arr[MAX];
vector<int> v;
set<int> s;
map<int,int> comp;
int tree[4*MAX];
void merge(int &node, int L, int R)
{
    node=max(L,R);
}
void build(int node, int L, int R)
{
    if(L==R)
    {
        tree[node]=0;
        return;
    }
    else
    {
        int mid=(L+R)/2;
        build(2*node,L,mid);
        build(2*node+1,mid+1,R);
        merge(tree[node],tree[2*node],tree[2*node+1]);
    }
}
void update(int node, int L, int R, int index, int val)
{
    if(L==R)
    {
        tree[node]=max(tree[node],val);
        return;
    }
    int mid=(L+R)/2;
    if(index>mid)
    {
        update(2*node+1,mid+1,R,index,val);
    }
    else
    {
        update(2*node,L,mid,index,val);
    }
    merge(tree[node],tree[2*node],tree[2*node+1]);
}
int query(int node, int L, int R, int _left, int _right)
{
    if(L>=_left && R<=_right)return tree[node];
    if(L>_right || R<_left)return 0;
    int mid=(L+R)/2;
    int l=query(2*node,L,mid,_left,_right);
    int r=query(2*node+1,mid+1,R,_left,_right);
    return max(l,r);
}
int main()
{
    cin>>n>>m;
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
        s.insert(arr[i]);
    }
    int idx=0;
    vector<int> bs;
    for(auto x:s)
    {
        comp[x]=idx;
        bs.push_back(x);
        idx++;
    }
    for(int i=0; i<n; i++)v.push_back(comp[arr[i]]);///coordinate compression of values
    int diff=comp.size();
    build(1,0,diff-1);
    for(int i=0; i<n; i++)
    {
        ///for the current height x, we can jump to it from every y which is bigger than x
        /// or we can jump from at least x-m
        ///so we need the max value on the range where the first comp[x] starts to the end of the array
        ///or the range where the first comp[x-m] occurs to the last comp[x], so we run the seg tree then update at the curr el
        int LB=lower_bound(bs.begin(),bs.end(),arr[i]-m)-bs.begin();
        int best=query(1,0,diff-1,LB,diff-1);
        int dp=0;
        if(best>0)dp=best+1;
        if (arr[i] <= m) dp = max(dp, 1);
        update(1,0,diff-1,comp[arr[i]],dp);
    }
    cout<<n-query(1,0,diff-1,0,diff-1)<<endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...