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 ull unsigned long long
#define pii pair<long long, long long>
#define st first
#define nd second
#define file "test"
using namespace std;
const long long oo = 1e18;
const long long N = 2e5 + 5;
int n, m, h[2*N], dp[2*N], bit[2*N], SIZE;
vector <int> compress;
int find(int x){
int l = 0, r = compress.size() - 1, mid, ans = 0;
while (l <= r){
mid = l + r >> 1;
if (compress[mid] >= x)
ans = mid + 1, r = mid - 1;
else
l = mid + 1;
}
return ans;
}
void upd(int idx, int val){
while (idx > 0)
bit[idx] = min(bit[idx], val),
idx -= idx &-idx;
}
int get(int idx, int ans = 1e9){
while (idx <= SIZE)
ans = min(ans, bit[idx]),
idx += idx & -idx;
return ans;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen(file".inp","r",stdin); freopen(file".out","w",stdout);
// #endif
cin >> n >> m;
compress.push_back(0);
for (int i = 1; i <= n; i ++){
cin >> h[i];
compress.push_back(h[i] - i*m);
}
compress.push_back(-(n + 1)*m);
sort(compress.begin(), compress.end());
SIZE = compress.size();
for (int i = 0; i <= n + 1000; i ++)
dp[i] = bit[i] = 1e9;
dp[0] = 0;
upd(find(0), -1);
/*
for (int i = 1; i <= n + 1; i ++){
for (int j = i - 1; j >= 0; j --)
if (h[j] - j*m >= h[i] - i*m)
dp[i] = min(dp[i], dp[j] + i - j - 1);
// cout << dp[i] << ' ';
}
*/
for (int i = 1; i <= n + 1; i ++){
ll pos = find(h[i] - i*m), tmp = get(pos);
dp[i] = tmp + i;
upd(pos, dp[i] - i - 1);
}
cout << dp[n + 1];
return 0;
}
/** /\_/\
(= ._.)
/ >0 \>1
________________________
/ Brainstorming section /
/=======================/
---
===
with two fixed point j, i you are allowed to change every pole between j, i (j < i)
-> could it be changed so that you can go from j->i
possible case:
+ a[i] <= a[j]
+ h[j] + (i - j)*m >= h[i] <=> h[j] + i*m - j*m >= h[i] <=> h[j] - j*m >= h[i] - i*m
upd(h[i], dp[i] - i - 1)
get(h[j] - j*m)
===
**/
// Before submit: spot the visible bug by reading the code.
Compilation message (stderr)
triusis.cpp: In function 'int find(int)':
triusis.cpp:18:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
18 | mid = l + r >> 1;
| ~~^~~
# | 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... |