[백준 13866] 팀 나누기 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
네 명의 스킬 레벨이 주어질 때, 두 팀으로 나누어 팀별 스킬 합의 차이를 최소로 만드는 문제입니다.
접근법
네 값을 정렬하면 가장 작은 값과 가장 큰 값을 한 팀으로, 나머지 두 값을 한 팀으로 구성할 때 차이가 최소가 됩니다. 정렬된 값을 a, b, c, d라 하면 (a + d)와 (b + c)의 차이의 절댓값이 답입니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
using System;
using System.Linq;
class Program {
static void Main() {
var nums = Console.ReadLine()!.Split().Select(int.Parse).ToArray();
Array.Sort(nums);
var diff = Math.Abs(nums[0] + nums[3] - nums[1] - nums[2]);
Console.WriteLine(diff);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vi a(4);
for (int i = 0; i < 4; i++) cin >> a[i];
sort(a.begin(), a.end());
int diff = abs(a[0] + a[3] - a[1] - a[2]);
cout << diff << "\n";
return 0;
}