|
| 1 | +// https://www.nowcoder.com/practice/2e27509b990d4d02a70c0f208f078cdf |
| 2 | + |
| 3 | +const rl = require("readline").createInterface({ input: process.stdin }); |
| 4 | +var iter = rl[Symbol.asyncIterator](); |
| 5 | +const readline = async () => (await iter.next()).value; |
| 6 | + |
| 7 | +void async function () { |
| 8 | + let [n, m] = (await readline()).split(' ').map(it => Number(it)) |
| 9 | + const str = await readline() |
| 10 | + const Ws = [] |
| 11 | + let idx = str.indexOf('W', 0) |
| 12 | + while (idx !== -1) { |
| 13 | + Ws.push(idx); |
| 14 | + idx = str.indexOf('W', idx + 1); |
| 15 | + } |
| 16 | + |
| 17 | + if (!Ws.length) { |
| 18 | + console.log(0) // 注意特殊情况 |
| 19 | + return |
| 20 | + } |
| 21 | + function check(k) { |
| 22 | + let idx = 0 // 当前已经覆盖第 i 个 W |
| 23 | + let cnt = 0 // 已经使用了刷的次数 |
| 24 | + while (idx < Ws.length) { |
| 25 | + cnt++ |
| 26 | + const curLast = Ws[idx] + k - 1 |
| 27 | + while (idx < Ws.length && Ws[idx] <= curLast) // 覆盖了的 |
| 28 | + idx++ |
| 29 | + } |
| 30 | + return cnt <= m |
| 31 | + } |
| 32 | + let l = 1 |
| 33 | + let r = n |
| 34 | + while (l <= r) { |
| 35 | + const k = Math.floor((l + r) / 2) |
| 36 | + if (check(k)) { |
| 37 | + r = k - 1 // 尝试更小 |
| 38 | + } else { |
| 39 | + l = k + 1 |
| 40 | + } |
| 41 | + } |
| 42 | + console.log(l) |
| 43 | +}() |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +// 复杂度过高 |
| 48 | +const rl = require("readline").createInterface({ input: process.stdin }); |
| 49 | +var iter = rl[Symbol.asyncIterator](); |
| 50 | +const readline = async () => (await iter.next()).value; |
| 51 | + |
| 52 | +void async function () { |
| 53 | + // 首先假如 m 只有一次,那么就是看 W 出现的两个极端位置 |
| 54 | + // 如果 m = 2 那么进入 W 极端位置中间找最靠近中间的 |
| 55 | + // m = 3 再进入 |
| 56 | + let [n, m] = (await readline()).split(' ').map(it => Number(it)) |
| 57 | + const str = await readline() |
| 58 | + const Ws = [] |
| 59 | + let idx = str.indexOf('W', 0) |
| 60 | + while (idx !== -1) { |
| 61 | + Ws.push(idx); |
| 62 | + idx = str.indexOf('W', idx + 1); |
| 63 | + } |
| 64 | + // console.log(Ws) |
| 65 | + let k = Infinity; |
| 66 | + // let l = 0 |
| 67 | + // let r = n-1 |
| 68 | + // while(str[l]!=='W') |
| 69 | + // l++ |
| 70 | + // while(str[r]!=='W') |
| 71 | + // r-- |
| 72 | + m -= 1 |
| 73 | + function dfs(m, l, r) { |
| 74 | + if (m == 0) { |
| 75 | + k = Math.min(k, Ws[r] - Ws[l] + 1) |
| 76 | + return |
| 77 | + } |
| 78 | + const mid = Math.floor((l + r) / 2) |
| 79 | + // let d = 0 |
| 80 | + // while(str[mid+d]!=='W' && str[mid+d]!=='W') |
| 81 | + // d++ |
| 82 | + dfs(m - 1, l, mid) |
| 83 | + dfs(m - 1, mid, r) |
| 84 | + } |
| 85 | + dfs(m, 0, Ws.length - 1) |
| 86 | + // while(m){ |
| 87 | + // const mid = Math.floor((l+r)/2) |
| 88 | + // let d = 0 |
| 89 | + // while(str[mid+d]!=='W') |
| 90 | + // m-- |
| 91 | + // } |
| 92 | + console.log(k) |
| 93 | +}() |
0 commit comments