main.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "sort"
  7. )
  8. // IntSlice ...
  9. type IntSlice []int
  10. func (a IntSlice) Len() int { return len(a) }
  11. func (a IntSlice) Less(i, j int) bool { return a[i] > a[j] }
  12. func (a IntSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  13. func leastPress(scanner *bufio.Scanner) []int {
  14. caseCnt := ReadInt(scanner)
  15. answer := make([]int, caseCnt)
  16. for cid := 0; cid < caseCnt; cid++ {
  17. params := ReadInts(scanner)
  18. // P: maximum cnt to place; K: key cnt; L: alphabet letter cnt.
  19. P, K, L := params[0], params[1], params[2]
  20. var freq IntSlice = ReadInts(scanner)
  21. sort.Sort(freq)
  22. for p, i := 0, 0; p < P && i < L; p++ {
  23. for j := 0; j < K && i < L; i, j = i+1, j+1 {
  24. answer[cid] += (p + 1) * freq[i]
  25. }
  26. }
  27. }
  28. return answer
  29. }
  30. func main() {
  31. inputFiles := []string{"A-small-practice.in", "A-large-practice.in", "test.in"}
  32. outputFiles := []string{"result-small.out", "result-large.out", "test.out"}
  33. const (
  34. small = iota
  35. large
  36. test
  37. )
  38. fileType := test
  39. fin, _ := os.Open(inputFiles[fileType])
  40. defer fin.Close()
  41. scanner := bufio.NewScanner(fin)
  42. answer := leastPress(scanner)
  43. fout, _ := os.Create(outputFiles[fileType])
  44. defer fout.Close()
  45. for i, v := range answer {
  46. s := fmt.Sprintf("Case #%d: %d\n", i+1, v)
  47. fout.WriteString(s)
  48. }
  49. }