작성일 :

문제 링크

16497번 - 대출 요청

설명

대출 요청 N개가 주어지며 각 요청은 대출일부터 반납일 전날까지의 구간입니다. 반납일에는 책이 다시 사용 가능합니다.

도서는 동일한 책 K권만 있습니다. 어떤 날이든 동시 대출 수가 K를 초과하면 모든 요청을 처리할 수 없습니다.


접근법

먼저, 날짜별 대출 수를 저장할 배열을 준비합니다. 날짜는 1부터 31까지입니다.

다음으로, 각 요청에 대해 대출일부터 반납일 전날까지 해당 날짜에 1씩 더합니다.

이후, 모든 날짜를 확인하여 K를 초과하는 날이 있으면 불가능, 없으면 가능을 출력합니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;

namespace Solution {
  class Program {
    static void Main(string[] args) {
      var n = int.Parse(Console.ReadLine()!);
      var day = new int[32];

      for (var i = 0; i < n; i++) {
        var p = Console.ReadLine()!.Split();
        var b = int.Parse(p[0]);
        var r = int.Parse(p[1]);
        for (var d = b; d < r; d++)
          day[d]++;
      }

      var k = int.Parse(Console.ReadLine()!);
      foreach (var cnt in day) {
        if (cnt > k) {
          Console.WriteLine(0);
          return;
        }
      }
      Console.WriteLine(1);
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  int day[32] = {0, };
  for (int i = 0; i < n; i++) {
    int b, r; cin >> b >> r;
    for (int d = b; d < r; d++)
      day[d]++;
  }

  int k; cin >> k;
  for (int d = 0; d < 32; d++) {
    if (day[d] > k) {
      cout << 0 << "\n";
      return 0;
    }
  }
  cout << 1 << "\n";

  return 0;
}