제출 #1327196

#제출 시각아이디문제언어결과실행 시간메모리
1327196martin7272772Stove (JOI18_stove)C++20
0 / 100
0 ms332 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * Problem: Stove (JOI 2017/2018 Final Round)
 * Goal: Minimize the total operating time of the stove.
 */

int main() {
    // Optimize input and output speed
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int N, K;
    if (!(cin >> N >> K)) return 0;

    vector<long long> T(N);
    for (int i = 0; i < N; ++i) {
        cin >> T[i];
    }

    // Each guest stays for exactly 1 minute (from T_i to T_i + 1).
    // If we turn the stove on/off for every guest individually,
    // the minimum time is N minutes, using N matches.
    long long total_time = N;

    // We calculate the gaps between consecutive guests.
    // A gap is the time from when guest i leaves (T[i]+1) 
    // to when guest i+1 arrives (T[i+1]).
    vector<long long> gaps;
    for (int i = 0; i < N - 1; ++i) {
        long long gap_duration = T[i + 1] - (T[i] + 1);
        if (gap_duration > 0) {
            gaps.push_back(gap_duration);
        }
    }

    // To reduce the number of matches from N down to K, 
    // we must "fill" (keep the stove on during) N - K gaps.
    // To minimize total time, we pick the shortest gaps to fill.
    sort(gaps.begin(), gaps.end());

    int matches_to_reduce = N - K;
    for (int i = 0; i < matches_to_reduce && i < (int)gaps.size(); ++i) {
        total_time += gaps[i];
    }

    cout << total_time << endl;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...