작성일 :

문제 링크

22150번 - Шоколадка

설명

왼쪽과 오른쪽 조각을 합쳐 n×n 초콜릿을 만들 수 있는지 확인해, 먹은 사람이 있으면 yes, 없으면 no를 출력하는 문제입니다.


접근법

각 행에서 왼쪽 조각과 오른쪽 조각의 합이 전체 크기와 같으면 그 행은 완전합니다. 모든 행이 완전하면 아무도 먹지 않은 것이므로 no, 하나라도 부족하면 yes를 출력합니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;

class Program {
  static void Main() {
    var t = int.Parse(Console.ReadLine()!);
    for (var tc = 0; tc < t; tc++) {
      var parts = Console.ReadLine()!.Split();
      var idx = 0;
      var n = int.Parse(parts[idx++]);
      var eaten = false;
      for (var i = 0; i < n; i++) {
        var l = int.Parse(parts[idx++]);
        var r = int.Parse(parts[idx++]);
        if (l + r < n) eaten = true;
      }

      Console.WriteLine(eaten ? "yes" : "no");
    }
  }
}

C++

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

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

  int t; cin >> t;
  for (int tc = 0; tc < t; tc++) {
    int n; cin >> n;
    bool eaten = false;
    for (int i = 0; i < n; i++) {
      int l, r; cin >> l >> r;
      if (l + r < n) eaten = true;
    }
    cout << (eaten ? "yes" : "no") << "\n";
  }

  return 0;
}