Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- java
- 백트래킹
- programmers
- 알고리즘
- lambda
- kotlin
- back-end
- 코틀린
- 그래프
- Spring
- 프로그래머스
- 자료구조
- LEVEL2
- backtracking
- Brute-force
- Java8
- baekjoon
- BFS
- 백준
- 네트워크
- DFS
- 운영체제
- TDD
- DP
- 프로젝트
- 자바
- algorithm
- OS
- 모던자바
- 스프링
Archives
- Today
- Total
요깨비's LAB
[백준, Brute-Force, Java] P.7795 먹을 것인가 먹힐 것인가 본문
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int T;
static int[] A;
static int[] B;
public static void main(String[] args) {
YoggaebReader input = new YoggaebReader();
T = input.nextInt();
StringBuilder sb = new StringBuilder();
for(int t=0;t<T;t++) {
// System.out.println("test case " + t);
int aLen = input.nextInt();
int bLen = input.nextInt();
A = new int[aLen];
B = new int[bLen];
for (int i = 0; i < aLen; i++) {
A[i] = input.nextInt();
}
for (int i = 0; i < bLen; i++) {
B[i] = input.nextInt();
}
Arrays.sort(A);
Arrays.sort(B);
int count = 0;
for(int i=0;i<aLen;i++) {
int findIdx = binarySearch(B,A[i]);
if(findIdx >= 0)
count += (findIdx);
else {
count += (findIdx+1) * -1;
}
}
sb.append(count + "\n");
}
System.out.print(sb.toString());
}
public static int binarySearch(int[] arr, int target) {
int start = 0;
int end = arr.length - 1;
int mid;
while (start <= end) {
mid = (start + end) / 2;
if (arr[mid] < target) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return start;
}
static class YoggaebReader {
private BufferedReader br;
private StringTokenizer st;
public YoggaebReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
'알고리즘(Java) > Brute-Force' 카테고리의 다른 글
[백준, Brute-Force, Java] P.2470 두 용액 (0) | 2021.10.14 |
---|---|
[백준, Brute-Force, Java] P.9663 n-queen (0) | 2021.09.30 |
[백준, Brute-Force, Java] P.1182 부분수열의 합 (0) | 2021.09.30 |
Comments