Codeforces189a-cut ribbon DP

Source: Internet
Author: User

Question: Here is a piece of rope that allows you to cut the rope into any segment consisting of A, B, and C to obtain the maximum number of segments;

Solution: Infinite backpacks

Solution code:

1 // file name: 189a. CPP 2 // Author: darkdream 3 // created time: thursday, July 24, 2014, 26 seconds, 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] [1, 4005]; 27 int main () {28 int n, a [4]; 29 memset (ANS, 0, sizeof (ANS )); 30 scanf ("% 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.