首页 > 代码库 > codeforces189A - Cut Ribbon DP

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 }
View Code