Advertisement
Catsher

АлгосыЛаба1 - Задача

Mar 13th, 2025 (edited)
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.31 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. func last[T interface{}](array []T) T {
  8.     return array[len(array)-1]
  9. }
  10.  
  11. func countMaxProfit(X0 int, profits [][]int) (int, []int) {
  12.     orgsAmount := len(profits)                        // Кол-во орг
  13.     moneyVariantsAmount := (X0 / 40) + 1              // Кол-во распределения денег (от 0 до 6 (т.е. от 0 до 200 млн))
  14.     dp := make([][]int, orgsAmount+1)                 // Табличка самых выгодных вложений (табличка ДП)
  15.     allocationsOfMoney := make([][]int, orgsAmount+1) // А это сколько какой орге денег давали
  16.  
  17.     for i := 0; i < orgsAmount+1; i++ {
  18.         dp[i] = make([]int, moneyVariantsAmount)
  19.         allocationsOfMoney[i] = make([]int, moneyVariantsAmount)
  20.     }
  21.  
  22.     for currentOrgIndex := 1; currentOrgIndex <= orgsAmount; currentOrgIndex++ {
  23.         for availableMoney := 0; availableMoney < moneyVariantsAmount; availableMoney++ {
  24.             bestSum := 0
  25.             bestAllocation := 0
  26.             for currentMoneyForThisOrg := 0; currentMoneyForThisOrg <= availableMoney; currentMoneyForThisOrg++ {
  27.                 currentProfit := dp[currentOrgIndex-1][availableMoney-currentMoneyForThisOrg] + profits[currentOrgIndex-1][currentMoneyForThisOrg]
  28.                 if currentProfit > bestSum {
  29.                     bestSum = currentProfit
  30.                     bestAllocation = currentMoneyForThisOrg
  31.                 }
  32.             }
  33.             dp[currentOrgIndex][availableMoney] = bestSum
  34.             allocationsOfMoney[currentOrgIndex][availableMoney] = bestAllocation
  35.         }
  36.     }
  37.  
  38.     resultAllocation := make([]int, orgsAmount)
  39.     remainingAmount := moneyVariantsAmount - 1
  40.     for i := orgsAmount; i > 0; i-- {
  41.         resultAllocation[i-1] = allocationsOfMoney[i][remainingAmount]
  42.         remainingAmount -= allocationsOfMoney[i][remainingAmount]
  43.     }
  44.  
  45.     for i := 0; i < len(resultAllocation); i++ {
  46.         resultAllocation[i] *= 40
  47.     }
  48.  
  49.     return last(last(dp)), resultAllocation
  50. }
  51.  
  52. func main() {
  53.     allOurMoney := 200
  54.  
  55.     profits := [][]int{
  56.         {0, 8, 10, 11, 12, 18}, // f1(x)
  57.         {0, 6, 9, 11, 13, 15},  // f2(x)
  58.         {0, 3, 4, 7, 11, 18},   // f3(x)
  59.         {0, 4, 6, 8, 13, 16},   // f4(x)
  60.     }
  61.  
  62.     maxIncome, allocation := countMaxProfit(allOurMoney, profits)
  63.  
  64.     fmt.Printf("Максимальный доход: %d млн. руб.\n", maxIncome)
  65.     fmt.Println("Распределение средств (млн. руб.):", allocation)
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement