Submission #1327195

#TimeUsernameProblemLanguageResultExecution timeMemory
1327195martin7272772Stove (JOI18_stove)C++20
Compilation error
0 ms0 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * Problem: Stove (JOI 2017/2018 Final Round)
 * [span_5](start_span)Task: Calculate minimum total operating time of the stove[span_5](end_span).
 * Constraints: N up to 100,000; K up to N; [span_6](start_span)T_i up to 1,000,000,000[span_6](end_span).
 */

int main() {
    // Optimization for fast I/O performance
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int N, K;
    [span_7](start_span)// Read N (number of guests) and K (number of matches)[span_7](end_span)
    if (!(cin >> N >> K)) return 0;

    vector<long long> T(N);
    for (int i = 0; i < N; ++i) {
        [span_8](start_span)// Read arrival time T_i for each guest[span_8](end_span)
        cin >> T[i];
    }

    // BASE CASE: The stove must be on at least 1 minute for every guest.
    [span_9](start_span)// Each guest stays from T_i to T_i + 1[span_9](end_span).
    // If we turn the stove on and off for every single guest (using N matches),
    [span_10](start_span)// the total time is exactly N minutes[span_10](end_span).
    long long total_time = N;

    // To use fewer than N matches, we must keep the stove on between guests.
    // We calculate the "gaps" between the departure of guest i and arrival of guest i+1.
    // Gap = T[i+1] - (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);
        }
    }

    // Sort gaps in ascending order.
    // We want to fill the smallest gaps first to stay within the match limit K.
    [span_11](start_span)// Staying on during a gap "saves" one match[span_11](end_span).
    sort(gaps.begin(), gaps.end());

    // Initially, we have N separate intervals (N matches used).
    // We need to reduce the number of matches used from N down to K.
    // Each gap we "fill" (keep stove on) reduces the match count by 1.
    int matches_to_reduce = N - K;
    
    for (int i = 0; i < matches_to_reduce; ++i) {
        // Add the duration of the smallest available gaps to the total time.
        total_time += gaps[i];
    }

    [span_12](start_span)// Output the minimum total operating time[span_12](end_span).
    cout << total_time << endl;

    return 0;
}

Compilation message (stderr)

stove.cpp: In function 'int main()':
stove.cpp:19:6: error: 'span_7' was not declared in this scope
   19 |     [span_7](start_span)// Read N (number of guests) and K (number of matches)[span_7](end_span)
      |      ^~~~~~
stove.cpp:19:14: error: 'start_span' has not been declared
   19 |     [span_7](start_span)// Read N (number of guests) and K (number of matches)[span_7](end_span)
      |              ^~~~~~~~~~
stove.cpp: In lambda function:
stove.cpp:20:5: error: expected '{' before 'if'
   20 |     if (!(cin >> N >> K)) return 0;
      |     ^~
stove.cpp: In function 'int main()':
stove.cpp:19:25: error: expected ';' before 'if'
   19 |     [span_7](start_span)// Read N (number of guests) and K (number of matches)[span_7](end_span)
      |                         ^
      |                         ;
   20 |     if (!(cin >> N >> K)) return 0;
      |     ~~                   
stove.cpp:24:10: error: 'span_8' was not declared in this scope
   24 |         [span_8](start_span)// Read arrival time T_i for each guest[span_8](end_span)
      |          ^~~~~~
stove.cpp:24:18: error: 'start_span' has not been declared
   24 |         [span_8](start_span)// Read arrival time T_i for each guest[span_8](end_span)
      |                  ^~~~~~~~~~
stove.cpp: In lambda function:
stove.cpp:25:9: error: expected '{' before 'cin'
   25 |         cin >> T[i];
      |         ^~~
stove.cpp: In function 'int main()':
stove.cpp:24:29: error: expected ';' before 'cin'
   24 |         [span_8](start_span)// Read arrival time T_i for each guest[span_8](end_span)
      |                             ^
      |                             ;
   25 |         cin >> T[i];
      |         ~~~                  
stove.cpp:29:6: error: 'span_9' was not declared in this scope
   29 |     [span_9](start_span)// Each guest stays from T_i to T_i + 1[span_9](end_span).
      |      ^~~~~~
stove.cpp:29:14: error: 'start_span' has not been declared
   29 |     [span_9](start_span)// Each guest stays from T_i to T_i + 1[span_9](end_span).
      |              ^~~~~~~~~~
stove.cpp: In lambda function:
stove.cpp:31:5: error: expected '{' before '[' token
   31 |     [span_10](start_span)// the total time is exactly N minutes[span_10](end_span).
      |     ^
stove.cpp: In function 'int main()':
stove.cpp:31:6: error: 'span_10' was not declared in this scope
   31 |     [span_10](start_span)// the total time is exactly N minutes[span_10](end_span).
      |      ^~~~~~~
stove.cpp:31:15: error: 'start_span' was not declared in this scope
   31 |     [span_10](start_span)// the total time is exactly N minutes[span_10](end_span).
      |               ^~~~~~~~~~
stove.cpp:47:6: error: 'span_11' was not declared in this scope
   47 |     [span_11](start_span)// Staying on during a gap "saves" one match[span_11](end_span).
      |      ^~~~~~~
stove.cpp:47:15: error: 'start_span' is not a type
   47 |     [span_11](start_span)// Staying on during a gap "saves" one match[span_11](end_span).
      |               ^~~~~~~~~~
stove.cpp: In lambda function:
stove.cpp:48:5: error: expected '{' before 'sort'
   48 |     sort(gaps.begin(), gaps.end());
      |     ^~~~
stove.cpp: In function 'int main()':
stove.cpp:47:26: error: expected ';' before 'sort'
   47 |     [span_11](start_span)// Staying on during a gap "saves" one match[span_11](end_span).
      |                          ^
      |                          ;
   48 |     sort(gaps.begin(), gaps.end());
      |     ~~~~                  
stove.cpp:57:9: error: 'total_time' was not declared in this scope; did you mean 'localtime'?
   57 |         total_time += gaps[i];
      |         ^~~~~~~~~~
      |         localtime
stove.cpp:60:6: error: 'span_12' was not declared in this scope
   60 |     [span_12](start_span)// Output the minimum total operating time[span_12](end_span).
      |      ^~~~~~~
stove.cpp:60:15: error: 'start_span' is not a type
   60 |     [span_12](start_span)// Output the minimum total operating time[span_12](end_span).
      |               ^~~~~~~~~~
stove.cpp: In lambda function:
stove.cpp:61:5: error: expected '{' before 'cout'
   61 |     cout << total_time << endl;
      |     ^~~~
stove.cpp: In function 'int main()':
stove.cpp:60:26: error: expected ';' before 'cout'
   60 |     [span_12](start_span)// Output the minimum total operating time[span_12](end_span).
      |                          ^
      |                          ;
   61 |     cout << total_time << endl;
      |     ~~~~