작성일 :

문제 링크

27593번 - Walking Boy

설명

하루 동안 보낸 메시지 시각들이 주어질 때, 메시지를 보내지 않은 시간에 120분 산책을 두 번 이상 넣을 수 있는지 판별하는 문제입니다.


접근법

산책은 연속 120분이 필요하고, 메시지는 산책 시작 직전이나 끝 직후에는 보낼 수 있습니다.

따라서 인접한 두 메시지 사이의 빈 구간 길이가 d라면, 그 구간 안에는 d / 120번의 산책을 넣을 수 있습니다. 자정부터 첫 메시지 전까지, 마지막 메시지부터 하루 끝까지도 같은 방식으로 계산하면 됩니다.

이렇게 얻은 산책 가능 횟수의 합이 2 이상이면 YES, 아니면 NO입니다.



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
using System;
using System.Linq;

class Program {
  static void Main() {
    int t = int.Parse(Console.ReadLine()!);

    for (int tc = 0; tc < t; tc++) {
      int n = int.Parse(Console.ReadLine()!);
      int[] a = Console.ReadLine()!.Split().Select(int.Parse).ToArray();

      int walks = a[0] / 120;

      for (int i = 1; i < n; i++) {
        walks += (a[i] - a[i - 1]) / 120;
      }

      walks += (1440 - a[n - 1]) / 120;

      Console.WriteLine(walks >= 2 ? "YES" : "NO");
    }
  }
}

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
28
29
30
31
32
#include <bits/stdc++.h>
using namespace std;

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

  int t;
  cin >> t;

  while (t--) {
    int n;
    cin >> n;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    int walks = a[0] / 120;

    for (int i = 1; i < n; i++) {
      walks += (a[i] - a[i - 1]) / 120;
    }

    walks += (1440 - a[n - 1]) / 120;

    cout << (walks >= 2 ? "YES" : "NO") << "\n";
  }

  return 0;
}

Tags: 27593, BOJ, C#, C++, 구현, 그리디, 백준, 알고리즘

Categories: ,