#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define clz __builtin_clzll
#define ctz __builtin_ctzll
#define popcount __builtin_popcount
#define lg(x) (63 - clz(x))
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(vector<X> &a) {
sort(all(a)); a.resize(unique(all(a)) - a.begin());
}
const ll oo = (ll) 1e18, inf = (ll) 1e9, mod = (ll) 1e9 + 7;
const int mxn = (int) 1e6 + 10, S = (int) 450, S2 = (int) 450, lg = (int) 18;
void add(ll &x, ll y) {
x += y;
if (x >= mod) x -= mod;
}
void sub(ll &x, ll y) {
x -= y;
if (x < 0) x += mod;
}
int n;
int a[mxn];
namespace one {
bool one() {
return n <= 1e3;
}
const int mxn = (int) 1e3 + 5;
int f[mxn];
vector<int> cur[mxn];
void solve() {
memset(f, 0x3f, sizeof f);
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j)
cur[i].push_back(a[j]);
compress(cur[i]);
}
for (int i = 1; i <= n; ++i) {
f[i] = f[i-1] + 1;
for (int j = i-1; j >= 1; --j) {
if (a[j] > a[j+1]) break;
if (a[i] == a[j]) {
minimize(f[i], f[j-1] + 1);
continue;
}
int cnt = upper_bound(all(cur[j-1]), a[i] - 1) - lower_bound(all(cur[j-1]), a[j] + 1);
if (cnt != 0) break;
minimize(f[i], f[j-1] + 1);
}
}
cout << f[n];
return;
}
}
namespace two {
bool two() {
return true;
}
struct PerSeg {
struct node {
int s;
int l, r;
node() {
s = 0;
l = r = -1;
}
};
int n;
vector<int> v;
vector<node> st;
PerSeg(int n) : n(n) {
create_node();
v = {0};
build(v[0], 1, n);
}
void create_node() {
st.push_back(node());
}
void build(int i, int l, int r) {
if (l == r) return;
int m = (l + r) >> 1;
if (st[i].l == -1) create_node(), st[i].l = sz(st) - 1;
if (st[i].r == -1) create_node(), st[i].r = sz(st) - 1;
build(st[i].l, l, m);
build(st[i].r, m+1, r);
return;
}
void upd(int p, int cur, int l, int r, int pos, int x) {
if (l > pos || r < pos) return;
if (l == r) {
st[cur].s = st[p].s + x;
return;
}
int m = (l + r) >> 1;
if (pos > m) {
st[cur].l = st[p].l;
if (st[cur].r == -1) create_node(), st[cur].r = sz(st) - 1;
upd(st[p].r, st[cur].r, m+1, r, pos, x);
}
else {
st[cur].r = st[p].r;
if (st[cur].l == -1) create_node(), st[cur].l = sz(st) - 1;
upd(st[p].l, st[cur].l, l, m, pos, x);
}
st[cur].s = st[st[cur].l].s + st[st[cur].r].s;
}
int upd(int pos, int x) {
create_node();
int root = sz(st) - 1;
upd(v.back(), root, 1, n, pos, x);
v.push_back(root);
return root;
}
int get(int cur, int l, int r, int u, int v) {
if (cur == -1) return 0;
if (l > v || r < u) return 0;
if (l >= u && r <= v) return st[cur].s;
int m = (l + r) >> 1;
int L = get(st[cur].l, l, m, u, v);
int R = get(st[cur].r, m + 1, r, u, v);
return L + R;
}
int get(int ver, int u, int v) {
if (u > v) return 0;
return get(ver, 1, n, u, v);
}
};
int f[mxn];
void solve() {
int mx = a[max_element(a + 1, a + 1 + n) - a];
PerSeg ps(mx);
vector<int> events(n + 1, 0);
for (int i = 1; i <= n; ++i) {
events[i] = ps.upd(a[i], 1);
}
memset(f, 0x3f, sizeof f);
f[0] = 0;
for (int i = 1, j = 1; i <= n; ++i) {
if (a[i] < a[i-1]) j = i;
while (j < i && ps.get(events[j-1], a[j] + 1, a[i] - 1) != 0)
j++;
minimize(f[i], f[j-1] + 1);
}
cout << f[n];
return;
}
}
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
{
// if (one::one()) return one::solve();
if (two::two()) return two::solve();
}
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define TASK "task"
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Compilation message
money.cpp: In function 'int main()':
money.cpp:219:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
219 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
money.cpp:220:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
220 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
17 ms |
29120 KB |
Output is correct |
8 |
Correct |
14 ms |
27984 KB |
Output is correct |
9 |
Correct |
12 ms |
29128 KB |
Output is correct |
10 |
Correct |
13 ms |
27824 KB |
Output is correct |
11 |
Correct |
12 ms |
29128 KB |
Output is correct |
12 |
Correct |
12 ms |
28360 KB |
Output is correct |
13 |
Correct |
12 ms |
27584 KB |
Output is correct |
14 |
Correct |
11 ms |
27640 KB |
Output is correct |
15 |
Correct |
15 ms |
28360 KB |
Output is correct |
16 |
Correct |
18 ms |
28544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
17 ms |
29120 KB |
Output is correct |
8 |
Correct |
14 ms |
27984 KB |
Output is correct |
9 |
Correct |
12 ms |
29128 KB |
Output is correct |
10 |
Correct |
13 ms |
27824 KB |
Output is correct |
11 |
Correct |
12 ms |
29128 KB |
Output is correct |
12 |
Correct |
12 ms |
28360 KB |
Output is correct |
13 |
Correct |
12 ms |
27584 KB |
Output is correct |
14 |
Correct |
11 ms |
27640 KB |
Output is correct |
15 |
Correct |
15 ms |
28360 KB |
Output is correct |
16 |
Correct |
18 ms |
28544 KB |
Output is correct |
17 |
Correct |
12 ms |
28100 KB |
Output is correct |
18 |
Correct |
12 ms |
29128 KB |
Output is correct |
19 |
Correct |
15 ms |
28616 KB |
Output is correct |
20 |
Correct |
13 ms |
28608 KB |
Output is correct |
21 |
Correct |
12 ms |
28872 KB |
Output is correct |
22 |
Correct |
15 ms |
28284 KB |
Output is correct |
23 |
Correct |
12 ms |
28932 KB |
Output is correct |
24 |
Correct |
11 ms |
27336 KB |
Output is correct |
25 |
Correct |
13 ms |
28360 KB |
Output is correct |
26 |
Correct |
15 ms |
29244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
17 ms |
29120 KB |
Output is correct |
8 |
Correct |
14 ms |
27984 KB |
Output is correct |
9 |
Correct |
12 ms |
29128 KB |
Output is correct |
10 |
Correct |
13 ms |
27824 KB |
Output is correct |
11 |
Correct |
12 ms |
29128 KB |
Output is correct |
12 |
Correct |
12 ms |
28360 KB |
Output is correct |
13 |
Correct |
12 ms |
27584 KB |
Output is correct |
14 |
Correct |
11 ms |
27640 KB |
Output is correct |
15 |
Correct |
15 ms |
28360 KB |
Output is correct |
16 |
Correct |
18 ms |
28544 KB |
Output is correct |
17 |
Correct |
12 ms |
28100 KB |
Output is correct |
18 |
Correct |
12 ms |
29128 KB |
Output is correct |
19 |
Correct |
15 ms |
28616 KB |
Output is correct |
20 |
Correct |
13 ms |
28608 KB |
Output is correct |
21 |
Correct |
12 ms |
28872 KB |
Output is correct |
22 |
Correct |
15 ms |
28284 KB |
Output is correct |
23 |
Correct |
12 ms |
28932 KB |
Output is correct |
24 |
Correct |
11 ms |
27336 KB |
Output is correct |
25 |
Correct |
13 ms |
28360 KB |
Output is correct |
26 |
Correct |
15 ms |
29244 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
18 ms |
27164 KB |
Output is correct |
33 |
Correct |
19 ms |
27336 KB |
Output is correct |
34 |
Correct |
14 ms |
28376 KB |
Output is correct |
35 |
Correct |
9 ms |
16844 KB |
Output is correct |
36 |
Correct |
14 ms |
27984 KB |
Output is correct |
37 |
Correct |
16 ms |
29128 KB |
Output is correct |
38 |
Correct |
15 ms |
29128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
17 ms |
29120 KB |
Output is correct |
8 |
Correct |
14 ms |
27984 KB |
Output is correct |
9 |
Correct |
12 ms |
29128 KB |
Output is correct |
10 |
Correct |
13 ms |
27824 KB |
Output is correct |
11 |
Correct |
12 ms |
29128 KB |
Output is correct |
12 |
Correct |
12 ms |
28360 KB |
Output is correct |
13 |
Correct |
12 ms |
27584 KB |
Output is correct |
14 |
Correct |
11 ms |
27640 KB |
Output is correct |
15 |
Correct |
15 ms |
28360 KB |
Output is correct |
16 |
Correct |
18 ms |
28544 KB |
Output is correct |
17 |
Correct |
12 ms |
28100 KB |
Output is correct |
18 |
Correct |
12 ms |
29128 KB |
Output is correct |
19 |
Correct |
15 ms |
28616 KB |
Output is correct |
20 |
Correct |
13 ms |
28608 KB |
Output is correct |
21 |
Correct |
12 ms |
28872 KB |
Output is correct |
22 |
Correct |
15 ms |
28284 KB |
Output is correct |
23 |
Correct |
12 ms |
28932 KB |
Output is correct |
24 |
Correct |
11 ms |
27336 KB |
Output is correct |
25 |
Correct |
13 ms |
28360 KB |
Output is correct |
26 |
Correct |
15 ms |
29244 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
18 ms |
27164 KB |
Output is correct |
33 |
Correct |
19 ms |
27336 KB |
Output is correct |
34 |
Correct |
14 ms |
28376 KB |
Output is correct |
35 |
Correct |
9 ms |
16844 KB |
Output is correct |
36 |
Correct |
14 ms |
27984 KB |
Output is correct |
37 |
Correct |
16 ms |
29128 KB |
Output is correct |
38 |
Correct |
15 ms |
29128 KB |
Output is correct |
39 |
Correct |
249 ms |
209696 KB |
Output is correct |
40 |
Runtime error |
291 ms |
262144 KB |
Execution killed with signal 9 |
41 |
Halted |
0 ms |
0 KB |
- |