公式

D - AtCoder Amusement Park 解説 by en_translator


Scan the queue from the front, and repeatedly update the following information to solve the problem.

  • The number of current empty seats
  • How many times the attraction was started so far

One can use for and if statements to update it appropriately.

It may make easier to come up with implementation if you understand like this: for each group, you either “just guide the group” or “start the attraction, then guide the group.”

Sample code is as follows.

N, K = map(int, input().split())

A = list(map(int, input().split()))

empty_sheets = K # Initially there are K empty seets,
start_count = 0 # and the attraction was started 0 times

for a in A:
    if empty_sheets < a: # If there is not enough empty seets,
        start_count += 1 # start the attraction,
        empty_sheets = K # and prepare K empty seats anew
    empty_sheets -= a # a people sits
    
start_count += 1 # Finally, start the attraction once

print(start_count)
#include <iostream>

using namespace std;

int main() {
    int N, K;
    cin >> N >> K;
    
    // Initially, there are K empty seats and the attraction was started 0 times
    int empty_sheets = K, start_count = 0;
    
    for (int i = 0; i < N; ++i) {
        int a;
        cin >> a; // Receive the input
        if (empty_sheets < a) { // If there is not enough empty seats,
            ++start_count; // start the attraction,
            empty_sheets = K; // and prepare K empty seats anew
        }
        empty_sheets -= a; // a people sits
    }
    ++start_count; // Finally, start the attraction once
    
    cout << start_count << endl;
    
    return 0;
}

投稿日時:
最終更新: