Model a shop with m windows using a window class. The ticket serial number is one value shared by all windows, so keep it once in the class (a class variable or static field), while the list of tickets each window has taken belongs to each window object separately. The windows are m distinct objects numbered 1 to m. The first line gives m and the number of commands q, and the next q lines give one command each. "take w": increase the serial number by 1 (the first ticket is 1), add that ticket to the end of window w's list, and print "serial list_length" (list_length is the number of tickets in window w's list after adding). "reset w": empty only window w's list. Other windows and the serial number stay as they are. Nothing is printed. "show w": print on one line the number of tickets in window w's list followed by the serial numbers in the list in the order they were added, separated by one space (print just 0 if it is empty).
Input
1 ≤ m ≤ 100, 1 ≤ q ≤ 5000, 1 ≤ w ≤ m
Output
one line for each take and show command
Example 1 · stdin
3 9
take 1
take 2
take 1
show 1
reset 1
show 1
take 1
show 1
show 3
stdout
1 1
2 1
3 2
2 1 3
0
4 1
1 4
0
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 6 languages
This problem takes only the languages with classes: Python, JavaScript, TypeScript, Java, C# and C++ (C has no classes, and Craykon cannot define classes yet). The point is separating "a value all objects share" from "a value each object has on its own". The shared serial is static int in Java and C#, static lastSerial = 0 in JS/TS, inline static int in C++ (C++17; without inline you need a separate definition int Window::lastSerial = 0; outside the class), and last_serial = 0 in the class body in Python. There are three traps. In Python, self.last_serial += 1 does not change the class variable: it creates a new value with the same name on that object only, so each window counts on its own. Change it through the class name, Window.last_serial += 1. The opposite: putting served = [] in the class body makes one list shared by every window. A per-object list is made inside __init__ with self.served = []. In JS, reading a static field as this.lastSerial gives undefined because the object does not have it, and you get NaN (TS stops this with a compile error). Finally, when making m objects, Python [Window()] * m and JS Array(m).fill(new Window()) make m slots point to one object. Create a new one per slot, as in Python [Window() for _ in range(m)] or JS Array.from({ length: m }, () => new Window()). C++ std::vector<Window> windows(m) makes m separate values, so it is fine.
Pythonmain.py · 35 lines
import sysclass Window: last_serial = 0 # 클래스 변수: 모든 창구가 하나를 함께 쓴다 def __init__(self): self.served = [] # 객체마다 따로 갖는 목록(여기서 만들어야 한다) def take(self): Window.last_serial += 1 # self.last_serial += 1 이면 이 창구만의 값이 새로 생긴다 self.served.append(Window.last_serial) return Window.last_serial, len(self.served) def reset(self): self.served = [] def show(self): return " ".join(map(str, [len(self.served)] + self.served))data = sys.stdin.read().split()m, q = int(data[0]), int(data[1])windows = [Window() for _ in range(m)]out = []for i in range(q): cmd, w = data[2 + 2 * i], windows[int(data[3 + 2 * i]) - 1] if cmd == "take": serial, local = w.take() out.append(f"{serial} {local}") elif cmd == "reset": w.reset() else: out.append(w.show())print("\n".join(out))
JavaScriptmain.js · 35 lines
class Window { static lastSerial = 0; // 정적 필드: 모든 창구가 하나를 함께 쓴다 constructor() { this.served = []; // 객체마다 따로 갖는 목록 } take() { Window.lastSerial++; // this.lastSerial 은 객체에 없어서 undefined this.served.push(Window.lastSerial); return `${Window.lastSerial} ${this.served.length}`; } reset() { this.served = []; } show() { return [this.served.length, ...this.served].join(" "); }}const data = require("fs").readFileSync(0, "utf8").trim().split(/\s+/);const m = Number(data[0]);const q = Number(data[1]);const windows = Array.from({ length: m }, () => new Window());const out = [];for (let i = 0; i < q; i++) { const cmd = data[2 + 2 * i]; const w = windows[Number(data[3 + 2 * i]) - 1]; if (cmd === "take") out.push(w.take()); else if (cmd === "reset") w.reset(); else out.push(w.show());}console.log(out.join("\n"));
TypeScriptmain.ts · 34 lines
import { readFileSync } from "node:fs";class Window { private static lastSerial = 0; // 정적 필드: 모든 창구가 하나를 함께 쓴다 private served: number[] = []; // 객체마다 따로 갖는 목록 take(): string { Window.lastSerial++; this.served.push(Window.lastSerial); return `${Window.lastSerial} ${this.served.length}`; } reset(): void { this.served = []; } show(): string { return [this.served.length, ...this.served].join(" "); }}const data: string[] = readFileSync(0, "utf8").trim().split(/\s+/);const m: number = Number(data[0]);const q: number = Number(data[1]);const windows: Window[] = Array.from({ length: m }, () => new Window());const out: string[] = [];for (let i = 0; i < q; i++) { const cmd = data[2 + 2 * i]; const w = windows[Number(data[3 + 2 * i]) - 1]; if (cmd === "take") out.push(w.take()); else if (cmd === "reset") w.reset(); else out.push(w.show());}console.log(out.join("\n"));
JavaMain.java · 49 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.StringTokenizer;class Window { private static int lastSerial = 0; // static: 모든 창구가 하나를 함께 쓴다 private List<Integer> served = new ArrayList<>(); // 객체마다 따로 String take() { lastSerial++; served.add(lastSerial); return lastSerial + " " + served.size(); } void reset() { served = new ArrayList<>(); } String show() { StringBuilder sb = new StringBuilder().append(served.size()); for (int s : served) sb.append(' ').append(s); return sb.toString(); }}public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); int m = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); Window[] windows = new Window[m]; for (int i = 0; i < m; i++) windows[i] = new Window(); StringBuilder out = new StringBuilder(); for (int i = 0; i < q; i++) { st = new StringTokenizer(in.readLine()); String cmd = st.nextToken(); Window w = windows[Integer.parseInt(st.nextToken()) - 1]; switch (cmd) { case "take" -> out.append(w.take()).append('\n'); case "reset" -> w.reset(); default -> out.append(w.show()).append('\n'); } } System.out.print(out); }}
C#Main.cs · 45 lines
using System;using System.Collections.Generic;using System.Text;class Window{ private static int lastSerial = 0; // static: 모든 창구가 하나를 함께 쓴다 private List<int> served = new List<int>(); // 객체마다 따로 public string Take() { lastSerial++; served.Add(lastSerial); return $"{lastSerial} {served.Count}"; } public void Reset() => served = new List<int>(); public string Show() => served.Count == 0 ? "0" : $"{served.Count} {string.Join(" ", served)}";}class Program{ static void Main() { string[] first = Console.ReadLine().Split(' '); int m = int.Parse(first[0]), q = int.Parse(first[1]); var windows = new Window[m]; for (int i = 0; i < m; i++) windows[i] = new Window(); var sb = new StringBuilder(); for (int i = 0; i < q; i++) { string[] p = Console.ReadLine().Trim().Split(' '); Window w = windows[int.Parse(p[1]) - 1]; switch (p[0]) { case "take": sb.Append(w.Take()).Append('\n'); break; case "reset": w.Reset(); break; default: sb.Append(w.Show()).Append('\n'); break; } } Console.Write(sb); }}
C++main.cpp · 40 lines
#include <iostream>#include <string>#include <vector>class Window {public: std::string take() { lastSerial++; served_.push_back(lastSerial); return std::to_string(lastSerial) + " " + std::to_string(served_.size()); } void reset() { served_.clear(); } std::string show() const { std::string s = std::to_string(served_.size()); for (int x : served_) s += " " + std::to_string(x); return s; }private: inline static int lastSerial = 0; // static: 모든 창구가 하나를 함께 쓴다(inline 없으면 클래스 밖에 정의가 필요) std::vector<int> served_; // 객체마다 따로};int main() { std::ios::sync_with_stdio(false); int m, q; std::cin >> m >> q; std::vector<Window> windows(m); // 서로 다른 Window 객체 m개 for (int i = 0; i < q; i++) { std::string cmd; int w; std::cin >> cmd >> w; Window& win = windows[w - 1]; if (cmd == "take") std::cout << win.take() << '\n'; else if (cmd == "reset") win.reset(); else std::cout << win.show() << '\n'; }}