嘿,我參加了Facebook黑客大賽,我得到了這個問題的解決方案。 我按照O(K^2)的順序解決了這個問題,但是這個人在O(^ K)中解決了它。 請解釋一下代碼在做什麼。什麼是map [(int)x] ++;它做了什麼?
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
你是什麼意思** O(^ K)**? – MrSmith42
爲什麼標記[c]? – AnT
您可以請問我們這些誰更喜歡沒有Facebook帳戶的問題? – dasblinkenlight