Codeforces 189A:Cut Ribbon(完全背包,DP)
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
Polycarpus has a ribbon, its length is nnn. He wants to cut the ribbon in a way that fulfils the following two conditions:
- After the cutting each ribbon piece should have length aaa, bbb or ccc.
- 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 nnn, aaa, bbb and ccc (1 ≤ n, a,b, c≤ 4000)(1 ≤ n, a,b, c≤ 4000)(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 aaa, bbb and ccc 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
input
5 5 3 2
output
2
input
7 5 5 2
output
2
Note
In the first example Polycarpus can cut the ribbon in such way: the first piece has length 222, the second piece has length 333.
In the second example Polycarpus can cut the ribbon in such way: the first piece has length 555, the second piece has length 222.
Solve
完全背包,看成给出三种商品,恰好能够装满背包的情况
注意初始化的问题,如果把dpdpdp数组全部初始化为−1-1−1的话,需要注意dp[j]=−1&&dp[j−a[i]]=−1dp[j]=-1\&\&dp[j-a[i]]=-1dp[j]=−1&&dp[j−a[i]]=−1的情况。或者就全部初始值给成小于−1-1−1的数
Code
/*************************************************************************
> Author: WZY
> School: HPU
> Created Time: 2019-04-01 18:30:38
************************************************************************/
#include <cmath>
#include <cstdio>
#include <time.h>
#include <cstring>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <random>
#include <iomanip>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <random>
#define ll long long
#define ull unsigned long long
#define lson o<<1
#define rson o<<1|1
#define ms(a,b) memset(a,b,sizeof(a))
#define SE(N) setprecision(N)
#define PSE(N) fixed<<setprecision(N)
#define bug cerr<<"-------------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
#define LEN(A) strlen(A)
const double E=exp(1);
const double eps=1e-9;
const double pi=acos(-1.0);
const int mod=1e9+7;
const int maxn=1e6+10;
const int maxm=1e3+10;
const int moha=19260817;
const int inf=1<<30;
const ll INF=1LL<<60;
using namespace std;
inline void Debug(){cerr<<'\n';}
inline void MIN(int &x,int y) {if(y<x) x=y;}
inline void MAX(int &x,int y) {if(y>x) x=y;}
inline void MIN(ll &x,ll y) {if(y<x) x=y;}
inline void MAX(ll &x,ll y) {if(y>x) x=y;}
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
cerr<<arg<<"";Debug(rest...);}
int dp[maxn];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);cin.tie(0);
cout.precision(20);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
int n;
int a[4];
cin>>n>>a[0]>>a[1]>>a[2];
ms(dp,-1);
dp[0]=0;
for(int i=0;i<3;i++)
for(int j=a[i];j<=n;j++)
if(dp[j-a[i]]!=-1)
MAX(dp[j],dp[j-a[i]]+1);
cout<<dp[n]<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s.\n";
#endif
return 0;
}
Codeforces 189A:Cut Ribbon(完全背包,DP)的更多相关文章
- Codeforces 189A. Cut Ribbon
题目链接:http://codeforces.com/problemset/problem/189/A 题意: 给你一个长度为 N 的布条, 再给你三个长度 a, b , c.你可以用剪刀去剪这些布条 ...
- Codeforces 730J:Bottles(背包dp)
http://codeforces.com/problemset/problem/730/J 题意:有n个瓶子,每个瓶子有一个当前里面的水量,还有一个瓶子容量,问要把所有的当前水量放到尽量少的瓶子里至 ...
- Codeforces 922 E Birds (背包dp)被define坑了的一题
网页链接:点击打开链接 Apart from plush toys, Imp is a huge fan of little yellow birds! To summon birds, Imp ne ...
- 【CF 189A Cut Ribbon】dp
题目链接:http://codeforces.com/problemset/problem/189/A 题意:一个长度为n的纸带,允许切割若干次,每次切下的长度只能是{a, b, c}之一.问最多能切 ...
- codeforces 148E Aragorn's Story 背包DP
Aragorn's Story Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/probl ...
- CF 189A Cut Ribbon
#include<bits/stdc++.h> using namespace std; const int maxn = 4000 + 131; int n, a, b, c; int ...
- Codeforces 922 思维贪心 变种背包DP 质因数质数结论
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- Codeforces Round #119 (Div. 2) Cut Ribbon(DP)
Cut Ribbon time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp
B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...
- Codeforces Round #119 (Div. 2)A. Cut Ribbon
A. Cut Ribbon time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- Hadoop入门 完全分布式运行模式-准备
目录 Hadoop运行环境 完全分布式运行模式(重点) scp secure copy 安全拷贝 1 hadoop102上的JDK文件推给103 2 hadoop103从102上拉取Hadoop文件 ...
- 23. 关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
原文:https://blog.csdn.net/u011596455/article/details/60322568 版权声明:本文为博主原创文章,转载请附上博文链接! 在Ubuntu中,有时候运 ...
- Hadoop、Hive【LZO压缩配置和使用】
目录 一.编译 二.相关配置 三.为LZO文件创建索引 四.Hive为LZO文件建立索引 1.hive创建的lzo压缩的分区表 2.给.lzo压缩文件建立索引index 3.读取Lzo文件的注意事项( ...
- d3 CSS
CSS的inline.block与inline-block 块级元素(block):独占一行,对宽高的属性值生效:如果不给宽度,块级元素就默认为浏览器的宽度,即就是100%宽. 行内元素(inline ...
- 接口测试 python+PyCharm 环境搭建
1.配置Python环境变量 a:我的电脑->属性->高级系统设置->环境变量->系统变量中的PATH变量. 变量名:PATH 修改变量值为:;C:\Python27 ...
- Java资源下载
tomcat http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.49/bin/apache-tomcat-8.5.49.tar.gz s ...
- 【Java】【设计模式】单例设计模式
思想: 为了避免其他程序过多建立该类对象,先禁止其他程序建立该类对象 为了让其他程序可以访问到该类对象,只好在本类中自定义一个对象 为了方便其他程序对自定义对象的访问,可以对外提供一些访问方式 代码体 ...
- JDBCUtils工具类的属性
package cn.itcast.util;import java.io.FileReader;import java.io.IOException;import java.net.URL;impo ...
- PHP之CURL实现含有验证码的模拟登录
博主最近在为学校社团写一个模拟登录教务系统来进行成绩查询的功能,语言当然是使用PHP啦,原理是通过php数据传输神器---curl扩展,向学校教务系统发送请求,通过模拟登录,获取指定url下的内容. ...
- Mysql资料 数据类型
目录 一.类型 整型 浮点型 定点数 字符串 二进制数据 时间日期类型 二.长度和范围 三.使用建议 原则 存储引擎 text和blob 浮点数和定点数 四.属性 一.类型 整型 取值范围如果加了un ...