N개의 장미를 가장 싸게 구매할 수 있는 금액을 구하는 문제이다. 입력값의 범위가 크므로 DP나 완전탐색 등은 불가능하다. 문제 해결 아이디어는 아래와 같다.
시간 복잡도 : O(c)(c: 특정 상수)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
long n = Long.parseLong(st.nextToken());
long a = Long.parseLong(st.nextToken());
long b = Long.parseLong(st.nextToken());
long c = Long.parseLong(st.nextToken());
long d = Long.parseLong(st.nextToken());
long result = Long.MAX_VALUE;
if (b * c < d * a) {
long temp;
temp = a;
a = c;
c = temp;
temp = b;
b = d;
d = temp;
}
for (int i = 0; i < c; ++i) {
long index = (long) Math.ceil((double) (n - i * a) / c);
if (index < 0) {
index = 0;
}
result = Math.min(result, i * b + index * d);
if (index == 0) {
break;
}
}
bw.write(result + "\n");
bw.close();
}
}