【动态规划】Order

woj里面遇到的一个动态规划问题,思路参考著名题目尼克的任务


Description
Knuthocean is a coach of WHUACM team now. One day, he wants to have a meeting with the team members, so he should order a classroom
for the meeting. Classrooms in WHU are lacking, so the administrator must accept some orders and refuse the others to optimize
the classroom’s use efficency. If one order is accepted, the time between the start time and the finish time of this order becomes active.
There can be at most one order during each time slice. Because of your capacity in programming, the administator asks you to
find a method to maximize the total active time. Your task is like that:read the orders and calculate the maximal length of the active time.


Input
Standard input will contain multiple test cases. For each test case, the first line is a number N,(N<=10000) followed by N lines.
Each line contains two integers, p and k.(1<=p<=k<=300000), where p is the start time and k is the finish time of an order.


Output
For each test case, you should output one line containing the maximal length of the active time.
Sample Input
4
1 2
3 5
1 4
4 5


Sample Output
5


Hint
Choose the order (1-2) and (3-5) and the length of the active time is 5 and is maximal.


Source
fp@WOJ


题目大意就是任意时间段只能执行一个任务,给出任务的起始时间点和结束时间点(左闭右闭),求最长的工作时间。


思路采用动态规划中的倒推法,假设长度为n,每个时间点i到n+1处工作的最长时间设为f[i],那么有f[n+1]=0。往前推导,对每一个f[i],有两种情况:

  1. 继承f[i+1],即不加入新的工作。
  2. 如果有某个工作(i,j),考虑倒推回f[j+1]的地方再加上这段工作。
    所以f[i]的推导公式为:
f[i] = max(f[i],f[i+t]+t);

最后得到的f[0](f[1])即为所求结果。


代码如下:

//
//  main.cpp
//  1072
//
//  Created by waple on 16/12/20.
//  Copyright © 2016年 waple. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

int f[300010];   //注意这里应该申明为全局变量,因为全局变量存储在数据段中,函数里的变量申请在堆栈段中,堆栈的存储空间远小于数据段,如果存储空间很大往往会报错。

int main(int argc, const char * argv[]) {
    int n;
    while (scanf("%d",&n)!=EOF) {
        vector<int> s[300010];   //使用向量存储工作任务信息
        int m = 0;
        //init
        for (int i = 0; i < n; i++) {
            int a,b;
            cin >> a >> b;
            m = max(m,b);    //m为最大长度
            s[a].push_back(b);
        }
        //cal
        f[m+1] = 0;
        for (int j = m; j >= 0; j--) {
            vector<int>::iterator it;
            f[j] = f[j+1];
            for (it = s[j].begin(); it != s[j].end(); it++) {
                f[j] = max(f[j],f[(*it)+1]+((*it)+1-j));
            }
        }
        printf("%d\n",f[0]);
    }
    return 0;
}

以上。

根据提供的引用内容,没有直接给出库存补货动态规划算法的Python实现。但是可以根据提供的信息,给出一个基于动态规划的库存管理算法的Python实现,供参考。 动态规划是一种解决多阶段决策过程最优化问题的方法。在库存管理问题中,我们可以将每个时间段看作一个阶段,每个阶段需要决策的是当前库存量和当前订单量,以及当前的需求量。我们需要在每个阶段决策当前的订货量,以使得总成本最小。 以下是一个基于动态规划的库存管理算法的Python实现: ```python def inventory_management(demand, holding_cost, shortage_cost, fixed_order_cost, order_cost, initial_inventory, max_inventory, num_periods): # 初始化状态和决策矩阵 state_matrix = [[(i, j) for j in range(max_inventory + 1)] for i in range(num_periods)] decision_matrix = [[0 for j in range(max_inventory + 1)] for i in range(num_periods)] # 初始化最终状态的值为0 final_state_value = [0 for i in range(max_inventory + 1)] # 从最后一个阶段开始向前递推 for t in range(num_periods - 1, -1, -1): for i in range(max_inventory + 1): # 计算当前状态下的最小成本 min_cost = float('inf') for j in range(max_inventory + 1): # 计算当前状态下的成本 if i + j >= demand[t]: cost = holding_cost * (i + j - demand[t]) + shortage_cost * (demand[t] - i - j) + order_cost * (j > 0) + fixed_order_cost * (j == initial_inventory) if t == num_periods - 1: # 如果是最后一个阶段,直接计算最小成本 if cost < min_cost: min_cost = cost decision_matrix[t][i] = j else: # 如果不是最后一个阶段,加上下一个阶段的最小成本 cost += final_state_value[j] if cost < min_cost: min_cost = cost decision_matrix[t][i] = j final_state_value[i] = min_cost # 返回决策矩阵和最小成本 return decision_matrix, final_state_value[initial_inventory] ``` 该算法的输入参数包括需求量、持有成本、缺货成本、固定订货成本、变动订货成本、初始库存量、最大库存量和时间段数。输出结果包括决策矩阵和最小成本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值