技巧
二分
思路
对结果进行二分尝试
尽可能找出最大的满足条件的 x
实现
package main import ( "bufio" . "fmt" "io" "os" ) func checkNum(arr []int, num ,K int) bool { if num == 0 { return true } cnt := 0 for i := 0; i < len(arr); i++ { cnt += arr[i] / num if cnt >= K { return true } } return false } // https://ac.nowcoder.com/acm/problem/23049 func NC23049(_r io.Reader, _w io.Writer) { in, out := bufio.NewReader(_r), bufio.NewWriter(_w) defer out.Flush() var N,K int Fscan(in, &N, &K) arr := make([]int, N) for i := 0; i < N; i++ { Fscan(in, &arr[i]) } l, r := 0, int(^uint(0) >> 1) for l <= r { mid := l + (r - l) / 2 if checkNum(arr, mid, K) { l = mid + 1 }else { r = mid - 1 } } Fprintln(out, r) } func main() { NC23049(os.Stdin, os.Stdout) }