하루 한 접시
[백준] 1092번: 배 [C#]
NaZZU
2024. 4. 5. 23:58
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
namespace 연습장
{
internal class Program
{
static StringBuilder sb = new StringBuilder();
static void Main(string[] args)
{
int N = int.Parse(Console.ReadLine()); // 크레인 개수
int[] crane = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); // 각 크레인의 무게 제한
Array.Sort(crane);
Array.Reverse(crane); // 무게제한을 저장하는 배열을 내림차순 정렬
int M = int.Parse(Console.ReadLine()); // 짐 개수
int[] boxes = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
Array.Sort(boxes);
Array.Reverse(boxes); // 짐 배열 내림차순 정렬
if (crane[0] < boxes[0])
{
Console.WriteLine("-1"); return; // 가장 큰 크레인이 가장 무거운 짐을 들 수 없다면 -1 출력 후 프로그램 종료
}
List<int> boxlist = new List<int>(boxes); // RemoveAt 메서드 사용을 위한 리스트 생성
int time = 0;
while (boxlist.Count != 0) // 박스의 개수가 0이 아니면 반복
{
for (int i = 0; i < crane.Length; i++) // 크레인의 개수만큼 반복
{
int removeIndenx = FindBox(boxlist, crane[i]); // 크레인이 들 박스의 크기를 받아오는 메서드
if (removeIndenx != -1)
boxlist.RemoveAt(removeIndenx); // 받아온 좌표의 짐 삭제
}
time++; // 시간 증가
}
Console.WriteLine(time);
}
static int FindBox(List<int> boxlist, int crane)
{
for (int i = 0; i < boxlist.Count; i++) // 0번지 부터 돌아가면서
{
if (crane >= boxlist[i]) // 크레인의 무게제한보다 짐의 무게가 작거나 같다면
return i; // 해당 인덱스 리턴
}
return -1; // 들 수 있는 짐이 없다면 -1 리턴
}
}
}
|
cs |