gpt4 book ai didi

java - 什么是 map[(int)x]++; 它有什么作用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:39:13 25 4
gpt4 key购买 nike

嘿,我参加了 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

最佳答案

map[(int)x]++ 与以下内容相同:

int index = (int) x;
map[index]++;

关于java - 什么是 map[(int)x]++; 它有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14716615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com