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,完美解决问题。本来不想管这个问题的,结果任务三需要在调试时打印字符串。
publicstaticvoidsolve() { intm= io.nextInt(), k = io.nextInt(), a1 = io.nextInt(), ak = io.nextInt(); intans= Math.max(0, m % k - a1) + Math.max(0, m / k - ak - Math.max(0, a1 - m % k) / k); io.println(ans); }