Advertisement
Catsher

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

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