Today, let's take a look at the second arithmetic problem of the 2009 Spring Festival Recruitment of the American Troupe.
image
First of all, we should analyze the rules of the topic.
According to the meaning of the question, the four adjacent elements of each lattice e must be equal, and not equal to E.
We define a concept: when the sum of the transverse and longitudinal coordinates of a lattice is odd, we call it an odd lattice, and when the sum of the transverse and longitudinal coordinates of a lattice is even, we call it an even lattice.
We can find that for a matrix satisfying the problem meaning, the number on all even lattices is equal, the number on all odd lattices is equal, and the number on odd lattices is not equal to the number on even lattices.
If the total number of modified numbers is required to be the least, which is equivalent to converting them into the numbers with the largest number of occurrences, we can decompose them into odd lattices with the largest number of occurrences, and even lattices with the largest number of occurrences.
Of course, we must also ensure that the number reserved on odd lattices is not equal to the number reserved on even lattices.
We agreed to mark the number of occurrences of Ni as (ni, ci).
We consider the most general case, that is, there are at least two different numbers on odd lattices and at least two different numbers on even lattices.
We rank the numbers on odd lattices from large to small, and get (n1, c1), (n2, c2)...
We rank the numbers on even lattices from large to small, and get (m1, d1), (m2, d2)...
If n1!= m1, then reserving N1 and M1 must be the optimal solution; if n1= m1, there are two alternatives, one is to reserve N1 and m2, the other is to reserve n2 and m1. In short, the optimal solution must be generated by reserving N1 and m1, N1 and m2 or n2 and m1.
But it will increase the complexity when we write code. In the code, we can directly double loop to determine the number of reserved digits when n1 and m1, n1 and m2, n2 and m1, n2 and M2 are retained, and then we can take the maximum value. The case of retaining n2 and M2 is often considered in the double cycle, and one case does not affect the final result.
Upper golang code:
package main import ( "bufio" "fmt" "os" "sort" "strconv" "strings" ) type pair struct { num int cnt int } type PairList []pair func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p PairList) Len() int { return len(p) } func (p PairList) Less(i, j int) bool { return p[i].cnt > p[j].cnt } func main() { nr := bufio.NewReader(os.Stdin) nm, _ := nr.ReadString('\n') nm = strings.Trim(nm, "\r\n") n, _ := strconv.Atoi(strings.Split(nm, " ")[0]) m, _ := strconv.Atoi(strings.Split(nm, " ")[1]) odde := make(map[int]int) //Statistics of the number of occurrences of numbers on odd-numbered lattices evene := make(map[int]int)//Statistics of the number of occurrences of numbers on even-numbered lattices for i := 0; i < n; i++ { str, _ := nr.ReadString('\n') str = strings.Trim(str, "\r\n") line := strings.Split(str, " ") for j := 0; j < m; j++ { e, _ := strconv.Atoi(line[j]) if (i+j)%2 == 0 { cnt, ok := odde[e] if !ok { odde[e] = 1 } else { odde[e] = cnt + 1 } } else { cnt, ok := evene[e] if !ok { evene[e] = 1 } else { evene[e] = cnt + 1 } } } } oddlist := make(PairList, 0, len(odde)) for n, c := range odde { oddlist = append(oddlist, pair{n, c}) // Encapsulate kv pairs in map into pair structure for sorting } sort.Sort(oddlist) if len(oddlist) == 1 { oddlist = append(oddlist, pair{-1, 0}) } evenlist := make(PairList, 0, len(evene)) for n, c := range evene { evenlist = append(evenlist, pair{n, c}) } sort.Sort(evenlist) if len(evenlist) == 1 { evenlist = append(evenlist, pair{-1, 0}) } var retained int // Number of reserved figures, maximum for i := 0; i < 2; i++ { for j := 0; j < 2; j++ { if oddlist[i].num != evenlist[j].num { retained = max(retained, oddlist[i].cnt + evenlist[j].cnt) } } } fmt.Println(n*m - retained) } func max(a, b int) int { if a > b { return a } else { return b } }
There is one detail to note when implementing the code:
Because in the previous analysis, it is assumed that both n2 and m2 exist, but in fact n2 or m2 may not exist, that is, there is only one number on odd or even lattices.
So in 60 and 69 lines of code, you need to determine whether the list length is 1, and if so, fill in a pair{num:-1, cnt:0}.
It can be verified that this method is able to ensure accurate results, and submitted code is also ac:)
Welcome to pay attention to the blogger's personal Wechat Public Number~~~