Submission #640371

# Submission time Handle Problem Language Result Execution time Memory
640371 2022-09-14T11:42:24 Z sumit_kk10 Discharging (NOI20_discharging) C++17
100 / 100
120 ms 22448 KB
#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define F first
#define S second
#define int long long
using namespace std;
const int N = 1e6 + 5, MOD = 1e9 + 7;
int n, a[N], dp[N];

struct Line{
    int m, c;

    int val(int x){
        return m*x + c;
    }
};

vector<Line> hulls;

bool check(Line a, Line b, Line cc){
    __int128 x = (b.c - a.c);
    x = x * (a.m - cc.m);
    __int128 y = (cc.c - a.c);
    y = y * (a.m - b.m);
    if(x > y)
        return true;
    return false;
}

void insert(int mm, int cc){
    Line cur;
    cur.m = mm;
    cur.c = cc;
    while(hulls.size() > 1){
        if(check(hulls[hulls.size() - 2], hulls[hulls.size() - 1], cur))
            hulls.pop_back();
        else
            break;
    }
    hulls.pb(cur);
}

int get(int x){
    int low = 0, high = (int) hulls.size() - 2, ans = (int) hulls.size() - 1;
    while(low <= high){
        int mid = (low + high) / 2;
        if(hulls[mid].val(x) <= hulls[mid + 1].val(x)){
            ans = mid;
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
    return hulls[ans].val(x);
}

void solve(){
    cin >> n;
    a[0] = LLONG_MAX;
    bool ok = true;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
        if(a[i] > a[i - 1])
            ok = false;
    }
    if(ok){
        cout << a[1] * n << "\n";
        return;
    }
    int mx = 0;
    for(int i = 1; i <= n; ++i){
        mx = max(mx, a[i]);
        insert(n - i + 1, dp[i - 1]);
        dp[i] = get(mx);
    }
    cout << dp[n] << "\n";
}
 
int32_t main(){
    fast;
    int t = 1;
    // cin >> t;
    while(t--)
        solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 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 288 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 0 ms 340 KB Output is correct
6 Correct 1 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 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 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 1 ms 340 KB Output is correct
2 Correct 1 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 0 ms 340 KB Output is correct
6 Correct 1 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 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 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 95 ms 15856 KB Output is correct
17 Correct 95 ms 16200 KB Output is correct
18 Correct 92 ms 15860 KB Output is correct
19 Correct 92 ms 15900 KB Output is correct
20 Correct 106 ms 15928 KB Output is correct
21 Correct 96 ms 15928 KB Output is correct
22 Correct 91 ms 15952 KB Output is correct
23 Correct 117 ms 15948 KB Output is correct
24 Correct 91 ms 15960 KB Output is correct
25 Correct 97 ms 15948 KB Output is correct
26 Correct 108 ms 16188 KB Output is correct
27 Correct 93 ms 16256 KB Output is correct
28 Correct 94 ms 16252 KB Output is correct
29 Correct 113 ms 16120 KB Output is correct
30 Correct 109 ms 16172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 8076 KB Output is correct
2 Correct 89 ms 9492 KB Output is correct
3 Correct 96 ms 9460 KB Output is correct
4 Correct 87 ms 9544 KB Output is correct
5 Correct 96 ms 9428 KB Output is correct
6 Correct 95 ms 9552 KB Output is correct
7 Correct 102 ms 9532 KB Output is correct
8 Correct 86 ms 9496 KB Output is correct
9 Correct 94 ms 9524 KB Output is correct
10 Correct 112 ms 9488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 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 288 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 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 0 ms 340 KB Output is correct
20 Correct 1 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 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 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 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 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 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 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 1 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 0 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 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 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 288 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 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 0 ms 340 KB Output is correct
20 Correct 1 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 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 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 95 ms 15856 KB Output is correct
31 Correct 95 ms 16200 KB Output is correct
32 Correct 92 ms 15860 KB Output is correct
33 Correct 92 ms 15900 KB Output is correct
34 Correct 106 ms 15928 KB Output is correct
35 Correct 96 ms 15928 KB Output is correct
36 Correct 91 ms 15952 KB Output is correct
37 Correct 117 ms 15948 KB Output is correct
38 Correct 91 ms 15960 KB Output is correct
39 Correct 97 ms 15948 KB Output is correct
40 Correct 108 ms 16188 KB Output is correct
41 Correct 93 ms 16256 KB Output is correct
42 Correct 94 ms 16252 KB Output is correct
43 Correct 113 ms 16120 KB Output is correct
44 Correct 109 ms 16172 KB Output is correct
45 Correct 91 ms 8076 KB Output is correct
46 Correct 89 ms 9492 KB Output is correct
47 Correct 96 ms 9460 KB Output is correct
48 Correct 87 ms 9544 KB Output is correct
49 Correct 96 ms 9428 KB Output is correct
50 Correct 95 ms 9552 KB Output is correct
51 Correct 102 ms 9532 KB Output is correct
52 Correct 86 ms 9496 KB Output is correct
53 Correct 94 ms 9524 KB Output is correct
54 Correct 112 ms 9488 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 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 1 ms 340 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 1 ms 340 KB Output is correct
69 Correct 1 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 1 ms 340 KB Output is correct
73 Correct 0 ms 340 KB Output is correct
74 Correct 0 ms 340 KB Output is correct
75 Correct 1 ms 340 KB Output is correct
76 Correct 99 ms 17592 KB Output is correct
77 Correct 101 ms 17552 KB Output is correct
78 Correct 93 ms 21336 KB Output is correct
79 Correct 93 ms 21580 KB Output is correct
80 Correct 110 ms 22264 KB Output is correct
81 Correct 92 ms 21448 KB Output is correct
82 Correct 116 ms 21360 KB Output is correct
83 Correct 90 ms 21836 KB Output is correct
84 Correct 89 ms 21716 KB Output is correct
85 Correct 96 ms 22024 KB Output is correct
86 Correct 90 ms 21512 KB Output is correct
87 Correct 88 ms 21280 KB Output is correct
88 Correct 95 ms 21604 KB Output is correct
89 Correct 103 ms 21412 KB Output is correct
90 Correct 85 ms 21312 KB Output is correct
91 Correct 86 ms 21144 KB Output is correct
92 Correct 104 ms 22448 KB Output is correct
93 Correct 120 ms 21768 KB Output is correct
94 Correct 95 ms 20856 KB Output is correct
95 Correct 92 ms 22220 KB Output is correct
96 Correct 84 ms 21200 KB Output is correct
97 Correct 94 ms 21448 KB Output is correct
98 Correct 93 ms 21708 KB Output is correct
99 Correct 93 ms 21316 KB Output is correct
100 Correct 93 ms 21468 KB Output is correct