Submission #1084225

# Submission time Handle Problem Language Result Execution time Memory
1084225 2024-09-05T16:23:47 Z anhthi Money (IZhO17_money) C++14
100 / 100
347 ms 30792 KB
#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 Seg {
        int n;
        vector<int> st;
        Seg(int n) : n(n) {
            st.resize(n << 2);
        }
        void upd(int p, int x) {
            int i = 1;
            for (int l = 1, r = n; l < r; ) {
                int m = (l + r) >> 1;
                if (p > m) {
                    l = m + 1;
                    i = 2 * i + 1;
                }
                else {
                    r = m;
                    i = 2 * i;
                }
            }
            st[i] += x;
            while (i > 1) {
                i >>= 1;
                st[i] = st[2 * i] + st[2 * i + 1];
            }
        }
        int get(int i, int l, int r, int u, int v) {
            if (l > v || r < u) return 0;
            if (l >= u && r <= v) return st[i];
            int m = (l + r) >> 1;
            int L = get(2 * i, l, m, u, v);
            int R = get(2 * i + 1, m + 1, r, u, v);
            return L + R;
        }
        int get(int u, int v) {
            if (u > v) return 0;
            return get(1, 1, n, u, v);
        }
    };
 
    int f[mxn];
 
    void solve() {
        int mx = a[max_element(a + 1, a + 1 + n) - a];
 
        Seg s(mx);
 
        memset(f, 0x3f, sizeof f);
        f[0] = 0; 
        for (int i = 1, j = 1; i <= n; ++i) {
            while (j < i && a[i] < a[i-1]) {
                s.upd(a[j], 1); j++;
            }
            while (j < i && s.get(a[j] + 1, a[i] - 1) != 0) {
                s.upd(a[j], 1); 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:176:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  176 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
money.cpp:177:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  177 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 2 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 2 ms 4700 KB Output is correct
7 Correct 6 ms 16220 KB Output is correct
8 Correct 8 ms 20060 KB Output is correct
9 Correct 8 ms 13660 KB Output is correct
10 Correct 7 ms 13660 KB Output is correct
11 Correct 7 ms 17752 KB Output is correct
12 Correct 6 ms 15708 KB Output is correct
13 Correct 6 ms 17244 KB Output is correct
14 Correct 4 ms 13404 KB Output is correct
15 Correct 10 ms 20316 KB Output is correct
16 Correct 9 ms 18608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 2 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 2 ms 4700 KB Output is correct
7 Correct 6 ms 16220 KB Output is correct
8 Correct 8 ms 20060 KB Output is correct
9 Correct 8 ms 13660 KB Output is correct
10 Correct 7 ms 13660 KB Output is correct
11 Correct 7 ms 17752 KB Output is correct
12 Correct 6 ms 15708 KB Output is correct
13 Correct 6 ms 17244 KB Output is correct
14 Correct 4 ms 13404 KB Output is correct
15 Correct 10 ms 20316 KB Output is correct
16 Correct 9 ms 18608 KB Output is correct
17 Correct 11 ms 19548 KB Output is correct
18 Correct 7 ms 17244 KB Output is correct
19 Correct 6 ms 17500 KB Output is correct
20 Correct 8 ms 20188 KB Output is correct
21 Correct 6 ms 15064 KB Output is correct
22 Correct 7 ms 19032 KB Output is correct
23 Correct 7 ms 17496 KB Output is correct
24 Correct 7 ms 16220 KB Output is correct
25 Correct 7 ms 18136 KB Output is correct
26 Correct 8 ms 19804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 2 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 2 ms 4700 KB Output is correct
7 Correct 6 ms 16220 KB Output is correct
8 Correct 8 ms 20060 KB Output is correct
9 Correct 8 ms 13660 KB Output is correct
10 Correct 7 ms 13660 KB Output is correct
11 Correct 7 ms 17752 KB Output is correct
12 Correct 6 ms 15708 KB Output is correct
13 Correct 6 ms 17244 KB Output is correct
14 Correct 4 ms 13404 KB Output is correct
15 Correct 10 ms 20316 KB Output is correct
16 Correct 9 ms 18608 KB Output is correct
17 Correct 11 ms 19548 KB Output is correct
18 Correct 7 ms 17244 KB Output is correct
19 Correct 6 ms 17500 KB Output is correct
20 Correct 8 ms 20188 KB Output is correct
21 Correct 6 ms 15064 KB Output is correct
22 Correct 7 ms 19032 KB Output is correct
23 Correct 7 ms 17496 KB Output is correct
24 Correct 7 ms 16220 KB Output is correct
25 Correct 7 ms 18136 KB Output is correct
26 Correct 8 ms 19804 KB Output is correct
27 Correct 2 ms 4700 KB Output is correct
28 Correct 2 ms 4700 KB Output is correct
29 Correct 1 ms 4700 KB Output is correct
30 Correct 2 ms 4700 KB Output is correct
31 Correct 1 ms 4700 KB Output is correct
32 Correct 7 ms 18668 KB Output is correct
33 Correct 6 ms 15196 KB Output is correct
34 Correct 7 ms 19804 KB Output is correct
35 Correct 4 ms 12124 KB Output is correct
36 Correct 7 ms 17240 KB Output is correct
37 Correct 7 ms 20160 KB Output is correct
38 Correct 12 ms 20464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 2 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 2 ms 4700 KB Output is correct
7 Correct 6 ms 16220 KB Output is correct
8 Correct 8 ms 20060 KB Output is correct
9 Correct 8 ms 13660 KB Output is correct
10 Correct 7 ms 13660 KB Output is correct
11 Correct 7 ms 17752 KB Output is correct
12 Correct 6 ms 15708 KB Output is correct
13 Correct 6 ms 17244 KB Output is correct
14 Correct 4 ms 13404 KB Output is correct
15 Correct 10 ms 20316 KB Output is correct
16 Correct 9 ms 18608 KB Output is correct
17 Correct 11 ms 19548 KB Output is correct
18 Correct 7 ms 17244 KB Output is correct
19 Correct 6 ms 17500 KB Output is correct
20 Correct 8 ms 20188 KB Output is correct
21 Correct 6 ms 15064 KB Output is correct
22 Correct 7 ms 19032 KB Output is correct
23 Correct 7 ms 17496 KB Output is correct
24 Correct 7 ms 16220 KB Output is correct
25 Correct 7 ms 18136 KB Output is correct
26 Correct 8 ms 19804 KB Output is correct
27 Correct 2 ms 4700 KB Output is correct
28 Correct 2 ms 4700 KB Output is correct
29 Correct 1 ms 4700 KB Output is correct
30 Correct 2 ms 4700 KB Output is correct
31 Correct 1 ms 4700 KB Output is correct
32 Correct 7 ms 18668 KB Output is correct
33 Correct 6 ms 15196 KB Output is correct
34 Correct 7 ms 19804 KB Output is correct
35 Correct 4 ms 12124 KB Output is correct
36 Correct 7 ms 17240 KB Output is correct
37 Correct 7 ms 20160 KB Output is correct
38 Correct 12 ms 20464 KB Output is correct
39 Correct 93 ms 22272 KB Output is correct
40 Correct 149 ms 26708 KB Output is correct
41 Correct 74 ms 21072 KB Output is correct
42 Correct 72 ms 22844 KB Output is correct
43 Correct 46 ms 21084 KB Output is correct
44 Correct 176 ms 29012 KB Output is correct
45 Correct 177 ms 28752 KB Output is correct
46 Correct 177 ms 30012 KB Output is correct
47 Correct 193 ms 29776 KB Output is correct
48 Correct 195 ms 27672 KB Output is correct
49 Correct 256 ms 30544 KB Output is correct
50 Correct 248 ms 30548 KB Output is correct
51 Correct 284 ms 30548 KB Output is correct
52 Correct 257 ms 30544 KB Output is correct
53 Correct 258 ms 30604 KB Output is correct
54 Correct 262 ms 30500 KB Output is correct
55 Correct 313 ms 30648 KB Output is correct
56 Correct 324 ms 30792 KB Output is correct
57 Correct 314 ms 30472 KB Output is correct
58 Correct 347 ms 30548 KB Output is correct
59 Correct 339 ms 30520 KB Output is correct
60 Correct 325 ms 30544 KB Output is correct
61 Correct 314 ms 30544 KB Output is correct
62 Correct 346 ms 30544 KB Output is correct