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 file ""
#define all(x) x.begin(), x.end()
#define sc second
#define fr first
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll inf = 1e18 + 5;
const ll mod = 1e9 + 7;
const int N = 1e5 + 5;
int dx[] = {+1, 0, -1, 0};
int dy[] = {0, +1, 0, -1};
int n;
int a[N], l[N];
vector<int> t(N * 4, 1e9);
vector<int> dp(N + 5);
void update(int u, int l, int r, int pos, int val) {
if (l == r) {
t[u] = val;
return;
}
int m = l + r >> 1;
if (pos <= m) update(u + u, l, m, pos, val);
else update (u + u + 1, m + 1, r, pos, val);
t[u] = min(t[u + u], t[u + u + 1]);
}
int get(int u, int ul, int ur, int l, int r) {
if (l > ur || ul > r) return 1e9;
if (l <= ul && ur <= r) return t[u];
int um = ul + ur >> 1;
return min(get(u + u, ul, um, l, r), get(u + u + 1, um + 1, ur, l, r));
}
int check(int lft, int rgt, int mx, int mn) {
int pos = -1;
int k = rgt;
while (lft <= rgt) {
int m = lft + rgt >> 1;
if (a[m] == mn) pos = m, lft = m +1;
if (a[m] > mn) rgt = m - 1;
if (a[m] < mn) pos = m, lft = m + 1;
}
if (a[pos] <= mn && a[pos] <= mx)
return dp[rgt];
else return 1e9;
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
srand(time(nullptr));
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
l[1] = 1;
int last = 1;
for (int i = 2; i <= n; i++) {
if (a[i] >= a[i - 1]) continue;
else {
for (int p = last; p <= i - 1; p++)
l[p] = last;
last = i;
}
}
for (int p = last; p <= n; p++) l[p] = last;
update(1, 1, n, 1, 0);
dp[1] = 1;
for (int i = 2; i <= n; i++) {
update(1, 1, n, i, dp[i - 1]);
dp[i] = get(1, 1, n, l[i], i) + 1;
if (l[i] == 1) {
dp[i] = 1;
continue;
}
//dp[i] = min(dp[i], check(l[l[i] - 1], l[i] - 1, a[i], a[l[i]]));
}
cout << dp[n];
return 0;
}
Compilation message (stderr)
money.cpp: In function 'void update(int, int, int, int, int)':
money.cpp:36:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int m = l + r >> 1;
| ~~^~~
money.cpp: In function 'int get(int, int, int, int, int)':
money.cpp:45:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int um = ul + ur >> 1;
| ~~~^~~~
money.cpp: In function 'int check(int, int, int, int)':
money.cpp:53:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int m = lft + rgt >> 1;
| ~~~~^~~~~
money.cpp:51:6: warning: unused variable 'k' [-Wunused-variable]
51 | int k = rgt;
| ^
# | 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... |