https://www.acmicpc.net/problem/10025
10025번: 게으른 백곰
첫 줄에 정수 N과 K가 들어온다. 둘째 줄부터 N째 줄까지, 공백을 사이에 두고 각 양동이의 얼음의 양을 나타내는 gi와 양동이의 좌표를 나타내는 xi가 주어진다.
www.acmicpc.net
저는 투포인트를 이용해 문제를 해결하였습니다.
투포인트를 통해 범위의 가중치 합을 구하여 최대를 찾았습니다.
package BOJ.twopoint;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_10025 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int k = stoi.apply(st.nextToken());
List<int[]> ices = new ArrayList<>(n);
for(int i = 0 ; i < n ; i++){
st = new StringTokenizer(br.readLine());
int weight = stoi.apply(st.nextToken());
int position = stoi.apply(st.nextToken());
ices.add(new int[] {weight,position});
}
ices.sort((o1,o2)-> o1[1] - o2[1]);
int result = cal(ices,n,k);
System.out.println(result);
}
private static int cal(List<int[]> ices, int n,int k) {
int si = 0;
int ei = 0;
int distance = 2*k + 1;
int sum = 0;
int result = 0;
while(ei < n){
if(ices.get(ei)[1] - ices.get(si)[1] <= distance){
sum += ices.get(ei++)[0];
result = Math.max(result,sum);
}else{
sum -= ices.get(si++)[0];
}
}
return result;
}
}
'알고리즘' 카테고리의 다른 글
백준 24463번 미로 (JAVA) (0) | 2022.08.17 |
---|---|
백준 17404번 RGB거리2 (JAVA) (0) | 2022.08.16 |
백준 18353번 병사 배치하기 (JAVA) (0) | 2022.08.14 |
백준 22233번 가희와 키워드 (JAVA) (0) | 2022.08.13 |
백준 21921번 블로그 (JAVA) (0) | 2022.08.12 |