1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package main
- import (
- "bufio"
- "fmt"
- "os"
- "sort"
- )
- // IntSlice ...
- type IntSlice []int
- func (a IntSlice) Len() int { return len(a) }
- func (a IntSlice) Less(i, j int) bool { return a[i] > a[j] }
- func (a IntSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
- func leastPress(scanner *bufio.Scanner) []int {
- caseCnt := ReadInt(scanner)
- answer := make([]int, caseCnt)
- for cid := 0; cid < caseCnt; cid++ {
- params := ReadInts(scanner)
- // P: maximum cnt to place; K: key cnt; L: alphabet letter cnt.
- P, K, L := params[0], params[1], params[2]
- var freq IntSlice = ReadInts(scanner)
- sort.Sort(freq)
- for p, i := 0, 0; p < P && i < L; p++ {
- for j := 0; j < K && i < L; i, j = i+1, j+1 {
- answer[cid] += (p + 1) * freq[i]
- }
- }
- }
- return answer
- }
- func main() {
- inputFiles := []string{"A-small-practice.in", "A-large-practice.in", "test.in"}
- outputFiles := []string{"result-small.out", "result-large.out", "test.out"}
- const (
- small = iota
- large
- test
- )
- fileType := test
- fin, _ := os.Open(inputFiles[fileType])
- defer fin.Close()
- scanner := bufio.NewScanner(fin)
- answer := leastPress(scanner)
- fout, _ := os.Create(outputFiles[fileType])
- defer fout.Close()
- for i, v := range answer {
- s := fmt.Sprintf("Case #%d: %d\n", i+1, v)
- fout.WriteString(s)
- }
- }
|