#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define ss second
#define ff first
#define piii pair<int,pii>
#define debu(x) (cerr << #x << " = "<< x << "\n")
#define debu2(x,y) (cerr << #x << " = "<< x << " " << #y << " = " << y << "\n")
#define debu3(x,y,z) (cerr << #x << " = "<< x << " " << #y << " = " << y << " " << #z << " = " << z<< "\n")
#define bitout(x,y) {\
cerr << #x << " : ";\
for (int justforbits = y; justforbits >=0; justforbits--)cout << (((1 << justforbits) & x)>=1);\
cout << "\n";\
}
#define rangeout(j,rangestart,rangeend) {\
cerr << "outputting" << #j<< ":\n";\
for (int forrang = rangestart; forrang <= rangeend; forrang++)cerr << j[forrang] << " ";\
cerr<<"\n";\
}
#define c1 {cerr << "Checkpoint 1! \n\n";cerr.flush();}
#define c2 {cerr << "Checkpoint 2! \n\n";cerr.flush();}
#define c3 {cerr << "Checkpoint 3! \n\n";cerr.flush();}
#define c4 {cerr << "Checkpoint 4! \n\n";cerr.flush();}
signed main()
{
int t1,t2,t3,t4;
mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,d,t;cin>>n>>d>>t;
priority_queue<piii,vector<piii>,greater<piii>>pq;
for(int a=1;a<=n;a++)
{
cin>>t1;
if(t1<=t)pq.push(mp(a,mp(a+(t-t1),a)));
}
set<int>curr;
priority_queue<pii,vector<pii>,greater<pii>>ends;
vector<int>foreach(n+1,0);
int tots=0;
for(int a=1;a<=n;a++)
{
while(!pq.empty()&&pq.top().ff<=a)
{
curr.insert(pq.top().ss.ss);
ends.push(pq.top().ss);
pq.pop();
}
while(!ends.empty()&&ends.top().ff<a)
{
curr.erase(ends.top().ss);
ends.pop();
}
if(!curr.empty())
{
tots++;
foreach[a]=(*prev(curr.end()));
}
}
//rangeout(foreach,1,n);
//new
vector<vector<int>>dp(n+5,vector<int>(d+1,tots));
vector<priority_queue<int>>starteach(n+5);
//sets -> a collection of the things I have
for(int a=n;a>=1;a--)//up to (inclusive)
{
if(foreach[a]!=0)for(int b=n+4;b>=a;b--){starteach[b].push(foreach[a]);}
for(int b=1;b<=d;b++)//number of mattresses
{
for(int c=((n+1)-(b-1));c>=a+1;c--)//prior number -> at least must have b-1 people
{
//debu3(a,b,c);
while(!starteach[c-1].empty()&&starteach[c-1].top()>=a)
{
starteach[c-1].pop();
}
dp[a][b]=min(dp[a][b],dp[c][b-1]-(int)starteach[c-1].size());
//debu(dp[a][b]);
}
}
}
int ans=n;
for(int a=1;a<=n;a++)
{
ans=min(dp[a][d],ans);
}
cout<<ans;
//place it to my left
}
//dp[x][y]=max(dp[i][y-1]+ifputthere); for each i
//-> each if put there -= those who are after i
//between last cut and current cut with numbers smaller than me
//^^store each one ending at x -> then when something pops up that is more then add it
//current cut has to be made at where I am right now
//dp[current at][cutnumber]=for each i at which the previous cut was made:
//dp[i][cutnumber-1]+no of things between i and current at whose endings are > current at
//(including current at into my current segment)
//a set ending at each person ->
//-> number above me is lower_bound it
//speed up?? -> do we need to find all i? (i fhv time try)
//only ever gets increasingly bigger -> just pop out while is bigger than
# | 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... |