1068. Find more Coins (30) "Backpack"--pat (Advanced level) practise

Source: Internet
Author: User

Topic Information

1068. Find more Coins (30)

Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there is a special requirement of the Payment:for each bill, she must pay the exact amount. Since she has as many as 104 coins and she, she definitely needs your help. You is supposed to tell hers, for any given amount of money, whether or not she can find some coins to pay for it.

Input Specification:

Each input file contains the one test case. For each case, the first line contains 2 positive numbers:n (<=10^4, the total number of coins) and M (<=10^2, the A Mount of money Eva have to pay). The second line contains N face values of the coins, which is all positive numbers. All the numbers in a line is separated by a space.

Output Specification:

For each test case, print in one line the face values V1 <= V2 <= ... <= Vk such that V1 + V2 + ... + Vk = M. All the numbers must is separated by a space, and there must is no extra space at the end of the line. If Such a solution is not unique, the output of the smallest sequence. If There is no solution, output "no solution" instead.

note:sequence {a[1], a[2], ...} is said to be "smaller" than sequence {b[1], b[2], ...} If there exists K >= 1 such that A [I]=b[i] for all I < K, and A[k] < b[k].

Sample Input 1:
8 9
5 9 8 7 2 3 4 1
Sample Output 1:
1 3 5
Sample Input 2:
4 8
7 2 4 3
Sample Output 2:
No Solution

Thinking of solving problems

01 The number of backpack records used last backtracking

AC Code
#include <cstdio>#include <functional>#include <algorithm>using namespace STD;intv[10005][ the], flag[10005][ the], a[10005];intpath[10005];intN, M;intMain () {scanf("%d%d", &n, &m); for(inti =1; I <= N; ++i) {scanf("%d", A + i); } sort (A +1, A + n +1, greater<int> ()); for(inti =1; I <= N; ++i) { for(intj =1; J <= M; ++J) {if(J < A[i] | | v[i-1][j-a[i]] + A[i] < v[i-1][j]) {V[i][j] = v[i-1][J]; }Else{V[i][j] = v[i-1][j-a[i]] + a[i]; FLAG[I][J] =1; }        }    }if(V[n][m] = = m) { vector<int>Rs while(m) { while(!flag[n][m])--n;            Rs.push_back (A[n]);        M-= a[n--]; } for(inti =0; I < rs.size ()-1; ++i) {printf("%d", Rs[i]); }printf("%d\n", Rs[rs.size ()-1]); }Else{printf("No solution\n"); }return 0;}

Personal game Promotion:
"10 Cloud Square" and block to eliminate the war!

1068. Find more Coins (30) "Backpack"--pat (Advanced level) practise

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.