작성일 :

문제 링크

15845번 - Winning ICPC

설명

각 팀이 문제마다 몇 개의 테스트케이스를 맞혔는지가 주어질 때, 완전히 푼 문제 수가 가장 많은 팀의 번호를 구하는 문제입니다.


접근법

한 팀이 어떤 문제를 풀었다고 보려면, 그 문제의 전체 테스트케이스 수와 맞힌 개수가 같아야 합니다.

따라서 팀별로 모든 문제를 보면서 완전히 푼 문제 수를 셉니다. 그 수가 현재까지의 최댓값보다 크면 우승 팀을 갱신하면 됩니다.

동률이면 번호가 더 작은 팀이 우승이므로, 같은 개수일 때는 갱신하지 않으면 됩니다.



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
28
29
30
31
32
using System;
using System.Linq;

class Program {
  static void Main() {
    int[] first = Console.ReadLine()!.Split().Select(int.Parse).ToArray();
    int n = first[0];
    int m = first[1];

    int[] total = Console.ReadLine()!.Split().Select(int.Parse).ToArray();

    int winner = 1;
    int bestSolved = -1;

    for (int team = 1; team <= n; team++) {
      int[] solved = Console.ReadLine()!.Split().Select(int.Parse).ToArray();
      int count = 0;

      for (int j = 0; j < m; j++) {
        if (solved[j] == total[j])
          count++;
      }

      if (count > bestSolved) {
        bestSolved = count;
        winner = team;
      }
    }

    Console.WriteLine(winner);
  }
}

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
33
34
35
36
37
38
#include <bits/stdc++.h>
using namespace std;

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

  int n, m;
  cin >> n >> m;

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

  int winner = 1;
  int bestSolved = -1;

  for (int team = 1; team <= n; team++) {
    int count = 0;

    for (int j = 0; j < m; j++) {
      int solved;
      cin >> solved;
      if (solved == total[j])
        count++;
    }

    if (count > bestSolved) {
      bestSolved = count;
      winner = team;
    }
  }

  cout << winner << "\n";

  return 0;
}

Tags: 15845, BOJ, C#, C++, 구현, 백준, 알고리즘

Categories: ,