[U] 3.1.6 Stamps will incorrectly question the DP

Source: Internet
Author: User

Chapter 3 I cannot answer the question in the first section ~~~ No way. You can only give the following question a try ~

What about this question...

First, I thought about a very silly DP equation. Let your program crash...


A two-dimensional boolean array of 10001*200 is opened, and the [denomination] [number of sheets] is used for DP. Because the maximum denomination is only 10000, the circular array method is used, barely make the space not burst, and the result goes through nine points, and 10th points have timed out decisively.

I thought about the card table later! Can be changed to one-dimensional DP! A [denomination] the minimum value is saved;

A [I] = min (a [I], a [I-denomination] + 1 );

In this way, A is good. The speed is okay.

/* ID: bysenLANG: C ++ PROG: stamps */# include <stdio. h> # define INF 0x0fffffusing namespace std; int re [10001]; // The DP array record constitutes the maximum number of stamps with the current denomination int v [201]; int min (int, int B) {return a <B? A: B;} int main () {freopen ("stamps. in "," r ", stdin); freopen (" stamps. out "," w ", stdout); int k, n; for (int I = 0; I <10001; I ++) re [I] = INF; scanf ("% d", & k, & n); for (int I = 0; I <n; I ++) {scanf ("% d ", & v [I]); re [v [I] = 1;} int top = v [0]; while (true) {top ++; if (top> 10001) re [top % 10001] = INF; for (int I = 0; I <n; I ++) if (top-v [I]> 0) re [top % 10001] = min (re [top % 10001], re [(top-v [I] + 10001) % 10001] + 1); if (re [top % 10001]> k) break;} printf ("% d \ n", top-1); return 0 ;}


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.