【动态规划专练-完全背包】CF189A (1300)

Cut Ribbon

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:

After the cutting each ribbon piece should have length a, b or c.
After the cutting the number of ribbon pieces should be maximum.
Help Polycarpus and find the number of ribbon pieces after the required cutting.

Input
The first line contains four space-separated integers n, a, b and c (1?≤?n,?a,?b,?c?≤?4000) — the length of the original ribbon and the acceptable lengths of the ribbon pieces after the cutting, correspondingly. The numbers a, b and c can coincide.

Output
Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists.

Examples
inputCopy
5 5 3 2
outputCopy
2
inputCopy
7 5 5 2
outputCopy
2
Note
In the first example Polycarpus can cut the ribbon in such way: the first piece has length 2, the second piece has length 3.

In the second example Polycarpus can cut the ribbon in such way: the first piece has length 5, the second piece has length 2.

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <queue>
#include <set>
#include<sstream>
#include <cstring>
#include <cmath>
#include <bitset>
//#pragma GCC optimize(2);
#define IOS ios::sync_with_stdio(false);
#define mm(a, b) memset(a, b, sizeof(a))
const double PI = acos(-1.0);
typedef long long ll;
const int N = 1e6+5;
const int M = N*2;
const double eps =1e-8;
const ll mod =  1e9+7;
const int INF = 0x3f3f3f3f;
const double maxd = -1e9;
const int maxn = 500;
using namespace std;
typedef pair<int,int> PII;
//dp初始化为0 ,dp[0]=0
//0 0 0 0 0 1 1 1
//0 0 0 0 0 1 1 1
//0 0 1 1 2 2 3 3
//dp初始化为-0x3f ,dp[0]=0
//0 -x -x -x -x 1 -x -x
//0 -x -x -x -x 1 -x -x
//0 -x  1 -x  2 1  3  2

//总结 : 要刚好装满,必须是从 0,0 转移过来的
int f[5000];
int main(){
    int n; int a[3];
    cin>>n;
    memset(f,-40,sizeof f); f[0]=0;
    for(int i=0;i<3;i++) cin>>a[i];
    for(int i=0;i<3;i++){
        for(int j=0;j<=n;j++){
            if(j-a[i]>=0){
                f[j]=max(f[j],f[j-a[i]]+1);
            }
            cout<<f[j]<<" ";
        }
        cout<<endl;
    }
    cout<<f[n]<<endl;
    return 0;
}

【动态规划专练-完全背包】CF189A (1300)

上一篇:Unity 关联 Live2D 学习2


下一篇:python 查看iterable对象的长度,及将iterable对象转换为list所带来的影响