dag-Coin Issue

Source: Internet
Author: User

Problem: There are n kinds of coins, the face value is v1,v2 ... Vn, there are infinitely many of each. Given a nonnegative integer s, how many coins can be selected, so that the sum of the par value is exactly s. Maximum number of coins to be exported.

Analysis: This problem is still a DP entry problem. First of all, I was thinking about the problem, the first is just beginning to think that the value of the coin is a state, the results of how to think of the impassability, the second is to read the purple book to his proposed by assigning a very small initial value to achieve the test of whether the road to the end, has been trying to get through, and finally in a certain Timor God's help to take off the pit-this problem is really pretty water = =. And then the idea of solving problems, the key point is how to set the state and detect whether it can reach the end point, the first problem is to set the face value and state, so the state of the way to select a coin, at the current face value and minus the value of the coin, to get the new face value and, that is, new state, The second problem is to set the position of the subscript 0 of the array with the nominal value and the subscript to 0, the rest is set to-1, and the status is set to a very small number at the DP, so that when a path reaches the end point, it can be taken to 0, and it can be added, and the path to the end cannot be greater than 0, no matter how you add it.

Code:

#include <cstdio>
#include <cstring>
int d[1000],v[10];
int max (int a, int b)
{return
	a > b a:b;
}
int Dpmax (int s,int n)
{
	if (D[s]!=-1) return
		D[s];
	D[s] = -10000;//is used to set the path that cannot go to the end point is definitely less than the path to the end point for
	(int i = 0;i < n;i++)
		if (S >= v[i])/equals sign is critical
			d[s] = max (d[ S], Dpmax (S-v[i], n) +1);
	return d[s];
}

int main ()
{
	int n, s, I;
	while (scanf ("%d%d", &n, &s)!= EOF)
	{for
		(i = 0;i < n;i++)
			scanf ("%d", &v[i]);
		memset (d,-1, sizeof d);
		D[0] = 0;//is used to discern whether the road can go to the endpoint
		printf ("%d\n", Dpmax (S, N));
	return 0;
}


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.