Submission #1107146

# Submission time Handle Problem Language Result Execution time Memory
1107146 2024-10-31T17:53:51 Z trnthienphc2003 Stove (JOI18_stove) C++17
100 / 100
37 ms 4688 KB
#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 1e6 + 7;
int n, k;
int t[maxn];

void input() {
    cin >> n >> k;
    for(int i = 1; i <= n; i++) {
        cin >> t[i];
    }
}

namespace sub1 {
    bool check() {
        return n <= 20 && *max_element(t + 1, t + 1 + n) <= 20;
    }

    int backtrack(int last_seg, int num_seg) {
        if(last_seg == n) return 0;
        int ans = 0;
        if(num_seg < k) ans = max(ans, backtrack(last_seg + 1, num_seg + 1) + t[last_seg + 1] - t[last_seg] - 1);
        ans = max(ans, backtrack(last_seg + 1, num_seg));
        return ans;
    }

    int solve() {
        cout << t[n] + 1 - t[1] - backtrack(1, 1);
        return 0;
    }
}

namespace sub2 {
    bool check() {
        return n <= 5000;
    }

    int solve() {
        long long ans = t[n] + 1 - t[1];
        priority_queue <long long> pq;
        for(int i = 2; i <= n; ++i) {
            pq.push(t[i] - t[i - 1] - 1);
        }

        int cnt = 1;
        while(cnt < k && !pq.empty()) {
            ans -= pq.top();
            pq.pop();
            cnt++;
        }

        cout << ans;
        return 0;
    }
}

namespace sub3 {
    bool check() {
        return n <= 1e6;
    }

    int solve() {
        // cout << "sub3\n";
        long long ans = t[n] + 1 - t[1];
        priority_queue <long long> pq;
        for(int i = 2; i <= n; ++i) {
            pq.push(t[i] - t[i - 1] - 1);
        }

        int cnt = 1;
        while(cnt < k && !pq.empty()) {
            ans -= pq.top();
            pq.pop();
            cnt++;
        }

        cout << ans;
        return 0;
    }
}

#define NAME "test"
int main() {
    if(fopen(NAME".txt", "r")) {
        freopen(NAME".txt", "r", stdin);
    }

    input();

    if(sub1::check()) return sub1::solve();
    if(sub2::check()) return sub2::solve();
    if(sub3::check()) return sub3::solve();
}

Compilation message

stove.cpp: In function 'int main()':
stove.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         freopen(NAME".txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 2 ms 336 KB Output is correct
13 Correct 2 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 2 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 2 ms 336 KB Output is correct
13 Correct 2 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 2 ms 336 KB Output is correct
16 Correct 35 ms 3784 KB Output is correct
17 Correct 31 ms 4604 KB Output is correct
18 Correct 28 ms 4556 KB Output is correct
19 Correct 32 ms 4564 KB Output is correct
20 Correct 34 ms 4688 KB Output is correct
21 Correct 36 ms 4544 KB Output is correct
22 Correct 37 ms 4552 KB Output is correct
23 Correct 37 ms 4564 KB Output is correct
24 Correct 37 ms 4556 KB Output is correct