main.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "sort"
  7. )
  8. func minProduct(scanner *bufio.Scanner) []int64 {
  9. caseCnt := ReadInt(scanner) // Math AGAIN.
  10. answer := make([]int64, caseCnt)
  11. for i := 0; i < caseCnt; i++ {
  12. // n = 2 is a very impotant case, because:
  13. // if x < x', y < y', then xy'+x'y - xy - x'y'
  14. // = -(x' - x)(y' - y) < 0, so xy'+x'y < xy + x'y'
  15. vecDim := ReadInt(scanner)
  16. vec1 := ReadInts(scanner)
  17. vec2 := ReadInts(scanner)
  18. sort.Ints(vec1)
  19. sort.Ints(vec2)
  20. for j := 0; j < vecDim; j++ {
  21. answer[i] += int64(vec1[j] * vec2[vecDim-1-j])
  22. }
  23. }
  24. return answer
  25. }
  26. func main() {
  27. inputFiles := []string{"A-small-practice.in", "A-large-practice.in", "test.in"}
  28. outputFiles := []string{"result-small.out", "result-large.out", "test.out"}
  29. const (
  30. small = iota
  31. large
  32. test
  33. )
  34. fileType := large
  35. fin, _ := os.Open(inputFiles[fileType])
  36. defer fin.Close()
  37. scanner := bufio.NewScanner(fin)
  38. answer := minProduct(scanner)
  39. fout, _ := os.Create(outputFiles[fileType])
  40. defer fout.Close()
  41. for i, n := range answer {
  42. s := fmt.Sprintf("Case #%d: %d\n", i+1, n)
  43. fout.WriteString(s)
  44. }
  45. }