이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
import java.util.*;
class jelly {
int find_maximum_unique(int x, int y, int[] a, int[] b) {
int N = a.length;
int[][] vals = new int[N][2];
for (int i = 0; i < N; i++)
vals[i] = new int[] {a[i],b[i]};
sort(vals);
long[] dpX = new long[x+1];
long[] budget = new long[N];
for (int i = 0; i < N; i++) {
long[] dpXnew = new long[x+1];
for (int j = 0; j <= x; j++) {
//buy in shop2
dpXnew[j] = dpX[j]+vals[i][1];
//buy in shop1
if (j >= vals[i][0])
dpXnew[j] = Math.min(dpXnew[j],dpX[j-vals[i][0]]);
}
dpX = dpXnew;
budget[i] = y-dpX[x];
}
int ans = 0;
int[] dpY = new int[y+1];
for (int i = N-1; i >= 0; i--) {
if (budget[i] >= 0)
ans = Math.max(ans, i+1+dpY[(int)budget[i]]);
int[] dpYnew = new int[y+1];
for (int j = 0; j <= y; j++) {
//dont take this jelly
dpYnew[j] = dpY[j];
//take this jelly
if (j >= vals[i][1])
dpYnew[j] = Math.max(dpYnew[j], dpY[j-vals[i][1]]+1);
}
dpY = dpYnew;
}
ans = Math.max(ans, dpY[y]);
return ans;
}
void sort(int[][] arr) {
//Sort an array (immune to quicksort TLE)
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int randomPosition = rgen.nextInt(arr.length);
int[] temp = arr[i];
arr[i] = arr[randomPosition];
arr[randomPosition] = temp;
}
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] arr1, int[] arr2) {
return arr1[0]-arr2[0];
//Ascending order.
}
});
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |