문제
https://level.goorm.io/exam/195697/%EA%B3%BC%EC%9D%BC-%EA%B5%AC%EB%A7%A4/quiz/1
구름LEVEL
난이도별 다양한 문제를 해결함으로써 SW 역량을 향상시킬 수 있습니다.
level.goorm.io
풀이
이 문제 같은 경우 과일을 나누어 살 수 있다고 문제에 제시되어 있다.
조각 과일의 가격이 1일때의 포만감인 C/P이 된다.
이 포만감을 가지고 큰 포만감 순으로 정렬을 한다.
과일의 가격이 K보다 작거나 같으면 (C/P * 과일 가격) 이 포만감이 될것이다.
만약 과일의 가격이 K보다 크다면 이는 조각난 과일을 선택해야 한다. 따라서 (C/P * K)가 포만감이 될것이다.
코드
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int p [] = new int [n];
int c [] = new int [n];
List<Info> list = new ArrayList<>();
for(int i=0;i<n;i++){
st = new StringTokenizer(br.readLine());
p[i] = Integer.parseInt(st.nextToken());
c[i] = Integer.parseInt(st.nextToken())/p[i];
list.add(new Info(p[i], c[i]));
}
Collections.sort(list);
long result = 0;
for(int i=0;i<n;i++){
Info info = list.get(i);
int buy = Math.min(k, info.p);
k = k-buy;
result += info.c*buy;
}
System.out.println(result);
}
static class Info implements Comparable<Info>{
int p;
int c;
public Info (int p, int c){
this.p = p;
this.c = c;
}
public int compareTo(Info o){
if(this.c == o.c){
return o.p - this.p;
}
return o.c - this.c;
}
}
}
문제
https://level.goorm.io/exam/195697/%EA%B3%BC%EC%9D%BC-%EA%B5%AC%EB%A7%A4/quiz/1
구름LEVEL
난이도별 다양한 문제를 해결함으로써 SW 역량을 향상시킬 수 있습니다.
level.goorm.io
풀이
이 문제 같은 경우 과일을 나누어 살 수 있다고 문제에 제시되어 있다.
조각 과일의 가격이 1일때의 포만감인 C/P이 된다.
이 포만감을 가지고 큰 포만감 순으로 정렬을 한다.
과일의 가격이 K보다 작거나 같으면 (C/P * 과일 가격) 이 포만감이 될것이다.
만약 과일의 가격이 K보다 크다면 이는 조각난 과일을 선택해야 한다. 따라서 (C/P * K)가 포만감이 될것이다.
코드
import java.io.*; import java.util.*; class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int p [] = new int [n]; int c [] = new int [n]; List<Info> list = new ArrayList<>(); for(int i=0;i<n;i++){ st = new StringTokenizer(br.readLine()); p[i] = Integer.parseInt(st.nextToken()); c[i] = Integer.parseInt(st.nextToken())/p[i]; list.add(new Info(p[i], c[i])); } Collections.sort(list); long result = 0; for(int i=0;i<n;i++){ Info info = list.get(i); int buy = Math.min(k, info.p); k = k-buy; result += info.c*buy; } System.out.println(result); } static class Info implements Comparable<Info>{ int p; int c; public Info (int p, int c){ this.p = p; this.c = c; } public int compareTo(Info o){ if(this.c == o.c){ return o.p - this.p; } return o.c - this.c; } } }