Submission #1327194

#TimeUsernameProblemLanguageResultExecution timeMemory
1327194martin7272772Stove (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)
 * Goal: Minimize total stove operating time given N guests and K matches.
 */

int main() {
    // Optimize standard I/O
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int N, K;
    [span_5](start_span)if (!(cin >> N >> K)) return 0;[span_5](end_span)

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

    // The minimum time the stove must be on is N (1 minute per guest).
    // We start by assuming the stove stays on from the first guest's 
    // arrival until the last guest's departure.
    // Total time = (T[N-1] + 1) - T[0]
    [span_7](start_span)[span_8](start_span)long long total_time = (T[N - 1] + 1) - T[0];[span_7](end_span)[span_8](end_span)

    // To minimize time, we want to turn the stove off during the largest 
    // gaps between guest visits. 
    // A gap between guest i and i+1 is T[i+1] - (T[i] + 1).
    vector<long long> gaps;
    for (int i = 0; i < N - 1; ++i) {
        long long gap_size = T[i + 1] - (T[i] + 1);
        if (gap_size > 0) {
            gaps.push_back(gap_size);
        }
    }

    // Sort gaps in descending order to remove the largest ones first.
    sort(gaps.rbegin(), gaps.rend());

    // With K matches, we can turn the stove on K times. 
    // The first match is used for the very first guest.
    // Each additional match (up to K-1) allows us to skip one gap.
    [span_9](start_span)int matches_to_use = min((int)gaps.size(), K - 1);[span_9](end_span)
    for (int i = 0; i < matches_to_use; ++i) {
        total_time -= gaps[i];
    }

    [span_10](start_span)cout << total_time << endl;[span_10](end_span)

    return 0;
}

Compilation message (stderr)

stove.cpp: In function 'int main()':
stove.cpp:18:6: error: 'span_5' was not declared in this scope
   18 |     [span_5](start_span)if (!(cin >> N >> K)) return 0;[span_5](end_span)
      |      ^~~~~~
stove.cpp:18:14: error: 'start_span' has not been declared
   18 |     [span_5](start_span)if (!(cin >> N >> K)) return 0;[span_5](end_span)
      |              ^~~~~~~~~~
stove.cpp: In lambda function:
stove.cpp:18:25: error: expected '{' before 'if'
   18 |     [span_5](start_span)if (!(cin >> N >> K)) return 0;[span_5](end_span)
      |                         ^~
stove.cpp: In function 'int main()':
stove.cpp:18:25: error: expected ';' before 'if'
   18 |     [span_5](start_span)if (!(cin >> N >> K)) return 0;[span_5](end_span)
      |                         ^~
      |                         ;
stove.cpp:18:65: error: 'end_span' has not been declared
   18 |     [span_5](start_span)if (!(cin >> N >> K)) return 0;[span_5](end_span)
      |                                                                 ^~~~~~~~
stove.cpp: In lambda function:
stove.cpp:20:5: error: expected '{' before 'vector'
   20 |     vector<long long> T(N);
      |     ^~~~~~
stove.cpp: In function 'int main()':
stove.cpp:18:74: error: expected ';' before 'vector'
   18 |     [span_5](start_span)if (!(cin >> N >> K)) return 0;[span_5](end_span)
      |                                                                          ^
      |                                                                          ;
   19 | 
   20 |     vector<long long> T(N);
      |     ~~~~~~                                                                
stove.cpp:22:10: error: 'span_6' was not declared in this scope
   22 |         [span_6](start_span)cin >> T[i];[span_6](end_span)
      |          ^~~~~~
stove.cpp:22:18: error: 'start_span' has not been declared
   22 |         [span_6](start_span)cin >> T[i];[span_6](end_span)
      |                  ^~~~~~~~~~
stove.cpp: In lambda function:
stove.cpp:22:29: error: expected '{' before 'cin'
   22 |         [span_6](start_span)cin >> T[i];[span_6](end_span)
      |                             ^~~
stove.cpp: In function 'int main()':
stove.cpp:22:29: error: expected ';' before 'cin'
   22 |         [span_6](start_span)cin >> T[i];[span_6](end_span)
      |                             ^~~
      |                             ;
stove.cpp:22:50: error: 'end_span' has not been declared
   22 |         [span_6](start_span)cin >> T[i];[span_6](end_span)
      |                                                  ^~~~~~~~
stove.cpp: In lambda function:
stove.cpp:23:5: error: expected '{' before '}' token
   23 |     }
      |     ^
stove.cpp: In function 'int main()':
stove.cpp:22:59: error: expected ';' before '}' token
   22 |         [span_6](start_span)cin >> T[i];[span_6](end_span)
      |                                                           ^
      |                                                           ;
   23 |     }
      |     ~                                                      
stove.cpp:29:6: error: 'span_7' was not declared in this scope
   29 |     [span_7](start_span)[span_8](start_span)long long total_time = (T[N - 1] + 1) - T[0];[span_7](end_span)[span_8](end_span)
      |      ^~~~~~
stove.cpp:29:14: error: 'start_span' has not been declared
   29 |     [span_7](start_span)[span_8](start_span)long long total_time = (T[N - 1] + 1) - T[0];[span_7](end_span)[span_8](end_span)
      |              ^~~~~~~~~~
stove.cpp: In lambda function:
stove.cpp:29:25: error: expected '{' before '[' token
   29 |     [span_7](start_span)[span_8](start_span)long long total_time = (T[N - 1] + 1) - T[0];[span_7](end_span)[span_8](end_span)
      |                         ^
