2016-level algorithm the third time on the machine-c.alvinzh fantasy conjecture--three times Square

Source: Internet
Author: User

905 Alvinzh's Fantasy conjecture--three-time thought

Medium problem. Test instructions simple, the topic is simple, divides a number into several cubic numbers and asks the minimum number of cubes.

Brain turn fast immediately think of greed, from the last three times down, anyway, there is 1^3 in the end of the guarantee to reduce. Sorry this is wrong, because 1,27,64,125 ... the number of cubic numbers is not a multiple relationship, and cannot constitute a greedy strategy. To give a counter example: 96=64+8+8+8+8=64+27+1+1+1+1+1, the answer is obviously 5, and greed will count to 7.

Since is not greedy, that is DP, no problem. First of all, the conventional approach, is to think: equivalent to a number into several, to find the smallest number of copies, then the number of numbers as the overall product of the backpack, each cubic number of values as the volume of each item, the value is considered as 1, the problem is converted into a complete backpack (because each cubic number can be taken more than once) to , think carefully.

So, a complete backpack board, but, here is a problem is full backpack full, what to do (perhaps because the subject can be guaranteed to fill the problem is not very conspicuous). Extrapolate, suppose it is possible to install dissatisfaction, how to do? This problem can be resolved when initializing the DP array. The following methods are also available for 01 backpacks:

    • normal 01 backpack or full backpack: initialized to 0;
    • 01 backpack or full backpack full value minimum: initialized to a large value such as \ (inf\) (0x3f3f3f3f);
    • 01 backpack or full backpack full for maximum value: initialized to a small value such as \ (-inf\) ( -0x3f3f3f3f);

Why is it? For the subject, initialized to a large value, if not full, the last Dp[n] will remain the INF, because each time we compare the smaller values are taken. The second practice game of the G is the 01 pack full to maximize value, initialized to the minimum value. (Late to write so much, I hope you can see QAQ)

There is another solution to this problem, that is, similar to the table, the smallest cubic number of all the numbers are calculated by iteration, and then \ (O (1) \) time to obtain the answer. The reference code for the specific visible queue iteration and the reference code three for the For loop iteration. In fact, there is also the idea of DP, because each iteration will use the previous results, for many groups of data, the same can save time. Easy to understand, you can learn a bit.

Analysis

For a complete knapsack direct solution, the time complexity is \ (O (V*∑ (V/WI)) \) .

Iteration of the words of complexity is not much, because of multiple sets of data, the total time to run the table is shorter, do not tangle this.

Extension: Full backpack full problem: HDU 1114.

Reference code one: Full backpack full to find the minimum value
////Created by Alvinzh on 2017/10/24.//Copyright (c) alvinzh. All rights reserved.//#include <cstdio>#include <cstring>#define INF 0x3f3f3f3f#define MaxSize 1000005intweight[ the];intAns[maxsize];intMain () { for(inti =1; I < the; ++i) Weight[i] = i * i * i;intN while(~SCANF ("%d", &n)) { for(inti =0; I <= N; ++i)//Initialize to maximum valueAns[i] = INF; ans[0] =0; for(inti =0; I < the; ++i) { for(intj = Weight[i]; J <= N; ++J) {if(Ans[j] > Ans[j-weight[i]] +1) Ans[j] = Ans[j-weight[i]] +1; }        }if(Ans[n] = = N) printf ("Oh no!\ n");Elseprintf"%d\n", Ans[n]); }}/** Full Backpack just fills up the problem and asks for the minimum value.  */
Reference code two: queue Iteration
////Created by Alvinzh on 2017/10/24.//Copyright (c) alvinzh. All rights reserved.//#include <cstdio>#include <cstring>#include <queue>#define INF 0x3f3f3f3f#define MaxSize 1000005using namespaceStdintweight[ the];intans[maxsize];queue<int> Q;voidInit () {memset (ans, INF,sizeof(ans)); for(inti =1; I < the; ++i) Weight[i] = i * i * i; for(inti =1; I <= -; ++i) {Ans[weight[i]] =1;    Q.push (Weight[i]); } while(! Q.empty ()) {intW = Q.front (); Q.pop (); for(inti =1; I <= -; ++i) {intnum = Weight[i] + W;if(Num >1000000) Break;if(Ans[num] < INF)Continue; Ans[num] = Ans[w] +1;        Q.push (num); }    }}intMain () {init ();intN while(~SCANF ("%d", &n)) {if(Ans[n] = = N) printf ("Oh no!\ n");Elseprintf"%d\n", Ans[n]); }}/** Idea: Direct wide search, the number of the first to throw into the queue, repeatedly using the number in the queue to update the number is not updated on the line, attention to the number of groups out of bounds.  */
Reference code three: for loop iterations
/*Author: Zeng (13422)Result:ac submission_id:391756Created At:fri Nov 18:26:40 gmt+0800 (CST)problem:905 time:353 memory:6612*/#include <cstdio>#include <algorithm>#include <iostream>using namespaceStdintf[1000001],n;intMain () { for(inti =1; I <=1000000; i++) F[i] =1000000000; f[0] =0; for(inti =0; I <=1000000; i++) for(intK =1; i + K * k * k <=1000000; k++) F[i+k*k*k] = min (F[i+k*k*k],f[i] +1); while(SCANF ("%d", &n)! = EOF)if(F[n] = = N) printf ("Oh no!\ n");Elseprintf"%d\n", F[n]);return 0;}

2016-level algorithm the third time on the machine-c.alvinzh fantasy conjecture--three times Square

Related Article

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.