https://www.acmicpc.net/problem/1092
자바로 다시 풀어봣습니다. 그리드문제이고 중요포인트는 크레인이 한번에 동시작용한다는걸 잊으면 안됩니다.
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan=new Scanner(System.in);
ArrayList<Long>crains=new ArrayList<>();
ArrayList<Long>boxs=new ArrayList<>();
int n=scan.nextInt();
for(int i=0;i<n;i++){
long crain=scan.nextLong();
crains.add(crain);
}
int m=scan.nextInt();
for(int i=0;i<m;i++){
long box=scan.nextLong();
boxs.add(box);
}
Collections.sort(crains);
Collections.sort(boxs);
Collections.reverse(crains);
Collections.reverse(boxs);
int i=0;
int j=0;
int gcnt=0;
boolean bchk=false;
while(!boxs.isEmpty()){
if(boxs.get(i)<=crains.get(j)){
// System.out.print(boxs.get(i));
// System.out.print(crains.get(j));
if( j==0 ){
gcnt++;
}
boxs.remove( i );
j++;
j = j%n;
i=0;
}else{
if(j == 0){
bchk = true;
break;
}
i++;
}
if(i>boxs.size()-1){
i=0;
j=0;
}
}
if(bchk==false){
System.out.println(gcnt);
}else{
System.out.println(-1);
}
}
}
'알고리즘 공부' 카테고리의 다른 글
백준 치즈 (0) | 2023.11.21 |
---|---|
백준 주사위 (복습) (1) | 2023.11.21 |
백준 트리(복습) (1) | 2023.11.20 |
백준 암호 만들기(복습) (0) | 2023.11.15 |
백준 동전2(복습) (1) | 2023.11.15 |