import sysfrom collections import dequelines = sys.stdin.read().split("\n")n, rack = map(int, lines[0].split())waiting = deque() # 기다리는 사람들: 뒤로 넣고 앞에서 꺼낸다out = []for line in lines[1:n + 1]: parts = line.split() if parts[0] == "want": if rack > 0: rack -= 1 out.append(parts[1]) else: waiting.append(parts[1]) out.append("-") elif waiting: out.append(waiting.popleft()) # 가장 먼저 온 사람이 받는다 else: rack += 1 # 기다리는 사람이 없으면 대여함에 꽂는다 out.append("-")out.append(f"{len(waiting)} {rack}")print("\n".join(out))
JavaScriptmain.js · 26 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const [n, u] = lines[0].trim().split(" ").map(Number);let rack = u;// JS에는 큐가 따로 없다. 배열 뒤에 넣고, 맨 앞 자리(head)만 뒤로 옮긴다.const waiting = [];let head = 0;const out = [];for (let i = 1; i <= n; i++) { const [cmd, name] = lines[i].trim().split(" "); if (cmd === "want") { if (rack > 0) { rack--; out.push(name); } else { waiting.push(name); out.push("-"); } } else if (head < waiting.length) { out.push(waiting[head++]); // 가장 먼저 온 사람이 받는다 } else { rack++; // 기다리는 사람이 없으면 대여함에 꽂는다 out.push("-"); }}out.push(`${waiting.length - head} ${rack}`);console.log(out.join("\n"));
TypeScriptmain.ts · 28 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const [n, u]: number[] = lines[0].trim().split(" ").map(Number);let rack: number = u;// TS에도 큐가 따로 없다. 배열 뒤에 넣고, 맨 앞 자리(head)만 뒤로 옮긴다.const waiting: string[] = [];let head = 0;const out: string[] = [];for (let i = 1; i <= n; i++) { const [cmd, name]: string[] = lines[i].trim().split(" "); if (cmd === "want") { if (rack > 0) { rack--; out.push(name); } else { waiting.push(name); out.push("-"); } } else if (head < waiting.length) { out.push(waiting[head++]); // 가장 먼저 온 사람이 받는다 } else { rack++; // 기다리는 사람이 없으면 대여함에 꽂는다 out.push("-"); }}out.push(`${waiting.length - head} ${rack}`);console.log(out.join("\n"));
JavaMain.java · 34 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayDeque;public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] first = in.readLine().trim().split(" "); int n = Integer.parseInt(first[0]); int rack = Integer.parseInt(first[1]); ArrayDeque<String> waiting = new ArrayDeque<>(); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { String[] parts = in.readLine().trim().split(" "); if (parts[0].equals("want")) { if (rack > 0) { rack--; out.append(parts[1]).append('\n'); } else { waiting.addLast(parts[1]); out.append("-\n"); } } else if (!waiting.isEmpty()) { out.append(waiting.pollFirst()).append('\n'); // 가장 먼저 온 사람이 받는다 } else { rack++; // 기다리는 사람이 없으면 대여함에 꽂는다 out.append("-\n"); } } out.append(waiting.size()).append(' ').append(rack); System.out.println(out); }}
C#Main.cs · 43 行
using System;using System.Collections.Generic;using System.Text;class Program{ static void Main() { string[] first = Console.ReadLine().Trim().Split(' '); int n = int.Parse(first[0]); int rack = int.Parse(first[1]); var waiting = new Queue<string>(); var sb = new StringBuilder(); for (int i = 0; i < n; i++) { string[] parts = Console.ReadLine().Trim().Split(' '); if (parts[0] == "want") { if (rack > 0) { rack--; sb.Append(parts[1]).Append('\n'); } else { waiting.Enqueue(parts[1]); sb.Append("-\n"); } } else if (waiting.Count > 0) { sb.Append(waiting.Dequeue()).Append('\n'); // 가장 먼저 온 사람이 받는다 } else { rack++; // 기다리는 사람이 없으면 대여함에 꽂는다 sb.Append("-\n"); } } sb.Append(waiting.Count).Append(' ').Append(rack).Append('\n'); Console.Write(sb); }}
C++main.cpp · 35 行
#include <iostream>#include <queue>#include <string>using namespace std;int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, rack; cin >> n >> rack; queue<string> waiting; for (int i = 0; i < n; i++) { string cmd; cin >> cmd; if (cmd == "want") { string name; cin >> name; if (rack > 0) { rack--; cout << name << '\n'; } else { waiting.push(name); cout << "-\n"; } } else if (!waiting.empty()) { cout << waiting.front() << '\n'; // 가장 먼저 온 사람이 받는다 waiting.pop(); // pop()은 값을 돌려주지 않는다 } else { rack++; // 기다리는 사람이 없으면 대여함에 꽂는다 cout << "-\n"; } } cout << waiting.size() << ' ' << rack << '\n'; return 0;}
Cmain.c · 35 行
#include <stdio.h>#include <string.h>#define MAXN 9000/* 배열로 만든 큐: tail 자리에 넣고 head 자리에서 꺼낸다. 기다리는 사람은 모두 합쳐 n명을 넘지 않으니 MAXN칸이면 된다. */static char waiting[MAXN][11];int main(void) { int n, rack; scanf("%d %d", &n, &rack); int head = 0, tail = 0; for (int i = 0; i < n; i++) { char cmd[8], name[11]; scanf("%7s", cmd); if (strcmp(cmd, "want") == 0) { scanf("%10s", name); if (rack > 0) { rack--; printf("%s\n", name); } else { strcpy(waiting[tail++], name); printf("-\n"); } } else if (head < tail) { printf("%s\n", waiting[head++]); /* 가장 먼저 온 사람이 받는다 */ } else { rack++; /* 기다리는 사람이 없으면 대여함에 꽂는다 */ printf("-\n"); } } printf("%d %d\n", tail - head, rack); return 0;}
Craykonmain.cray · 36 行
첫줄 = コンソール.数読み();n = 첫줄[0];rack = 첫줄[1];// 목록으로 만든 큐: 뒤에 넣고, 맨 앞 자리(head)만 뒤로 옮긴다waiting = [];head = 0;繰り返す (i = 0; i < n; i++){ parts = コンソール.行読み().空白除去().分ける(); もし (parts[0] == "want") { もし (rack > 0) { rack -= 1; コンソール.書く(parts[1]); } でなければ { waiting.追加(parts[1]); コンソール.書く("-"); } } でなければ もし (head < waiting.個数) { // 가장 먼저 온 사람이 받는다 コンソール.書く(waiting[head]); head += 1; } でなければ { // 기다리는 사람이 없으면 대여함에 꽂는다 rack += 1; コンソール.書く("-"); }}コンソール.書く(waiting.個数 - head + " " + rack);