嘿,我参加了 Facebook 黑客竞赛,我得到了这个问题的解决方案。我以 O(K^2) 的顺序解决了这个问题,但这个人在 O(^K) 中解决了这个问题。请解释一下代码在做什么。
import java.io.File;
import java.util.Scanner;
public class FindTheMin {
private long getNextMin(long[] map, long start, long k) {
while (map[(int)(start)] > 0)
start++;
return start;
}
public long findNth(long n, long k, long a, long b, long c, long r) {
long[] cache = new long[100010];
long[] map = new long[100010];
long pre = a;
for (int i = 0; i < k; i++) {
long num;
if (i == 0)
num = a;
else
num = (b * pre + c) % r;
cache[i] = num;
if (num <= k + 1)
map[(int) num]++;
pre = num;
}
pre = getNextMin(map, 0, k);
cache[(int)k] = pre;
map[(int)pre]++;
for (int i = 0; i <= (int)k; i++) {
long deque = cache[i];
if (deque > k) {
long x = getNextMin(map, pre, k);
cache[i] = x;
map[(int)x]++;
pre = x;
} else {
if (deque < pre) {
if(map[(int)deque] == 1) {
cache[i] = deque;
pre = deque;
continue;
}
}
map[(int)deque]--;
long x = getNextMin(map, pre, k);
cache[i] = x;
pre = x;
map[(int)x]++;
}
}
return cache[(int)((n - 1) % (k + 1))];
}
public static void main(String[] args) {
try {
Scanner s = new Scanner(new File("in.txt"));
int m = s.nextInt();
FindTheMin f = new FindTheMin();
for (int i = 1; i <= m; i++) {
int n, k, a, b, c, r;
n = s.nextInt();
k = s.nextInt();
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
r = s.nextInt();
System.out.println("Case #" + i + ": " + f.findNth(n, k, a, b, c, r));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
学习这样的东西的最佳方法是什么。学习java最好的方法?这是问题 https://www.facebook.com/hackercup/problems.php?pid=494433657264959&round=185564241586420