답안 #671106

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
671106 2022-12-12T03:56:33 Z vuavisao Discharging (NOI20_discharging) C++14
100 / 100
108 ms 25132 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define ld long double
using namespace std;

const ll N = 1e6 + 10;
const ll INF = 1e18;

struct line {
    ll a = 0, b = 0;
    line() {};
    line(ll _a, ll _b) { a = _a; b = _b; }

    ll Fn(const ll& x) { return this -> a * x + this -> b; }
};

bool bad(line A, line B, line C) {
    return (ld) (C.b - A.b) / (A.a - C.a) < (ld) (B.b - A.b) / (A.a - B.a);
}

struct CHT {
    vector<line> memo = {};
    ll ptr = 0; /// for a query where x has been sorted

    void add_line(const line& cur) {
        ll k = memo.size();
        while(k >= 2 && bad(memo[k - 2], memo[k - 1], cur)) {
            memo.pop_back();
            -- k;
        }
        memo.push_back(cur); ++ k;
        if(ptr >= k) ptr = k - 1;
    }

    ll Query_bs(const ll& x) {
        if(memo.empty()) return INF;
        ll l = 0, r = memo.size() - 1;
        while(l < r) {
            ll mid = (l + r) >> 1;
            if(memo[mid].Fn(x) < memo[mid + 1].Fn(x)) r = mid;
            else l = mid + 1;
        }
        return memo[l].Fn(x);
    }

    ll Query_ptr(const ll& x) {
        if(memo.empty()) return INF;
        while(ptr < memo.size() - 1 && memo[ptr].Fn(x) > memo[ptr + 1].Fn(x)) ++ ptr;
        return memo[ptr].Fn(x);
    }

    void clear() {
        this -> memo.clear();
        this -> ptr = 0;
    }
};

void pre_process();

ll n, a[N];

ll dp[N];

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen("Discharging.inp", "r")) {
        freopen("Discharging.inp", "r", stdin);
        freopen("Discharging.out", "w", stdout);
    }
    cin >> n;
    for(ll i = 1; i <= n; ++ i) cin >> a[i];
    reverse(a + 1, a + 1 + n);
    deque<ll> dq = {};
    for(ll i = 1; i <= n; ++ i) {
        while(!dq.empty() && a[dq.back()] <= a[i]) dq.pop_back();
        dq.push_back(i);
    }
    CHT cht = {};
    cht.add_line(line(a[dq[0]], 0));
    for(ll i = 1; i <= (int) dq.size(); ++ i) {
        dp[i] = cht.Query_ptr(dq[i - 1]);
        if(i < (int) dq.size()) cht.add_line(line(a[dq[i]], dp[i]));
    }
    cout << dp[dq.size()];
    return 0;
}

/// Code by vuavisao

Compilation message

Discharging.cpp: In member function 'long long int CHT::Query_ptr(const long long int&)':
Discharging.cpp:50:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         while(ptr < memo.size() - 1 && memo[ptr].Fn(x) > memo[ptr + 1].Fn(x)) ++ ptr;
      |               ~~~~^~~~~~~~~~~~~~~~~
Discharging.cpp: In function 'int32_t main()':
Discharging.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen("Discharging.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen("Discharging.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 376 KB Output is correct
3 Correct 0 ms 328 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 332 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 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 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 340 KB Output is correct
5 Correct 1 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 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 340 KB Output is correct
5 Correct 1 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 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 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 84 ms 19436 KB Output is correct
17 Correct 90 ms 21192 KB Output is correct
18 Correct 81 ms 16992 KB Output is correct
19 Correct 89 ms 21036 KB Output is correct
20 Correct 88 ms 21708 KB Output is correct
21 Correct 92 ms 21172 KB Output is correct
22 Correct 84 ms 20024 KB Output is correct
23 Correct 106 ms 24228 KB Output is correct
24 Correct 97 ms 22588 KB Output is correct
25 Correct 99 ms 22808 KB Output is correct
26 Correct 102 ms 25132 KB Output is correct
27 Correct 88 ms 18844 KB Output is correct
28 Correct 89 ms 21436 KB Output is correct
29 Correct 103 ms 22916 KB Output is correct
30 Correct 98 ms 24404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 103 ms 17752 KB Output is correct
2 Correct 100 ms 17704 KB Output is correct
3 Correct 94 ms 17740 KB Output is correct
4 Correct 94 ms 17784 KB Output is correct
5 Correct 94 ms 17740 KB Output is correct
6 Correct 100 ms 17812 KB Output is correct
7 Correct 94 ms 17728 KB Output is correct
8 Correct 96 ms 17740 KB Output is correct
9 Correct 95 ms 17788 KB Output is correct
10 Correct 96 ms 17784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 376 KB Output is correct
3 Correct 0 ms 328 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 332 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 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 1 ms 340 KB Output is correct
14 Correct 1 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 1 ms 340 KB Output is correct
19 Correct 1 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 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 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 0 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 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 1 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 336 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 376 KB Output is correct
3 Correct 0 ms 328 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 332 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 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 1 ms 340 KB Output is correct
14 Correct 1 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 1 ms 340 KB Output is correct
19 Correct 1 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 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 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 84 ms 19436 KB Output is correct
31 Correct 90 ms 21192 KB Output is correct
32 Correct 81 ms 16992 KB Output is correct
33 Correct 89 ms 21036 KB Output is correct
34 Correct 88 ms 21708 KB Output is correct
35 Correct 92 ms 21172 KB Output is correct
36 Correct 84 ms 20024 KB Output is correct
37 Correct 106 ms 24228 KB Output is correct
38 Correct 97 ms 22588 KB Output is correct
39 Correct 99 ms 22808 KB Output is correct
40 Correct 102 ms 25132 KB Output is correct
41 Correct 88 ms 18844 KB Output is correct
42 Correct 89 ms 21436 KB Output is correct
43 Correct 103 ms 22916 KB Output is correct
44 Correct 98 ms 24404 KB Output is correct
45 Correct 103 ms 17752 KB Output is correct
46 Correct 100 ms 17704 KB Output is correct
47 Correct 94 ms 17740 KB Output is correct
48 Correct 94 ms 17784 KB Output is correct
49 Correct 94 ms 17740 KB Output is correct
50 Correct 100 ms 17812 KB Output is correct
51 Correct 94 ms 17728 KB Output is correct
52 Correct 96 ms 17740 KB Output is correct
53 Correct 95 ms 17788 KB Output is correct
54 Correct 96 ms 17784 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 0 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 1 ms 340 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 0 ms 340 KB Output is correct
65 Correct 1 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 336 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 0 ms 340 KB Output is correct
72 Correct 1 ms 340 KB Output is correct
73 Correct 1 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 87 ms 14060 KB Output is correct
77 Correct 90 ms 14196 KB Output is correct
78 Correct 79 ms 13492 KB Output is correct
79 Correct 90 ms 13768 KB Output is correct
80 Correct 88 ms 14456 KB Output is correct
81 Correct 80 ms 13640 KB Output is correct
82 Correct 78 ms 13644 KB Output is correct
83 Correct 87 ms 14016 KB Output is correct
84 Correct 87 ms 13848 KB Output is correct
85 Correct 87 ms 14104 KB Output is correct
86 Correct 82 ms 13528 KB Output is correct
87 Correct 89 ms 13400 KB Output is correct
88 Correct 88 ms 13820 KB Output is correct
89 Correct 81 ms 13716 KB Output is correct
90 Correct 87 ms 13520 KB Output is correct
91 Correct 78 ms 13320 KB Output is correct
92 Correct 108 ms 23008 KB Output is correct
93 Correct 96 ms 21280 KB Output is correct
94 Correct 82 ms 12972 KB Output is correct
95 Correct 88 ms 14412 KB Output is correct
96 Correct 92 ms 13364 KB Output is correct
97 Correct 86 ms 13748 KB Output is correct
98 Correct 82 ms 13908 KB Output is correct
99 Correct 83 ms 13516 KB Output is correct
100 Correct 82 ms 13672 KB Output is correct