pat1068. Find More Coins (30)
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:
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)的更多相关文章
- 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 ...
- 1068. Find More Coins (30)
题目如下: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...
- PAT甲题题解-1068. Find More Coins (30)-dp,01背包
一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...
- 1068 Find More Coins (30)(30 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- 1068 Find More Coins (30分)(dp)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- PAT (Advanced Level) 1068. Find More Coins (30)
01背包路径输出. 保证字典序最小:从大到小做背包. #include<cstdio> #include<cstring> #include<cmath> #inc ...
- 【PAT甲级】1068 Find More Coins (30 分)(背包/DP)
题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. AAAAAccepted code: #define HAVE_ST ...
- PAT 1068 Find More Coins[dp][难]
1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...
- PAT 甲级 1068 Find More Coins(0,1背包)
1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...
随机推荐
- JavaScript知识体系索引
JavaScript 参考手册 www.w3school.com.cn JavaScript基础语法 JavaScript的数据类型 Javascript的对象分类 JavaScript内置对象 Ja ...
- 更新XML的Attribute(属性)
有一个XML文档,一个属性"pk"错了,正确是2.我们怎样把它更改正确?原XML文档如下: <?xml version="1.0" encoding=&q ...
- iOS开发图片与颜色处理工具
1.根据颜色生成一张图片 /** 根据颜色生成一张图片 @param color 颜色进制 UIColor类型 @return 一张UIImage图片 */ + (UIImage *)createIm ...
- AngularJS(四)——ng-controller(控制器)
前言 上篇大概说了一下指令的应用格式以及创建自定义指令方法,本篇重点介绍一些ng-controller都有哪些小作用. 内容 通过修改控制器部分,修改显示界面. Demo <div ng-app ...
- Django博客开发教程,Django快速开发个人blog
学DjangoWEB框架,估计大部分的朋友都是从Blog开发开始入门的,Django中文网发布了一个Django开发教程,这个教程简单易懂,能让你快速的使用Django开发一个漂亮的个人blog,是D ...
- 10、OpenCV Python 图像二值化
__author__ = "WSX" import cv2 as cv import numpy as np #-----------二值化(黑0和白 255)---------- ...
- SP16549 QTREE6 - Query on a tree VI LCT维护颜色联通块
\(\color{#0066ff}{ 题目描述 }\) 给你一棵n个点的树,编号1~n.每个点可以是黑色,可以是白色.初始时所有点都是黑色.下面有两种操作请你操作给我们看: 0 u:询问有多少个节点v ...
- php http 缓存(客户端缓存)
<?php /* * Expires:过期时间 * Cache-Control: 响应头信息 * (max-age:[秒]缓存过期时间(请求时间开始到过期时间的秒数), * s-maxage:[ ...
- kuangbin专题十六 KMP&&扩展KMP HDU4300 Clairewd’s message
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...
- 查看和导入证书(.cer / .pfx)
作为文件形式存在的证书一般有这几种格式: 1.带有私钥的证书 由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进制格式的证书形 ...