#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define pi pair<int, int>
#define pl pair<ll,ll>
#define all(x) (x).begin(),(x).end()
vi a;
int n,d,t;
struct segTree {
struct node {
long double mn=1e9;
long double lzy=0;
int cnt=0;
node(long double _mn=1e9): mn(_mn) {}
};
int sze;
array<node,75000*4> nodes;
node unite(const node& a, const node& b) {
if (a.mn<=b.mn) {
return a;
}
return b;
}
void push(int v, int tl, int tr) {
nodes[v].mn+=nodes[v].lzy;
if (tl!=tr) {
nodes[2*v].lzy+=nodes[v].lzy;
nodes[2*v+1].lzy+=nodes[v].lzy;
}
nodes[v].lzy=0;
}
segTree(int n=1): sze(n) {}
node get(int l, int r, int v, int tl, int tr) {
push(v,tl,tr);
if (l<=tl && tr<=r) {
return nodes[v];
}
if (tr<l || r<tl) {
return node();
}
int tm=tl+(tr-tl)/2;
return unite(get(l,r,2*v,tl,tm),get(l,r,2*v+1,tm+1,tr));
}
node get(int l, int r) {
return get(l,r,1,0,sze-1);
}
void update1(int l, int r, long double add, int v, int tl, int tr) {
push(v,tl,tr);
if (l<=tl && tr<=r) {
nodes[v].lzy+=add;
push(v,tl,tr);
return;
}
if (tr<l || r<tl) {
return;
}
int tm=tl+(tr-tl)/2;
update1(l,r,add,2*v,tl,tm);
update1(l,r,add,2*v+1,tm+1,tr);
nodes[v]=unite(nodes[2*v],nodes[2*v+1]);
}
void update1(int l, int r, long double add) {
update1(l,r,add,1,0,sze-1);
}
void update2(int ind, const node& val, int v, int tl, int tr) {
push(v,tl,tr);
if (tr<ind || ind<tl) {
return;
}
if (tl==tr) {
nodes[v]=unite(val,nodes[v]);
return;
}
int tm=tl+(tr-tl)/2;
update2(ind,val,2*v,tl,tm);
update2(ind,val,2*v+1,tm+1,tr);
nodes[v]=unite(nodes[2*v],nodes[2*v+1]);
}
void update2(int ind, const node& val) {
update2(ind,val,1,0,sze-1);
}
};
segTree dat;
auto solve(long double pri) {
fill(all(dat.nodes),1e9);
dat.update2(0,0);
for (int i=0; i<n; i++) {
if (a[i]>t) {
auto temp=dat.get(0,n);
temp.cnt++;
temp.mn+=pri;
dat.update2(0,temp);
dat.update1(i+1,n,1);
}
else {
dat.update2(min(n,i+t-a[i]+1),dat.get(0,min(n,i+t-a[i]+1)-1));
dat.update1(0,min(n,i+t-a[i]+1)-1,1e9);
}
}
return dat.get(0,n);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> d >> t;
dat.sze=n+1;
a.resize(n);
int cnt=0;
int dd=0;
for (int i=0; i<n; i++) {
cin >> a[i];
if (a[i]<=t) {
cnt++;
}
if (i>0 && a[i]>t && a[i-1]<=t) {
dd++;
}
}
d=min(dd,d);
long double l=0,r=n;
for (int i=0; i<40; i++) {
long double m=(l+r)/2;
auto temp=solve(m);
if (temp.cnt>d) {
l=m;
}
else {
r=m;
}
}
auto ans=solve((l+r)/2);
cout << cnt+round(ans.mn-(l+r)/2*d) << '\n';
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... |