Problem solution · Go

Codeforces 725D — Contest Balloons

Codeforces 725D — Contest Balloons: 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
74 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 725D — Contest Balloons, 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

  • 74 lines of Go from the credited upstream file 725D.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 725D — Contest Balloons · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "container/heap"	. "fmt"	"io"	"sort") type int64Heap725 []int64 func (h int64Heap725) Len() int              { return len(h) }func (h int64Heap725) Less(i, j int) bool    { return h[i] < h[j] }func (h int64Heap725) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }func (h *int64Heap725) Push(v interface{})   { *h = append(*h, v.(int64)) }func (h *int64Heap725) Pop() (v interface{}) { n := len(*h); *h, v = (*h)[:n-1], (*h)[n-1]; return } // github.com/EndlessCheng/codeforces-gofunc CF725D(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush()	type team struct{ balls, weight int64 } 	var n, ans int	var myBalls, w, gives int64	Fscan(in, &n, &myBalls, &w)	ts := make([]team, n-1, n)	for i := range ts {		Fscan(in, &ts[i].balls, &ts[i].weight)	}	sort.Slice(ts, func(i, j int) bool { return ts[i].balls > ts[j].balls })	ts = append(ts, team{}) 	for _, t := range ts {		if b := t.balls; b <= myBalls {			gives += myBalls - b			myBalls = b			break		}		ans++	}	h := &int64Heap725{}	for i := 0; ts[i].balls > 0; {		for ; ts[i].balls > myBalls; i++ {			Push(h, ts[i].weight-ts[i].balls+1)		}		for len(*h) > 0 {			d := (*h)[0]			if d > gives {				break			}			Pop(h)			gives -= d		}		if len(*h) < ans {			ans = len(*h)		}		for _, t := range ts[i:] {			if b := t.balls; b < myBalls {				gives += myBalls - b				myBalls = b				break			}		}	}	Fprint(out, ans+1)} //func main() {//	CF725D(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 ↗