动态规划之钢条分割
来源:互联网 发布:apache.exe 编辑:程序博客网 时间:2024/05/17 01:34
问题描述:
p(n): p[1],p[2],.....,p[n]表示钢条长度为1,2,.....,n时的价格。给定length长度的钢条如何分割或者不分割整条卖才能得到最大收益。
#include <iostream>#include<vector>#include<algorithm>using namespace std;int top_to_bottom(int *price,int * max_value,int n){ if(n==0)return max_value[0]=0; if(max_value[n]>0)return max_value[n]; int q=-10; int i,pos; for( i=1;i<=n;i++){ int temp=price[i]+top_to_bottom(price,max_value,n-i); if(temp>q){ q=temp; pos=i; } } max_value[n]=q; return q; //splits.push_back(pos);}int bottom_up(int *price,int *max_value,int len,int *splits){ int i,j; for(i=1;i<=len;i++){ int q=-10; for(j=1;j<=i;j++){ if(price[j]+price[i-j]>q){ q=price[j]+price[i-j]; splits[i]=j; } } max_value[i]=q; } return max_value[len];}void print_splits(int *splits,int len){ //打印分割点 while(len>0){ cout<<splits[len]<<endl; len-=splits[len]; } return;}int main(){ int n;//价格表长度 cin>>n; int len;//钢条长度 cin>>len; int *price=new int [n]; int *max_value=new int[n]; int *splits=new int[len]; price[0]=0; for(int i=1;i<=n;i++){ cin>>price[i]; max_value[i]=0; } //vector<int> splits; int price_sum=top_to_bottom(price,max_value,len);//自顶向下递归算法计算最大收益,没办法得出分割点 cout<<price_sum<<endl; int price_bottom_up=bottom_up(price,max_value, len,splits);//自底向上递归算法计算最大收益和得出分割点。 cout<<price_bottom_up<<endl; print_splits(splits,len); //cout << "Hello world!" << endl; return 0;}
0 0
- 动态规划之钢条分割
- 动态规划之钢条分割
- 动态规划求解钢条分割问题
- 动态规划之切割钢条
- 动态规划之钢条切割
- 动态规划之钢条切割
- 动态规划之钢条切割
- 动态规划之钢条切割
- 动态规划之钢条切割
- 动态规划之切割钢条
- 动态规划之钢条切割
- 动态规划之钢条 切割
- 动态规划之切割钢条
- 动态规划之钢条切割
- 动态规划之钢条问题
- 动态规划问题之钢条切割
- 动态规划之钢条切割问题
- 动态规划之钢条切割问题
- 有关tomcat命令,注册tomcat
- android横竖屏处理
- 蓝桥杯联系系统算法训练——比较大小
- POJ1703简单的并查集
- 【JZOJ 3636】【BOI2012】Mobile
- 动态规划之钢条分割
- Linux下的date命令
- hdu--1495
- 蓝桥杯练习系统算法训练——最小公倍数
- 如何写一个随机洗牌函数
- SpringMVC ---请求过程分析
- jenkins持续集成配置
- xxx的纠错(1)
- 深入浅出Redis-Spring整合Redis