P2066 机器分配 (区间DP)

题意分析

总公司拥有高效设备M台,准备分给下属的N个分公司。各分公司若获得这些设备,可以为国家提供一定的盈利。问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值。其中M≤15,N≤10。分配原则:每个公司有权获得任意数目的设备,但总台数不超过设备数M。

区间DP

状态设计

dp[i][j]dp[i][j] 表示前i家公司分j台机器的最大的收益值。

状态转移

dp[i][j]=max(dp[i][j],dp[i1][jk]+income[i][

program lagrange_reconstruction implicit none integer, parameter :: dp = selected_real_kind(15, 300) integer :: N, i, j, num_points real(dp), dimension(:), allocatable :: x_nodes, y_nodes real(dp) :: a, b, h, x real(dp) :: exact, interpolated, error a = -5.0_dp b = 5.0_dp write(*,*) '请输入插值节点数 N :' read(*,*) N allocate(x_nodes(0:N), y_nodes(0:N)) do i = 0, N x_nodes(i) = a + (b - a) * i / N y_nodes(i) = f(x_nodes(i)) end do write(*,'(A)') 'x, exact_f(x), interpolated, error' num_points = 101 h = (b - a) / (num_points - 1) do i = 0, num_points - 1 x = a + i * h exact = f(x) call lagrange_interpolation(N, x_nodes, y_nodes, x, interpolated) error = abs(exact - interpolated) write(*,'(F6.2, 3E14.6)') x, exact, interpolated, error end do deallocate(x_nodes, y_nodes) pause contains function f(x) result(res) real(dp), intent(in) :: x real(dp) :: res res = 1.0_dp / (1.0_dp + x*x) end function f subroutine lagrange_interpolation(N, x_nodes, y_nodes, x, interpolated_value) integer, intent(in) :: N real(dp), dimension(0:N), intent(in) :: x_nodes, y_nodes real(dp), intent(in) :: x real(dp), intent(out) :: interpolated_value integer :: i, j real(dp) :: product, term interpolated_value = 0.0_dp do i = 0, N product = 1.0_dp do j = 0, N if (j /= i) then product = product * (x - x_nodes(j)) / (x_nodes(i) - x_nodes(j)) end if end do term = y_nodes(i) * product interpolated_value = interpolated_value + term end do end subroutine lagrange_interpolation end program lagrange_reconstruction代码解释
最新发布
10-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值