Submission #879447

# Submission time Handle Problem Language Result Execution time Memory
879447 2023-11-27T13:01:58 Z kh0i Discharging (NOI20_discharging) C++17
100 / 100
143 ms 36580 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif

using ll = long long;
using pii = pair<int, int>;

#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
    assert(l <= r);
    return uniform_int_distribution<ll> (l, r)(rng);
}

const int N = 1e6 + 3;

#pragma once

struct Node{
    ll val;

    const Node operator + (const Node &other){
        Node res;
        res.val = max(val, other.val);
        return res;
    }
};

struct ST{
    int n;
    vector<Node> st;

    Node def = Node(); // default value

    void init(int _n){
        n = _n;
        st.resize(4 * n + 6, def);
    }

    void _update(int id, int l, int r, int pos, ll val){
        if(l == r){
            st[id].val = val;
            return;
        }

        int m = (l + r) / 2;
        if(l <= pos and pos <= m)
            _update(2 * id, l, m, pos, val);
        else
            _update(2 * id + 1, m + 1, r, pos, val);

        st[id] = st[2 * id] + st[2 * id + 1];
    }

    Node _query(int id, int l, int r, int tl, int tr){
        if(l > tr or tl > r)
            return def;
        if(tl <= l and r <= tr)
            return st[id];
        int m = (l + r) / 2;

        return _query(2 * id,  l, m, tl, tr) + _query(2 * id + 1, m + 1, r, tl, tr);
    }

    Node query(int l, int r){
        return _query(1, 1, n, l, r);
    }

    void update(int pos, ll val){
        _update(1, 1, n, pos, val);
    }
};

int n;
ll a[N], f[N];

namespace Sub14{
    bool valid_input(){
        return n <= 1500;
    }

    void solve(){
        ST dak;
        dak.init(n);
        for(int i = 1; i <= n; ++i)
            dak.update(i, a[i]);

        memset(f, 0x3f3f3f, sizeof(f));
        f[0] = 0;

        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= i; ++j)
                f[i] = min(f[i], f[j - 1] + 1ll * (n - j + 1) * dak.query(j, i).val);

        cout << f[n];
    }
}

struct Line {
    mutable ll k, m, p;
    bool operator<(const Line& o) const { return k < o.k; }
    bool operator<(ll x) const { return p < x; }
};

struct LineContainer : multiset<Line, less<>> {
    // (for doubles, use inf = 1/.0, div(a,b) = a/b)
    static const ll inf = LLONG_MAX;
    ll div(ll a, ll b) { // floored division
        return a / b - ((a ^ b) < 0 && a % b); }
    bool isect(iterator x, iterator y) {
        if (y == end()) return x->p = inf, 0;
        if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
        else x->p = div(y->m - x->m, x->k - y->k);
        return x->p >= y->p;
    }
    void add(ll k, ll m) {
        auto z = insert({k, m, 0}), y = z++, x = y;
        while (isect(y, z)) z = erase(z);
        if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p)
            isect(x, erase(y));
    }
    ll query(ll x) {
        assert(!empty());
        auto l = *lower_bound(x);
        return l.k * x + l.m;
    }
};

namespace Sub2{
    bool valid_input(){
        bool ok = 1;
        for(int i = 2; i <= n; ++i)
            ok &= (a[i] >= a[i - 1]);
        return ok;
    }

    LineContainer cht;

    ll f[N];

    void solve(){
        f[0] = 1;
        cht.add(-n, 0);
        for(int i = 1; i <= n; ++i){
            f[i] = -cht.query(a[i]);
            cht.add(-(n - i), -f[i]);
        }

        cout << f[n];
    }
}

namespace Sub3{
    bool valid_input(){
        bool ok = 1;
        for(int i = n - 1; i >= 1; --i)
            ok &= (a[i] >= a[i + 1]);
        return ok;
    }

    ll pref[N];

    void solve(){
        f[0] = 0;
        pref[1] = f[0] + 1ll * n * a[1];
        for(int i = 1; i <= n; ++i){
            f[i] = pref[i];
            pref[i + 1] = min(pref[i], f[i] + 1ll * (n - i) * a[i + 1]);
        }
        cout << f[n];
    }
}

namespace Sub5{
    int r[N];
    ll mx[N];
    LineContainer cht;

    void solve(){
        int cur = 0;
        for(int i = 1; i <= n; ++i){
            if(a[cur] < a[i]){
                while(cur < i){
                    cht.add(-(n - cur), -f[cur]);
                    ++cur;
                }
            }
            f[i] = -cht.query(a[cur]);
        }
        cout << f[n];
    }
}

