package main
import (
"fmt"
"sort"
)
type Moon struct {
stock float64
totalPrice float64
unitPrice float64
}
type MoonSlice []Moon
func (s MoonSlice) Len() int { return len(s) }
func (s MoonSlice) Swap(i,j int) { s[i],s[j] = s[j],s[i] }
func (s MoonSlice) Less(i,j int) bool { return s[i].unitPrice > s[j].unitPrice }
func main(){
var N,Need int
var List [] Moon
var MaxSale float64 = 0
var NeedF float64 = 0
fmt.Scanf("%d %d",&N,&Need)
List = make([]Moon,N)
NeedF = float64(Need)
for i :=0 ;i < N;i ++{
fmt.Scanf("%f",&List[i].stock)
}
for i :=0 ;i < N;i ++ {
fmt.Scanf("%f",&List[i].totalPrice)
List[i].unitPrice = List[i].totalPrice / List[i].stock
}
sort.Sort(MoonSlice(List))
for i:=0;Need>0 && i<N;i++{
if List[i].stock > NeedF{
MaxSale += List[i].totalPrice *NeedF/ List[i].stock
Need = 0
}else if List[i].stock <= NeedF{
MaxSale += List[i].totalPrice
NeedF -= List[i].stock
}
}
fmt.Printf("%.2f",MaxSale)
}
主要运用到了Go的sort()函数,这个需要提前学习一下。