Submission #697496

# Submission time Handle Problem Language Result Execution time Memory
697496 2023-02-10T06:53:17 Z hct_2so1 Discharging (NOI20_discharging) C++14
72 / 100
120 ms 32216 KB
#include <bits/stdc++.h>

#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define all(v) (v).begin(), (v).end()
#define F first
#define S second
#define __builtin_popcount __builtin_popcountll
#define sz(v) ((int) (v).size())
#define uni(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define pii(a, b) make_pair(a, b)

using namespace std;
typedef long long ll;

///

const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll oo = 1e18 + 7;

const int N = 1e6 + 5;

int n, a[N], nxt[N];
ll dp[N];

struct line
{
    ll a, b;
    line (const ll &_a, const ll &_b)
    {
        a = _a;
        b = _b;
    }
    ll eval(const ll &x)
    {
        return a * x + b;
    }
};

deque <line> st;

bool bad(const line &l1, const line &l2, const line &l3)
{
    return (l3.b - l1.b) * (l1.a - l2.a) < (l2.b - l1.b) * (l1.a - l3.a);
}

void add(const line &x)
{
    while (st.size() >= 2 && bad(st[st.size() - 2], st[st.size() - 1], x)) st.pop_back();
    st.push_back(x);
}

ll query(ll x)
{
    while (st.size() >= 2 && st[0].eval(x) >= st[1].eval(x)) st.pop_front();
    return st[0].eval(x);
}

void Input()
{
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    nxt[1] = 1;
    for (int i = 2; i <= n; i ++)
        if (a[nxt[i - 1]] < a[i]) nxt[i] = i;
        else nxt[i] = nxt[i - 1];
}

void solve()
{
    add(line(0, 0));
    for (int i = 1; i <= n; i ++)
    {
        if (nxt[i] == i) dp[i] = query(a[i]) + 1LL * n * a[i];
        else dp[i] = dp[i - 1];
        add(line(-i, dp[i]));
    }
    cout << dp[n];
}

int main()
{
    std::ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("DIAMOND.inp", "r", stdin);
//    freopen("DIAMOND.out", "w", stdout);
    int test = 1;
    //cin >> test;
    while (test--)
    {
        Input();
        solve();
    }
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 90 ms 15896 KB Output is correct
17 Correct 90 ms 15844 KB Output is correct
18 Correct 80 ms 15964 KB Output is correct
19 Correct 93 ms 21716 KB Output is correct
20 Correct 96 ms 21560 KB Output is correct
21 Correct 93 ms 21648 KB Output is correct
22 Correct 92 ms 21284 KB Output is correct
23 Correct 103 ms 22408 KB Output is correct
24 Correct 93 ms 21836 KB Output is correct
25 Correct 96 ms 22208 KB Output is correct
26 Correct 100 ms 22524 KB Output is correct
27 Correct 86 ms 20932 KB Output is correct
28 Correct 100 ms 21804 KB Output is correct
29 Correct 95 ms 22028 KB Output is correct
30 Correct 103 ms 22280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 32216 KB Output is correct
2 Correct 120 ms 32208 KB Output is correct
3 Correct 113 ms 32128 KB Output is correct
4 Correct 109 ms 32096 KB Output is correct
5 Correct 111 ms 32172 KB Output is correct
6 Correct 108 ms 32212 KB Output is correct
7 Correct 112 ms 32104 KB Output is correct
8 Correct 108 ms 32092 KB Output is correct
9 Correct 107 ms 32128 KB Output is correct
10 Correct 109 ms 32212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 90 ms 15896 KB Output is correct
31 Correct 90 ms 15844 KB Output is correct
32 Correct 80 ms 15964 KB Output is correct
33 Correct 93 ms 21716 KB Output is correct
34 Correct 96 ms 21560 KB Output is correct
35 Correct 93 ms 21648 KB Output is correct
36 Correct 92 ms 21284 KB Output is correct
37 Correct 103 ms 22408 KB Output is correct
38 Correct 93 ms 21836 KB Output is correct
39 Correct 96 ms 22208 KB Output is correct
40 Correct 100 ms 22524 KB Output is correct
41 Correct 86 ms 20932 KB Output is correct
42 Correct 100 ms 21804 KB Output is correct
43 Correct 95 ms 22028 KB Output is correct
44 Correct 103 ms 22280 KB Output is correct
45 Correct 110 ms 32216 KB Output is correct
46 Correct 120 ms 32208 KB Output is correct
47 Correct 113 ms 32128 KB Output is correct
48 Correct 109 ms 32096 KB Output is correct
49 Correct 111 ms 32172 KB Output is correct
50 Correct 108 ms 32212 KB Output is correct
51 Correct 112 ms 32104 KB Output is correct
52 Correct 108 ms 32092 KB Output is correct
53 Correct 107 ms 32128 KB Output is correct
54 Correct 109 ms 32212 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 0 ms 340 KB Output is correct
63 Correct 0 ms 340 KB Output is correct
64 Correct 0 ms 340 KB Output is correct
65 Correct 0 ms 340 KB Output is correct
66 Correct 0 ms 340 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 0 ms 340 KB Output is correct
69 Correct 0 ms 340 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 0 ms 340 KB Output is correct
73 Correct 0 ms 340 KB Output is correct
74 Correct 1 ms 340 KB Output is correct
75 Correct 1 ms 340 KB Output is correct
76 Correct 88 ms 21968 KB Output is correct
77 Incorrect 91 ms 22008 KB Output isn't correct
78 Halted 0 ms 0 KB -