void solve(){
    cin >> n;
    for(int i = 1; i <= n; ++i)
        cin >> a[i];
    if(Sub14::valid_input())
        Sub14::solve();
    else if(Sub2::valid_input())
        Sub2::solve();
    else if(Sub3::valid_input())
        Sub3::solve();
    else
        Sub5::solve();
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(0);
    #define task "troll"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
//    cin >> test;
    for(int i = 1; i <= test; ++i){
//        cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef LOCAL
        cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    #endif
    return 0;
}

Compilation message

Discharging.cpp:26:9: warning: #pragma once in main file
   26 | #pragma once
      |         ^~~~
Discharging.cpp: In function 'int32_t main()':
Discharging.cpp:222:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  222 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:223:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  223 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10720 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 1 ms 10588 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 1 ms 10588 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 10588 KB Output is correct
2 Correct 88 ms 10760 KB Output is correct
3 Correct 87 ms 10588 KB Output is correct
4 Correct 83 ms 10760 KB Output is correct
5 Correct 92 ms 10764 KB Output is correct
6 Correct 87 ms 10588 KB Output is correct
7 Correct 92 ms 10776 KB Output is correct
8 Correct 85 ms 10588 KB Output is correct
9 Correct 89 ms 10840 KB Output is correct
10 Correct 82 ms 10588 KB Output is correct
11 Correct 91 ms 10588 KB Output is correct
12 Correct 85 ms 10588 KB Output is correct
13 Correct 81 ms 10760 KB Output is correct
14 Correct 92 ms 10764 KB Output is correct
15 Correct 86 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 10588 KB Output is correct
2 Correct 88 ms 10760 KB Output is correct
3 Correct 87 ms 10588 KB Output is correct
4 Correct 83 ms 10760 KB Output is correct
5 Correct 92 ms 10764 KB Output is correct
6 Correct 87 ms 10588 KB Output is correct
7 Correct 92 ms 10776 KB Output is correct
8 Correct 85 ms 10588 KB Output is correct
9 Correct 89 ms 10840 KB Output is correct
10 Correct 82 ms 10588 KB Output is correct
11 Correct 91 ms 10588 KB Output is correct
12 Correct 85 ms 10588 KB Output is correct
13 Correct 81 ms 10760 KB Output is correct
14 Correct 92 ms 10764 KB Output is correct
15 Correct 86 ms 10588 KB Output is correct
16 Correct 109 ms 23540 KB Output is correct
17 Correct 120 ms 24516 KB Output is correct
18 Correct 104 ms 22868 KB Output is correct
19 Correct 119 ms 24508 KB Output is correct
20 Correct 121 ms 24260 KB Output is correct
21 Correct 120 ms 24288 KB Output is correct
22 Correct 113 ms 24020 KB Output is correct
23 Correct 121 ms 25160 KB Output is correct
24 Correct 124 ms 24664 KB Output is correct
25 Correct 119 ms 24912 KB Output is correct
26 Correct 124 ms 25172 KB Output is correct
27 Correct 109 ms 23744 KB Output is correct
28 Correct 143 ms 24508 KB Output is correct
29 Correct 125 ms 24724 KB Output is correct
30 Correct 127 ms 24904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 36580 KB Output is correct
2 Correct 85 ms 36544 KB Output is correct
3 Correct 77 ms 36556 KB Output is correct
4 Correct 77 ms 36424 KB Output is correct
5 Correct 77 ms 36512 KB Output is correct
6 Correct 84 ms 36432 KB Output is correct
7 Correct 78 ms 36432 KB Output is correct
8 Correct 79 ms 36396 KB Output is correct
9 Correct 81 ms 36548 KB Output is correct
10 Correct 83 ms 36428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10720 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 1 ms 10588 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 1 ms 10588 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 98 ms 10588 KB Output is correct
16 Correct 88 ms 10760 KB Output is correct
17 Correct 87 ms 10588 KB Output is correct
18 Correct 83 ms 10760 KB Output is correct
19 Correct 92 ms 10764 KB Output is correct
20 Correct 87 ms 10588 KB Output is correct
21 Correct 92 ms 10776 KB Output is correct
22 Correct 85 ms 10588 KB Output is correct
23 Correct 89 ms 10840 KB Output is correct
24 Correct 82 ms 10588 KB Output is correct
25 Correct 91 ms 10588 KB Output is correct
26 Correct 85 ms 10588 KB Output is correct
27 Correct 81 ms 10760 KB Output is correct
28 Correct 92 ms 10764 KB Output is correct
29 Correct 86 ms 10588 KB Output is correct
30 Correct 86 ms 10756 KB Output is correct
31 Correct 93 ms 10764 KB Output is correct
32 Correct 81 ms 10588 KB Output is correct
33 Correct 92 ms 10588 KB Output is correct
34 Correct 84 ms 10756 KB Output is correct
35 Correct 98 ms 10588 KB Output is correct
36 Correct 89 ms 10588 KB Output is correct
37 Correct 89 ms 10588 KB Output is correct
38 Correct 93 ms 10904 KB Output is correct
39 Correct 92 ms 10840 KB Output is correct
40 Correct 81 ms 10588 KB Output is correct
41 Correct 92 ms 10764 KB Output is correct
42 Correct 93 ms 10760 KB Output is correct
43 Correct 93 ms 10760 KB Output is correct
44 Correct 89 ms 10768 KB Output is correct
45 Correct 85 ms 10756 KB Output is correct
46 Correct 84 ms 10756 KB Output is correct
47 Correct 98 ms 10756 KB Output is correct
48 Correct 83 ms 10760 KB Output is correct
49 Correct 82 ms 10588 KB Output is correct
50 Correct 83 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10720 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 1 ms 10588 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 1 ms 10588 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 98 ms 10588 KB Output is correct
16 Correct 88 ms 10760 KB Output is correct
17 Correct 87 ms 10588 KB Output is correct
18 Correct 83 ms 10760 KB Output is correct
19 Correct 92 ms 10764 KB Output is correct
20 Correct 87 ms 10588 KB Output is correct
21 Correct 92 ms 10776 KB Output is correct
22 Correct 85 ms 10588 KB Output is correct
23 Correct 89 ms 10840 KB Output is correct
24 Correct 82 ms 10588 KB Output is correct
25 Correct 91 ms 10588 KB Output is correct
26 Correct 85 ms 10588 KB Output is correct
27 Correct 81 ms 10760 KB Output is correct
28 Correct 92 ms 10764 KB Output is correct
29 Correct 86 ms 10588 KB Output is correct
30 Correct 109 ms 23540 KB Output is correct
31 Correct 120 ms 24516 KB Output is correct
32 Correct 104 ms 22868 KB Output is correct
33 Correct 119 ms 24508 KB Output is correct
34 Correct 121 ms 24260 KB Output is correct
35 Correct 120 ms 24288 KB Output is correct
36 Correct 113 ms 24020 KB Output is correct
37 Correct 121 ms 25160 KB Output is correct
38 Correct 124 ms 24664 KB Output is correct
39 Correct 119 ms 24912 KB Output is correct
40 Correct 124 ms 25172 KB Output is correct
41 Correct 109 ms 23744 KB Output is correct
42 Correct 143 ms 24508 KB Output is correct
43 Correct 125 ms 24724 KB Output is correct
44 Correct 127 ms 24904 KB Output is correct
45 Correct 77 ms 36580 KB Output is correct
46 Correct 85 ms 36544 KB Output is correct
47 Correct 77 ms 36556 KB Output is correct
48 Correct 77 ms 36424 KB Output is correct
49 Correct 77 ms 36512 KB Output is correct
50 Correct 84 ms 36432 KB Output is correct
51 Correct 78 ms 36432 KB Output is correct
52 Correct 79 ms 36396 KB Output is correct
53 Correct 81 ms 36548 KB Output is correct
54 Correct 83 ms 36428 KB Output is correct
55 Correct 86 ms 10756 KB Output is correct
56 Correct 93 ms 10764 KB Output is correct
57 Correct 81 ms 10588 KB Output is correct
58 Correct 92 ms 10588 KB Output is correct
59 Correct 84 ms 10756 KB Output is correct
60 Correct 98 ms 10588 KB Output is correct
61 Correct 89 ms 10588 KB Output is correct
62 Correct 89 ms 10588 KB Output is correct
63 Correct 93 ms 10904 KB Output is correct
64 Correct 92 ms 10840 KB Output is correct
65 Correct 81 ms 10588 KB Output is correct
66 Correct 92 ms 10764 KB Output is correct
67 Correct 93 ms 10760 KB Output is correct
68 Correct 93 ms 10760 KB Output is correct
69 Correct 89 ms 10768 KB Output is correct
70 Correct 85 ms 10756 KB Output is correct
71 Correct 84 ms 10756 KB Output is correct
72 Correct 98 ms 10756 KB Output is correct
73 Correct 83 ms 10760 KB Output is correct
74 Correct 82 ms 10588 KB Output is correct
75 Correct 83 ms 10588 KB Output is correct
76 Correct 114 ms 22772 KB Output is correct
77 Correct 107 ms 22472 KB Output is correct
78 Correct 106 ms 22088 KB Output is correct
79 Correct 105 ms 22340 KB Output is correct
80 Correct 113 ms 22868 KB Output is correct
81 Correct 114 ms 21960 KB Output is correct
82 Correct 109 ms 22160 KB Output is correct
83 Correct 107 ms 22624 KB Output is correct
84 Correct 108 ms 22356 KB Output is correct
85 Correct 111 ms 22600 KB Output is correct
86 Correct 105 ms 22100 KB Output is correct
87 Correct 109 ms 21988 KB Output is correct
88 Correct 105 ms 22356 KB Output is correct
89 Correct 120 ms 22204 KB Output is correct
90 Correct 116 ms 22100 KB Output is correct
91 Correct 106 ms 21836 KB Output is correct
92 Correct 122 ms 25248 KB Output is correct
93 Correct 128 ms 24660 KB Output is correct
94 Correct 108 ms 21584 KB Output is correct
95 Correct 113 ms 22912 KB Output is correct
96 Correct 104 ms 21704 KB Output is correct
97 Correct 108 ms 22264 KB Output is correct
98 Correct 111 ms 22376 KB Output is correct
99 Correct 104 ms 22100 KB Output is correct
100 Correct 113 ms 22060 KB Output is correct