codeforces189A - Cut Ribbon DP

题意:给你一段绳子,让你把绳子切成 只有a,b,c组成的任意段,求得到最大段数;

解题思路:无穷背包

解题代码:

 1 // File Name: 189a.cpp
 2 // Author: darkdream
 3 // Created Time: 2014年07月24日 星期四 08时11分26秒
 4
 5 #include<vector>
 6 #include<list>
 7 #include<map>
 8 #include<set>
 9 #include<deque>
10 #include<stack>
11 #include<bitset>
12 #include<algorithm>
13 #include<functional>
14 #include<numeric>
15 #include<utility>
16 #include<sstream>
17 #include<iostream>
18 #include<iomanip>
19 #include<cstdio>
20 #include<cmath>
21 #include<cstdlib>
22 #include<cstring>
23 #include<ctime>
24
25 using namespace std;
26 int ans[4][4005];
27 int main(){
28     int n , a[4] ;
29     memset(ans,0,sizeof(ans));
30     scanf("%d %d %d %d",&n,&a[1],&a[2],&a[3]);
31     ans[0][0] = 1;
32     for(int i =1;i <= 3;i ++)
33     {
34        for(int j = 0;j<= 4000;j++)
35        {
36           if(j < a[i] || ans[i][j-a[i]] == 0)
37               ans[i][j] = ans[i-1][j];
38           else {
39               ans[i][j] = max(ans[i][j-a[i]]+1,ans[i-1][j]);
40           }
41        }
42
43     }
44 /*    for(int i =1;i <= 3; i ++)
45     {
46          for(int j =1;j <= 7;j ++)
47              printf("%d ",ans[i][j]);
48          printf("\n");
49     }*/
50     printf("%d\n",ans[3][n]-1);
51
52
53     return 0;
54 }

codeforces189A - Cut Ribbon DP,布布扣,bubuko.com

时间: 2024-10-10 16:40:55

codeforces189A - Cut Ribbon DP的相关文章

CodeForces 189A Cut Ribbon

Cut Ribbon Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 189A64-bit integer IO format: %I64d      Java class name: (Any) Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way

Cut Ribbon

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

hdu 5452 Minimum Cut 树形dp

Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5452 Description Given a simple unweighted graph G (an undirected graph containing no loops nor multiple edges) with n nodes and m edges. Let T be a spa

UVA 10003 Cutting Sticks 区间DP+记忆化搜索

UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的长度L,第二行是切割点的个数n,接下来的n行是切割点在木棍上的坐标. 输出切割木棍的最小费用 前话-区间dp简单入门 区间dp的入门下面博客写的非常好,我就是看的他们博客学会的,入门简单,以后的应用就得靠自己了. https://blog.csdn.net/qq_41661809/article/d

POJ 1179 Polygon

题目大意:给一个多边形,每个顶点有一个值,每个边编号从1到N,边的属性是加或者乘.首先先拆掉一条边,剩下的如下做:选定一条边以及这条边的两个端点(两个数)用新顶点替换(新顶点即:按照这条边的属性(加或乘)算出这两个数的乘积或者和).到最后剩一个点,也就是一个值.求这些值的最大值输出,并输出此时最先拆掉的是哪条边. Input: 4 t  -7  t  4  x  2  x  5 Output: 33 1 2 区间DP问题 注意最大值可以由两个最小值相乘得到 注意下标和求min_v和max_v时的

LeetCode: Palindrome Partition

LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], [&q

hdu 5421 小明系列问题——小明序列(LIS最长上升子序列)

1 /***************************************************** 2 题目: 小明系列问题——小明序列(hdu 4521) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=4521 4 算法: LIS最长上升子序列 5 6 ******************************************************/ 7 #include<cstdio> 8 #include<

11782 - Optimal Cut(树形DP+记忆化搜索)

题目链接:11782 - Optimal Cut 题意:按前序遍历给定一棵满二叉树,现在有k次,可以选k个节点,获得他们的权值,有两个条件: 1.一个节点被选了,他的子节点就不能选了. 2.最终选完后,根到所有叶子的路径上,都要有一个被选的节点. 思路:树形dp,dp[u][k]代表在结点u,可以选k个节点,那么就分两种情况 选u节点,dp[u][k] = node[u]; 选子节点之和,那么就把k次分配给左右孩子,dp[u][k] = max(dp[u][k], dp[u][i], dp[u]

CodeForces 176B Word Cut (计数DP)

Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 176B Description Let's consider one interesting word game. In this game you should transform one word into another through specia