pat 1068 动态规划/Fina More Conis

1068. Find More Coins (30)

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 was a special requirement
of the payment: for each bill, she must pay the exact amount. Since she has as
many as 104 coins with her, she definitely needs your help. You
are supposed to tell her, for any given amount of money, whether or not she can
find some coins to pay for it.

Input Specification:M

Each input file contains one test case. For each case, the first line
contains 2 positive numbers: N (<=104, the total number of coins)
and M(<=102, the amount of money Eva has to pay). The second line
contains N face values of the coins, which are all positive numbers. All the
numbers in a line are 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 be separated by a space, and there
must be no extra space at the end of the line. If such a solution is not unique,
output 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

自己的动态规划有些弱,一直在想办法提高,在遇到这道题目后,第一时间知道肯定要用DP(这里的要求与经典的背包问题很相似),在尝试着去做的过程中,回忆着DP的应用场景,结合这道题目的特殊需求,在过程中进行设计;
先回顾下动态规划问题的几个要素:最优子结构、子问题重叠、边界与子问题的独立性;这里我们可以看到面值M通过选择若干小面值的组合来组成,假定每个面值是value[i],我们的问题变成了对于每个阶段的i-th coin,求得M-value[i](选取了i-th coin)/M9未选取i-th coin)的最佳组合问题,而这个问题与原问题是完全独立的,且本质完全相同,即子结构问题,并且每一步的问题都是重叠的;至于边界,在没有一枚硬币被选中的时候,支付金额为0,之后的问题可以依次累计。
【关于动态规划的帮助理解,有一篇讲解金矿问题的很形象直观的博客讲得很好】http://www.cnblogs.com/sdjl/articles/1274312.html
这里的特殊地方在于除了选取到面值恰好相同的结果外,还要求在有多个解的情况下,能够选出顺序的value最小的一种组合,经过考虑后,我采取的是对value进行高->低的排序,之后进行动态规划的求解,并单独设置标志,由于要得的是value尽可能小,因此在后边进行选择的只要加入后支付值>或者是=都以最新的value组合为准,这样就保证了相同的组合小面值的被选取。

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <stdlib.h>

#include <algorithm>

#include <vector>

using
namespace std;

int pay[10010][110];

int flag[10010][110];//0:heritate from the last circle;1:add the current value

int value[10010];

vector<int> v;

bool
cmp(int
a, int
b){

    return
a > b;

}

int
main(){

    int
N, M,i,max=0,temp1,temp2,j,temp;

    int
row, col;

    scanf("%d%d", &N, &M);

    for
(i = 1; i <= N; i++){

        scanf("%d", &value[i]);

    }

    sort(value+1, value + N + 1, cmp);

    for
(i = 1; i <= N; i++){

        //temp1:the last row,corresponding value

        //temp2:add the new value

        max += value[i];//the upper bound

        temp = max > M ? M : max;

        for
(j = value[i]; j <= temp; j++){

            temp1 = pay[i - 1][j];

            temp2 = pay[i - 1][j - value[i]] + value[i];

            if
(temp1 <= temp2){//add

                flag[i][j] = i;

                pay[i][j] = temp2;

            }

            else

                pay[i][j] = temp1;

        }

    }

    if
(pay[N][M] == M){//succeed to find the proper value

        row = N, col = M;

        while
(pay[row][col] != value[row]){

            if
(flag[row][col] != 0){

                v.push_back(value[row]);

                col = col - value[row];

                row--;

            }

                

            else{

                row--;

            }

        }

        v.push_back(value[row]);

        for
(i = 0; i < v.size()-1; i++)

            printf("%d ", v[i]);

        printf("%d", v[i]);

    }

    else{

        printf("No Solution");

    }

    system("pause");

    return
0;

}

  

 

pat 1068 动态规划/Fina More Conis,布布扣,bubuko.com

时间: 2024-10-08 17:21:44

pat 1068 动态规划/Fina More Conis的相关文章

PAT 1068. 万绿丛中一点红

PAT 1068. 万绿丛中一点红 对于计算机而言,颜色不过是像素点对应的一个24位的数值.现给定一幅分辨率为MxN的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围8个相邻像素的颜色差充分大. 输入格式: 输入第一行给出三个正整数,分别是M和N(<= 1000),即图像的分辨率:以及TOL,是所求像素点与相邻点的颜色差阈值,色差超过TOL的点才被考虑.随后N行,每行给出M个像素的颜色值,范围在[0, 224)内.所有同行数字间用空格或TAB分开. 输出格式:

PAT 1068. Find More Coins

标准10背包 #include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; const int ROWS = 10002; const int COLS = 102; char dp[ROWS][COLS]; bool dfs(vector<int> &coins, vector<int> &am

PAT 1068 Find More Coins (30)

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 was a special requirement of the payment: f

1068. Find More Coins (30)【背包】——PAT (Advanced Level) Practise

题目信息 1068. Find More Coins (30) 时间限制150 ms 内存限制65536 kB 代码长度限制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 a

PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***

1068 Find More Coins (30 分)   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 was a special

PAT 甲级 1068 Find More Coins

https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 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

【PAT甲级】1068 Find More Coins (30 分)(背包/DP)

题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[10007];int dp[107];int vis[10007][107];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.ti

PAT (Advanced Level) Practice 1068 Find More Coins

题解 01背包板子 + 记录路径.这次的记录路径比较特殊,要从多组解中找到一组由尽量小价值的硬币组成的解.所以不能利用一维数组记录路径,path[目前重量] = 物品序号,因为这样最后只能记录一个可能符合或不符合要求解.所以应该利用二维数组记录路径,path[ 物品序号 ][ 目前重量 ] = 1,这样可以记录多组解.因为要求为找到最小的一组解,所以先将拥有的硬币从大到小排序,以便于进行01背包时,可以从大到小更新解. 代码 #include<bits/stdc++.h> using name

PAT甲级题分类汇编——杂项

集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 Sort with Swap(0, i) 25 通过与0号元素交换来排序 数学 1068 Find More Coins 30 子集和问题 算法 1070 Mooncake 25 背包问题 算法 1078 Hashing 25 散列 散列 1085 Perfect Sequence 25 符合约束的最大数列长度 集合 1092 To