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 ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(998244353)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
struct fen{
vector<int> tree;
int n;
fen(int _n = 0){
n = _n;
tree.resize(n + 3, INF);
}
void upd(int p, int c){
while(p <= n){
minimize(tree[p], c);
p += p & -p;
}
}
int get(int p){
int ans = INF;
while(p){
minimize(ans, tree[p]);
p -= p & - p;
}
return ans;
}
};
const int MAX = 2e5 + 10;
int N, M;
int h[MAX], dp[MAX];
void solve(){
cin >> N >> M;
for(int i = 1; i <= N; i++) cin >> h[i];
vector<ll> tem;
for(int i = 0; i <= N; i++){
tem.push_back(1LL * M * i - h[i]);
}
cps(tem);
fen bit(sz(tem));
auto pos = [&](ll k) -> int{
return lower_bound(ALL(tem), k) - tem.begin() + 1;
};
bit.upd(pos(-M), 0);
for(int i = 1; i <= N; i++){
int p = pos(1LL * M * i - h[i]);
dp[i] = i - 1 + bit.get(p);
bit.upd(p, dp[i] - i);
}
int ans = dp[N];
for(int i = N; i >= 1; i--){
minimize(ans, dp[i - 1] + (N - i + 1));
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("cnt-mst.inp","r",stdin);
// freopen("cnt-mst.out","w",stdout);
int t = 1;
// cin >> t;
while(t--)
solve();
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... |