codeforces 189A. Cut Ribbon
来源:互联网 发布:软件开发部门ios 编辑:程序博客网 时间:2024/06/06 08:36
题目链接:http://www.codeforces.com/problemset/problem/189/A
题意:有一块长为n的布,现在要把它剪开,只能剪成长度为a, b, c的小块布。求最多能剪成多少块。
思路:一个必须装满的完全背包问题。我们设dp[i]表示长度为i的布最多能剪成多少块,那么dp[i] = 只能被dp[i - a], dp[i - b], dp[i - c]更新,还有一点,如果以上三者都为零的话,证明此点不会被更新,直接dp即可。为了边界处理简单,所以采用记忆化搜索。
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <cstdlib>typedef long long ll;const int MAXN = 4000 + 100;int dp[MAXN], a[3];int dfs(int x){ if(x <= 0){ return 0; } if(dp[x] != -1){ return dp[x]; } if(x == a[2] || x == a[1]){ dp[x] = std::max(dfs(x - a[2]), std::max(dfs(x - a[1]), dfs(x - a[0]))) + 1; return dp[x]; } dp[x] = std::max(dfs(x - a[2]), std::max(dfs(x - a[1]), dfs(x - a[0]))); if(dp[x]){ ++dp[x]; } return dp[x];}int main(){ int n; scanf("%d %d %d %d", &n, a, a + 1, a + 2); std::sort(a, a + 3); memset(dp, -1, sizeof(dp)); dp[a[0]] = 1; printf("%d\n", dfs(n)); //for(int i = 1; i <= n; ++i){ //printf("%d ", dp[i]); //} //puts(""); return 0;}
0 0
- codeforces 189A Cut Ribbon
- CodeForces 189A Cut Ribbon
- CodeForces 189A-Cut Ribbon
- codeforces 189A. Cut Ribbon
- CodeForces 189A Cut Ribbon
- Codeforces-189A-Cut Ribbon
- codeforces 189a Cut Ribbon 水dp
- codeforces——189A——Cut Ribbon
- CF - 189A - Cut Ribbon
- CF 189A Cut Ribbon
- CF 189 A Cut Ribbon
- CF 189A Cut Ribbon
- A. Cut Ribbon
- codefroces A. Cut Ribbon
- Codeforces Round #119 (Div. 2) / 189A Cut Ribbon (完全背包)
- Codeforces 189A. Cut Ribbon 完全背包装满最多装下物品的个数
- Codeforces Round #119 (Div. 2) A. Cut Ribbon
- Codeforces189 A. Cut Ribbon(DP)
- makedown语言中中文输入 首行缩进-博客必备
- linux下MySQL安装及设置
- 如何解读Caffe源码
- 操作系统知识点
- poj 3608 Bridge Across Islands (计算几何)
- codeforces 189A. Cut Ribbon
- POJ3254-Corn Fields(状压DP)
- Python 报错IndentationError: unindent does not match any outer indentation level
- java实现快速排序
- jstl标签的用法(一)
- iOS8之后UIAlertView被弃用 UIAlertController成功上位
- js 窗口操作
- HDU 1561(树形dp)
- eclipse启动项目非常慢