Problem solution · Go

Codeforces 1684C — Column Swapping

Codeforces 1684C — Column Swapping: a Go solution using sorting and greedy selection. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Sorting and greedy selection
Source
EndlessCheng Codeforces Go
Length
126 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

Sorting and greedy selection

For Codeforces 1684C — Column Swapping, the implementation first exposes a useful order, then scans that order while making locally justified choices.

  1. Choose the key that reveals the greedy or grouping structure.
  2. Sort the relevant records by that key.
  3. Scan in order, maintaining the invariant that makes each local choice safe.

Code notes

  • 126 lines of Go from the credited upstream file 1684C.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.

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 1684C — Column Swapping · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"sort") // github.com/EndlessCheng/codeforces-gofunc CF1684C(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush() 	var T, n, m into:	for Fscan(in, &T); T > 0; T-- {		Fscan(in, &n, &m)		k := -1		a := make([][]int, n)		for i := range a {			a[i] = make([]int, m)			for j := range a[i] {				Fscan(in, &a[i][j])			}			if !sort.IntsAreSorted(a[i]) {				k = i			}		}		if k < 0 {			Fprintln(out, 1, 1)			continue		}		b := append([]int{}, a[k]...)		sort.Ints(b)		ps := []int{}		for j, v := range a[k] {			if v != b[j] {				if len(ps) == 2 {					Fprintln(out, -1)					continue o				}				ps = append(ps, j)			}		}		i, j := ps[0], ps[1]		for _, r := range a {			r[i], r[j] = r[j], r[i]			if !sort.IntsAreSorted(r) {				Fprintln(out, -1)				continue o			}		}		Fprintln(out, i+1, j+1)	}} // 我的憨憨写法(虽然是线性的,但是明显排序更优雅)func CF1684C2(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush() 	var T, n, m into:	for Fscan(in, &T); T > 0; T-- {		Fscan(in, &n, &m)		k := -1		a := make([][]int, n)		for i := range a {			a[i] = make([]int, m)			for j := range a[i] {				Fscan(in, &a[i][j])			}			if !sort.IntsAreSorted(a[i]) {				k = i			}		}		if k < 0 {			Fprintln(out, 1, 1)			continue		}		ps := []int{}		for j := 1; j < m; j++ {			if a[k][j-1] > a[k][j] {				if len(ps) == 2 {					Fprintln(out, -1)					continue o				}				ps = append(ps, j)			}		}		var i, j int		if len(ps) == 1 {			j = ps[0]			i = sort.SearchInts(a[k][:j], a[k][j]+1)			for _, r := range a {				r[i], r[j] = r[j], r[i]				if !sort.IntsAreSorted(r) {					r[i], r[j] = r[j], r[i]					goto next				}				r[i], r[j] = r[j], r[i]			}			Fprintln(out, i+1, j+1)			continue		next:			i = ps[0] - 1			j = i + sort.SearchInts(a[k][i+1:], a[k][i])		} else {			i, j = ps[0]-1, ps[1]		}		for _, r := range a {			r[i], r[j] = r[j], r[i]			if !sort.IntsAreSorted(r) {				Fprintln(out, -1)				continue o			}		}		Fprintln(out, i+1, j+1)	}} //func main() { CF1684C(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 ↗