publicstaticvoidsolve() { intn= io.nextInt(), m = io.nextInt(); int[] a = newint[n]; for (inti=1; i < n; i++) { a[i] = io.nextInt(); } int[] b = newint[n]; for (inti=0; i < n; i++) { b[i] = io.nextInt(); } Arrays.sort(b);
intk= calc(a, b, 1);
intlo=1, hi = m; while (lo <= hi) { intmid= lo + (hi - lo) / 2; if (calc(a, b, mid) == k) lo = mid + 1; else hi = mid - 1; } io.println((long) m * k + (m - lo + 1)); }
privatestaticintcalc(int[] a, int[] b, int x) { a[0] = x; a = a.clone(); Arrays.sort(a);
intn= a.length, k = 0; for (inti=0, j = 0; i < n - k; i++, j++) { while (j < n && a[i] >= b[j]) { k++; j++; } } return k; }
publicstaticvoidsolve() { intn= io.nextInt(), m = io.nextInt(); int[] a = newint[n]; a[0] = Integer.MAX_VALUE; for (inti=1; i < n; i++) { a[i] = io.nextInt(); } int[] b = newint[n]; for (inti=0; i < n; i++) { b[i] = io.nextInt(); }
Arrays.sort(a); Arrays.sort(b);
intk=0, val = m + 1; for (inti=0, j = 0; i < n - k; i++, j++) { while (j < n && a[i] >= b[j]) { val = b[j]; k++; j++; } } io.println((long) m * (k - 1) + Math.max(0, m - val + 1)); }
publicstaticvoidsolve() { intn= io.nextInt(); char[] s = io.next().toCharArray();
longans=0L; intl= n - 1, r = n - 1; for (; l >= 0; l--) { if (s[l] == '0') { ans += r - l; r--; io.print(ans + " "); } } io.println("-1 ".repeat(r + 1)); }
classSolution { publicintminimumChanges(String s, int k) { intn= s.length();
int[][] change = newint[n][n]; for (inti=0; i < n - 1; i++) { for (intj= i + 1; j < n; j++) { change[i][j] = calc(s.substring(i, j + 1)); } }
int[][] dp = newint[n][k]; for (inti=0; i < n; i++) { Arrays.fill(dp[i], -1); } return dfs(0, s, n, k - 1, change, dp); }
privateintcalc(String s) { intn= s.length(), res = n; for (intd=1; d < n; d++) { if (n % d == 0) { intcnt=0; for (intk=0; k < d; k++) { for (inti= k, j = n - d + k; i < j; i += d, j -= d) { if (s.charAt(i) != s.charAt(j)) { cnt++; } } } res = Math.min(res, cnt); } } return res; }
privateintdfs(int i, String s, int n, int k, int[][] change, int[][] dp) { if (k == 0) { return change[i][n - 1]; }
if (dp[i][k] != -1) { return dp[i][k]; }
intres= n; for (intj= i + 1; j < n - 2 * k; j++) { res = Math.min(res, dfs(j + 1, s, n, k - 1, change, dp) + change[i][j]); } return dp[i][k] = res; } }