publicstaticvoidsolve() { intn= io.nextInt(), h = io.nextInt(), x = io.nextInt(); int[] p = newint[n]; for (inti=0; i < n; i++) { p[i] = io.nextInt(); } x = x - h;
intlo=0, hi = n - 1; while (lo <= hi) { intmid= lo + (hi - lo) / 2; if (p[mid] < x) lo = mid + 1; else hi = mid - 1; } io.println(lo + 1); }
publicstaticvoidsolve() { intn= io.nextInt(), m = io.nextInt();
List<int[]>[] g = newList[n]; Arrays.setAll(g, k -> newArrayList<>()); for (inti=0; i < m; i++) { intu= io.nextInt() - 1, v = io.nextInt() - 1, w = io.nextInt(); g[u].add(newint[]{v, w}); g[v].add(newint[]{u, w}); }
boolean[] vis = newboolean[n]; for (inti=0; i < n; i++) { dfs(i, 0, g, vis); } io.println(ans); }
privatestaticvoiddfs(int x, int dis, List<int[]>[] g, boolean[] vis) { vis[x] = true; ans = Math.max(ans, dis); for (int[] t : g[x]) { inty= t[0], w = t[1]; if (!vis[y]) { dfs(y, dis + w, g, vis); } } vis[x] = false; }
publicstaticvoidsolve() { intn= io.nextInt(), m = io.nextInt(), d = io.nextInt(); int[] a = newint[n]; for (inti=0; i < n; i++) a[i] = io.nextInt(); PriorityQueue<Integer> q = newPriorityQueue<>(); longsum=0L, ans = 0L; for (inti=0; i < n; i++) { if (a[i] <= 0) continue; q.offer(a[i]); sum += a[i]; if (q.size() > m) sum -= q.poll(); ans = Math.max(ans, sum - (long) d * (i + 1)); } io.println(ans); }
const map<int,int> m; cout << m[1024]; // 错误,No viable overloaded operator[] for type 'const map<int, int>' cout << m.at(1024); // 正确
② = 拷贝对象的底层结构,不像 Java 中拷贝的是对象的地址(相当于 C++ 中的指针吧)。
1 2 3 4 5
map<int,int> m; m[1024] = 1024; auto n = m; n[1024] = 2048; cout << m[1024]; // 输出:1024
③ 在 Java 中只要是对象就可以和 null 比较,而 C++ 中只有指针可以和 nullptr 比较。
GDB
① 使用 GDB 调试经常会看到 Python Exception <class ‘gdb.error’>: There is no member named _M_p,点击此处产生该问题的原因,以及相应的解决方案告诉我下载 libstdc++6-dbgsym,完美解决问题。本来不想管这个问题的,结果任务三需要在调试时打印字符串。