Problem solution · Go

Codeforces 1996F — Bomb

Codeforces 1996F — Bomb: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
42 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Codeforces 1996F — Bomb, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 42 lines of Go from the credited upstream file 1996F.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 1996F — Bomb · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io"	"sort") func cf1996F(in io.Reader, out io.Writer) {	var T, n, k, ans int	for Fscan(in, &T); T > 0; T-- {		Fscan(in, &n, &k)		a := make([]struct{ a, b int }, n)		for i := range a {			Fscan(in, &a[i].a)		}		for i := range a {			Fscan(in, &a[i].b)		}		sort.Search(1e9+1, func(mx int) bool {			k, s := k, 0			for _, p := range a {				if p.a > mx {					t := (p.a-mx-1)/p.b + 1					k -= t					s += (p.a*2 - (t-1)*p.b) * t / 2				}			}			if k < 0 {				return false			}			// 二分结束后,剩余的 k 次操作,每次操作的得分一定都恰好等于 mx			// 反证法:如果操作若干次后,后续的操作只能得到 < mx 的分数,那么二分结果必然 < mx,矛盾			ans = s + mx*k			return true		})		Fprintln(out, ans)	}} //func main() { cf1996F(bufio.NewReader(os.Stdin), os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