1068. Find More Coins (30)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:

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

提交代码

学习网址:http://blog.csdn.net/xyt8023y/article/details/47255241

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
int mo[],f[][];
bool vis[][];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
int i,j,n,m;
scanf("%d %d",&n,&m);
for(i=; i<=n; i++)
{
scanf("%d",&mo[i]);
}
sort(mo+,mo+n+,cmp);
//f[i][j]=maz(f[i-1][j],f[i-1][j-mo[i]]+mo[i])
for(i=; i<=n; i++)
{
for(j=; j<=m; j++)
{
if(mo[i]<=j&&(f[i-][j-mo[i]]+mo[i]>=f[i-][j]))//这里要取到等号,为了状态的衔接
{
f[i][j]=f[i-][j-mo[i]]+mo[i];
vis[i][j]=true;
}
else
{
f[i][j]=f[i-][j];
}
}
}
/*for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
cout<<i<<" "<<j<<" "<<f[i][j]<<endl;
}
cout<<endl;
}*/ if(f[n][m]!=m)
{
printf("No Solution\n");
}
else
{
vector<int> v;
while(m)
{
while(!vis[n][m])
{
n--;
}
v.push_back(mo[n]);
m-=mo[n];
n--;
}
printf("%d",v[]);
for(i=;i<v.size();i++){
printf(" %d",v[i]);
}
printf("\n");
}
return ;
}

pat1068. Find More Coins (30)的更多相关文章

  1. 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 ...

  2. 1068. Find More Coins (30)

    题目如下: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...

  3. PAT甲题题解-1068. Find More Coins (30)-dp,01背包

    一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...

  4. 1068 Find More Coins (30)(30 分)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  5. 1068 Find More Coins (30分)(dp)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  6. PAT (Advanced Level) 1068. Find More Coins (30)

    01背包路径输出. 保证字典序最小:从大到小做背包. #include<cstdio> #include<cstring> #include<cmath> #inc ...

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

    题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. AAAAAccepted code: #define HAVE_ST ...

  8. PAT 1068 Find More Coins[dp][难]

    1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...

  9. PAT 甲级 1068 Find More Coins(0,1背包)

    1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...

随机推荐

  1. socketserver,threading

    一,socketserver  #server import socketserver class Myserver(socketserver.BaseRequestHandler): def han ...

  2. 在PowerShell中操作SharePoint对象

    1. 用PowerShell创建一个SharePoint内容对象创建一个自定义列表:$SPSite = New-Object Microsoft.SharePoint.SPSite("htt ...

  3. 高仿JDK动态代理 底层源码实现

    动态代理实现思路 实现功能:通过Proxy.newProxyInstance返回代理对象 1.创建一个处理业务逻辑的接口,我们也和JDK一样,都使用InvocationHandler作为接口名,然后接 ...

  4. excel测试数据导入

    需求背景:测试数据的导入一般在dataprovider中写入对应的测试数据,当参数较多,组合测试或者接口参数测试的测试数据都需要逐一写数据驱动类,数据准备消耗了大量时间.前一篇博客中介绍了对偶测试的小 ...

  5. 【Leetcode】Single Number

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  6. 类关系/self/特殊成员

    1.依赖关系 在方法中引入另一个类的对象 2.关联关系.聚合关系.组合关系 #废话少说 直接上代码===>选课系统 # coding:utf-8 class Student(object): d ...

  7. python 根据 数据库创建java 文件

    #coding=utf-8 import pymysql import os import re # 包全路径 packagepath=r'E:\idea工程\dc-exam\dc-exam\src\ ...

  8. [Leetcode]017. Letter Combinations of a Phone Number

    public List<String> letterCombinations(String digits) { LinkedList<String> ans = new Lin ...

  9. 查询mysql单库的修改时间,大小

      select database_name,max(last_update) from mysql.innodb_table_stats group by database_name;   SELE ...

  10. mysql 中decimal中去掉后面多余的0

    #去除Decimal后面多余的0 #处理前SELECT '0.12000','1.203010','-0.20' #处理后SELECT 0+CAST('0.12000' AS CHAR),0+CAST ...