搬运礼物

来源:互联网 发布:淘宝助理要收费 吗 编辑:程序博客网 时间:2024/04/29 13:15

http://codevs.cn/problem/3409/
n个礼物分K次搬运,一个搬a个价值W【A】 求最小价值。
一开始认为,将所有情况考虑出来:将N分为a1*num1+a2*num2+a3*num3……=n
min=min(min,num1*w[num1]+num2*w[num2]….);
再仔细一想纯粹扯淡,也没啥好的想法。
记忆化DP,从前面开始,将前面的最好的状态考虑好,慢慢向后走。

#include<iostream>#include<stdio.h>#include<cmath>#include<algorithm>using namespace std;int main(){    int a[5005] = { 0 };    int b[5005] = { 0 };    int n;    cin >> n;    for (int i = 1; i < n+1; i++)    {        scanf_s("%d",&a[i]);        b[i] = 999999;    }    b[1] = a[1];    for (int i = 2; i < n+1; i++)    {        for (int j = 1; j <= i ; j++)            b[i] = min(b[i], b[i - j] + a[j]);    }    cout << b[n] << endl;    system("pause");    return 0;}
0 0
原创粉丝点击