A neighborhood library numbers its days, calling the day it opened day 1. The library is shut on its fixed closed days. The first line gives the number of closed days m, and the second line gives the m closed day numbers separated by single spaces. The closed days are given in any order, not necessarily sorted. The third line gives the number of loans n, and each of the next n lines gives one loan's borrowing day d and loan period k, separated by a single space. For each loan, the due day is first set to day d + k. If that day is not a closed day, the due day stays day d + k. If that day is a closed day, the due day moves one day later, and if that day is also a closed day, it moves one more day. It keeps moving one day at a time like this until it lands on a day that is not a closed day. The borrowing day d may itself be a closed day (books are borrowed from a self-service machine); that has no effect on the due day. Print the due day number of each loan, one per line, in input order.
Input
1 ≤ m ≤ 50, closed day numbers are integers from 1 to 2,000 and are all different. 1 ≤ n ≤ 1,000, 1 ≤ d ≤ 1,000, 1 ≤ k ≤ 30. Numbers in the input are plain digits without commas
Output
n lines, each the due day number
Example 1 · stdin
4
12 5 6 9
4
3 2
1 1
10 2
2 7
stdout
7
2
13
10
Example 2 · stdin
1
3
2
1 2
3 30
stdout
4
33
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 · 8 languages
This problem asks whether a day is 'in' the list of closed days. Python uses in on a set, Java HashSet<Integer>.contains, C# HashSet<int>.Contains, C++ unordered_set::count (contains from C++20), JS/TS includes on an array, and Craykon Contains on the list read with Console.ReadNumbers(). The C standard library has no function that tests membership in an unsorted array (bsearch works only on sorted arrays), so the solution writes a function is_closed that takes the array together with its count m and compares one by one. With only 50 closed days, scanning the list from the start is fast enough. Trap 1: the moved day must be checked again. If you ask only once with if, the first loan of example 1, where days 5 and 6 are both closed, becomes 6 instead of 7. Write 'move one day while it is a closed day' literally as a while loop (Craykon while). Trap 2: in JS/TS, due in closed asks whether that index (key) exists, not whether the value is there. The list in example 1 has indexes 0 to 3, so 5 in closed is false and the first loan becomes 5, while the second loan wrongly treats days 2 and 3 as closed and becomes 4. Trap 3: indexOf returns -1 when the value is missing and 0 when it is at the front. If you ask closed.indexOf(due) > 0, you miss the closed day at the front of the list, and the third loan of example 1 becomes 12 instead of 13. Ask !== -1 or use includes. Trap 4: the values in the list and the value you ask about must have the same type. If you only split the second line and do not convert it, the list holds the string "5". JS/TS includes (which compares like ===), Python in and Craykon Contains all treat the number 5 and the string "5" as different. In Craykon, if you read it with Console.ReadLine().Split() and forget ToNumber(), no day is ever seen as closed, and the first loan of example 1 becomes 5. Trap 5: in Java, passing an int[] to Arrays.asList gives not a List<Integer> but a List<int[]> holding one array. It compiles, but contains(due) is always false, so the first loan of example 1 becomes 5. Add the numbers one by one to a HashSet<Integer> as this solution does. Trap 6: in C, the parameter int days[] is really a pointer. So sizeof days / sizeof days[0] inside the function is not the number of closed days but pointer size ÷ int size (8 ÷ 4 = 2 on the judge's 64-bit system). It looks only at the first 2 days and misses the rest, so the first loan of example 1 becomes 6. The judge's gcc 11 only warns and compiles it. Pass the count as a separate parameter.
Pythonmain.py · 11 lines
input() # mclosed = set(map(int, input().split())) # 수로 바꿔 집합에 모은다: 문자열 "5"와 수 5는 다르다n = int(input())out = []for _ in range(n): d, k = map(int, input().split()) due = d + k while due in closed: # 미룬 날도 휴관일인지 다시 묻는다(if로는 한 번만 묻는다) due += 1 out.append(due)print("\n".join(map(str, out)))
JavaScriptmain.js · 13 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");// 수로 바꾼 배열: includes는 ===로 비교하니 문자열 "5"는 수 5와 다르다const closed = lines[1].trim().split(/\s+/).map(Number);const n = Number(lines[2]);const out = [];for (let i = 0; i < n; i++) { const [d, k] = lines[3 + i].trim().split(/\s+/).map(Number); let due = d + k; // `due in closed`는 값이 아니라 칸 번호를 묻는다. 값은 includes로 묻는다 while (closed.includes(due)) due++; // 미룬 날도 다시 묻는다 out.push(due);}console.log(out.join("\n"));
TypeScriptmain.ts · 14 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");// 수로 바꾼 배열: includes는 ===로 비교하니 문자열 "5"는 수 5와 다르다const closed: number[] = lines[1].trim().split(/\s+/).map(Number);const n = Number(lines[2]);const out: number[] = [];for (let i = 0; i < n; i++) { const [d, k] = lines[3 + i].trim().split(/\s+/).map(Number); let due = d + k; // `due in closed`는 값이 아니라 칸 번호를 묻는다. 값은 includes로 묻는다 while (closed.includes(due)) due++; // 미룬 날도 다시 묻는다 out.push(due);}console.log(out.join("\n"));
JavaMain.java · 25 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.HashSet;import java.util.Set;public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in.readLine(); // m // int[]를 Arrays.asList에 넘기면 List<int[]>가 되니, Set<Integer>에 하나씩 넣는다 Set<Integer> closed = new HashSet<>(); for (String c : in.readLine().trim().split("\\s+")) { closed.add(Integer.parseInt(c)); } int n = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { String[] p = in.readLine().trim().split("\\s+"); int due = Integer.parseInt(p[0]) + Integer.parseInt(p[1]); while (closed.contains(due)) due++; // 미룬 날도 다시 묻는다 out.append(due).append('\n'); } System.out.print(out); }}
C#Main.cs · 23 lines
using System;using System.Collections.Generic;using System.Linq;using System.Text;class Program{ static void Main() { Console.ReadLine(); // m var closed = new HashSet<int>(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse)); int n = int.Parse(Console.ReadLine()); var sb = new StringBuilder(); for (int i = 0; i < n; i++) { string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int due = int.Parse(p[0]) + int.Parse(p[1]); while (closed.Contains(due)) due++; // 미룬 날도 다시 묻는다 sb.Append(due).Append('\n'); } Console.Write(sb); }}
C++main.cpp · 21 lines
#include <iostream>#include <unordered_set>int main() { int m, n; std::cin >> m; std::unordered_set<int> closed; for (int i = 0; i < m; i++) { int day; std::cin >> day; closed.insert(day); } std::cin >> n; for (int i = 0; i < n; i++) { int d, k; std::cin >> d >> k; int due = d + k; while (closed.count(due) > 0) due++; // 미룬 날도 다시 묻는다(C++20부터는 closed.contains(due)) std::cout << due << '\n'; }}
Cmain.c · 28 lines
#include <stdbool.h>#include <stdio.h>/* C 표준 라이브러리에는 정렬하지 않은 배열에서 "들어 있나"를 묻는 함수가 없다(bsearch는 정렬된 배열용). 배열과 함께 개수도 받아 하나씩 비교한다. 매개변수 days는 포인터라서 함수 안에서 sizeof로 개수를 셀 수 없다. */bool is_closed(const int *days, int m, int x) { for (int i = 0; i < m; i++) { if (days[i] == x) return true; } return false;}int main(void) { int m, n; int days[50]; scanf("%d", &m); for (int i = 0; i < m; i++) scanf("%d", &days[i]); scanf("%d", &n); for (int i = 0; i < n; i++) { int d, k; scanf("%d %d", &d, &k); int due = d + k; while (is_closed(days, m, due)) due++; /* 미룬 날도 다시 묻는다 */ printf("%d\n", due); } return 0;}
Craykonmain.cray · 13 lines
Console.ReadLine(); // mclosed = Console.ReadNumbers(); // 수 목록: 나누기()만 하면 문자열 "5"가 들어 있어 수 5를 찾지 못한다n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){ 칸 = Console.ReadLine().Split(); due = 칸[0].ToNumber() + 칸[1].ToNumber(); while (closed.Contains(due)) { due += 1; } Console.WriteLine(due);}