stove.cpp: In function 'int main()':
stove.cpp:29:26: error: 'span_8' was not declared in this scope
   29 |     [span_7](start_span)[span_8](start_span)long long total_time = (T[N - 1] + 1) - T[0];[span_7](end_span)[span_8](end_span)
      |                          ^~~~~~
stove.cpp:29:34: error: 'start_span' was not declared in this scope
   29 |     [span_7](start_span)[span_8](start_span)long long total_time = (T[N - 1] + 1) - T[0];[span_7](end_span)[span_8](end_span)
      |                                  ^~~~~~~~~~
stove.cpp:29:99: error: 'end_span' has not been declared
   29 |     [span_7](start_span)[span_8](start_span)long long total_time = (T[N - 1] + 1) - T[0];[span_7](end_span)[span_8](end_span)
      |                                                                                                   ^~~~~~~~
stove.cpp: In lambda function:
stove.cpp:29:108: error: expected '{' before '[' token
   29 |     [span_7](start_span)[span_8](start_span)long long total_time = (T[N - 1] + 1) - T[0];[span_7](end_span)[span_8](end_span)
      |                                                                                                            ^
stove.cpp: In function 'int main()':
stove.cpp:29:117: error: 'end_span' was not declared in this scope
   29 |     [span_7](start_span)[span_8](start_span)long long total_time = (T[N - 1] + 1) - T[0];[span_7](end_span)[span_8](end_span)
      |                                                                                                                     ^~~~~~~~
stove.cpp:36:30: error: 'T' was not declared in this scope
   36 |         long long gap_size = T[i + 1] - (T[i] + 1);
      |                              ^
stove.cpp:38:13: error: 'gaps' was not declared in this scope
   38 |             gaps.push_back(gap_size);
      |             ^~~~
stove.cpp:43:10: error: 'gaps' was not declared in this scope
   43 |     sort(gaps.rbegin(), gaps.rend());
      |          ^~~~
stove.cpp:48:6: error: 'span_9' was not declared in this scope
   48 |     [span_9](start_span)int matches_to_use = min((int)gaps.size(), K - 1);[span_9](end_span)
      |      ^~~~~~
stove.cpp:48:14: error: 'start_span' is not a type
   48 |     [span_9](start_span)int matches_to_use = min((int)gaps.size(), K - 1);[span_9](end_span)
      |              ^~~~~~~~~~
stove.cpp:48:25: error: type-specifier invalid in lambda
   48 |     [span_9](start_span)int matches_to_use = min((int)gaps.size(), K - 1);[span_9](end_span)
      |                         ^~~
stove.cpp: In lambda function:
stove.cpp:48:29: error: expected '{' before 'matches_to_use'
   48 |     [span_9](start_span)int matches_to_use = min((int)gaps.size(), K - 1);[span_9](end_span)
      |                             ^~~~~~~~~~~~~~
stove.cpp: In function 'int main()':
stove.cpp:48:28: error: expected ';' before 'matches_to_use'
   48 |     [span_9](start_span)int matches_to_use = min((int)gaps.size(), K - 1);[span_9](end_span)
      |                            ^~~~~~~~~~~~~~~
      |                            ;
stove.cpp:48:84: error: 'end_span' is not a type
   48 |     [span_9](start_span)int matches_to_use = min((int)gaps.size(), K - 1);[span_9](end_span)
      |                                                                                    ^~~~~~~~
stove.cpp: In lambda function:
stove.cpp:49:5: error: expected '{' before 'for'
   49 |     for (int i = 0; i < matches_to_use; ++i) {
      |     ^~~
stove.cpp: In function 'int main()':
stove.cpp:48:93: error: expected ';' before 'for'
   48 |     [span_9](start_span)int matches_to_use = min((int)gaps.size(), K - 1);[span_9](end_span)
      |                                                                                             ^
      |                                                                                             ;
   49 |     for (int i = 0; i < matches_to_use; ++i) {
      |     ~~~                                                                                      
stove.cpp:49:25: error: 'matches_to_use' was not declared in this scope
   49 |     for (int i = 0; i < matches_to_use; ++i) {
      |                         ^~~~~~~~~~~~~~
stove.cpp:49:21: error: 'i' was not declared in this scope
   49 |     for (int i = 0; i < matches_to_use; ++i) {
      |                     ^
stove.cpp:53:6: error: 'span_10' was not declared in this scope
   53 |     [span_10](start_span)cout << total_time << endl;[span_10](end_span)
      |      ^~~~~~~
stove.cpp:53:15: error: 'start_span' is not a type
   53 |     [span_10](start_span)cout << total_time << endl;[span_10](end_span)
      |               ^~~~~~~~~~
stove.cpp: In lambda function:
stove.cpp:53:26: error: expected '{' before 'cout'
   53 |     [span_10](start_span)cout << total_time << endl;[span_10](end_span)
      |                          ^~~~
stove.cpp: In function 'int main()':
stove.cpp:53:26: error: expected ';' before 'cout'
   53 |     [span_10](start_span)cout << total_time << endl;[span_10](end_span)
      |                          ^~~~
      |                          ;
stove.cpp:53:63: error: 'end_span' is not a type
   53 |     [span_10](start_span)cout << total_time << endl;[span_10](end_span)
      |                                                               ^~~~~~~~
stove.cpp: In lambda function:
stove.cpp:55:5: error: expected '{' before 'return'
   55 |     return 0;
      |     ^~~~~~
stove.cpp: In function 'int main()':
stove.cpp:53:72: error: expected ';' before 'return'
   53 |     [span_10](start_span)cout << total_time << endl;[span_10](end_span)
      |                                                                        ^
      |                                                                        ;
   54 | 
   55 |     return 0;
      |     ~~~~~~