L3-001. 凑零钱

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

韩梅梅喜欢满宇宙到处逛街。现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债。韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是否可能精确凑出要付的款额。

输入格式:

输入第一行给出两个正整数:N(<=104)是硬币的总个数,M(<=102)是韩梅梅要付的款额。第二行给出N枚硬币的正整数面值。数字间以空格分隔。

输出格式:

在一行中输出硬币的面值 V1 <= V2 <= ... <= Vk,满足条件 V1 + V2 + ... + Vk = M。数字间以1个空格分隔,行首尾不得有多余空格。若解不唯一,则输出最小序列。若无解,则输出“No Solution”。

注:我们说序列{A[1], A[2], ...}比{B[1], B[2], ...}“小”,是指存在 k >= 1 使得 A[i]=B[i] 对所有 i < k 成立,并且 A[k] < B[k]。

输入样例1:

8 9
5 9 8 7 2 3 4 1

输出样例1:

1 3 5

输入样例2:

4 8
7 2 4 3

输出样例2:

No Solution
#include <bits/stdc++.h>
using namespace std;
#define maxn 11000
int vis[maxn];
int s[maxn];
int tmp[maxn];
int n, m;
int cnt;
int flag;
int all;
void dfs(int index,int sum, int cnt,int tol)//tol 为剩余的钱数
{
if(flag || sum > m || tol < m - sum)
return;
if(sum == m)
{
for(int i = ; i < cnt - ; i++)
printf("%d ",tmp[i]);
printf("%d\n",tmp[cnt - ]);
flag = ;
return;
}
for(int i = index + ; i < n; i++)
{
if(!vis[i] && s[i] <= m - sum)
{
vis[i] = ;
tmp[cnt] = s[i];
dfs(i,sum + s[i], cnt+, tol - s[i]);
if(flag) return;
vis[i] = ;
}
}
}
int main()
{ scanf("%d%d", &n, &m);
all = ;
for(int i = ; i < n; i++)
{
scanf("%d", &s[i]);
all += s[i];
}
sort(s, s + n);
cnt = ;
dfs(-,, cnt,all);
if(!flag)
printf("No Solution\n");
}

01

本来背包判断是否存在,然后dfs的 但是只有28分   然后学了下记录路径

b[sum]=a[i] 就是代表 sum的钱=a[i]+b[ sum-a[i] ] 这样递归下去 直接sum=0 这样就得到路径了

#include <bits/stdc++.h>
using namespace std; int dp[];
int b[];
int a[]; void dfs(int t)
{
if(t==b[t])
{
printf("%d",b[t]);
return ;
}
dfs(t-b[t]);
printf(" %d",b[t]);
} int main()
{
int n,m;
scanf("%d%d",&n,&m); for(int i=; i<=n; i++)
scanf("%d",&a[i]);
sort(a+,a+n+);
for(int i=; i<=n; i++)
{
for(int j=m; j>=a[i]; j--)
{
if(dp[j-a[i]] || j==a[i])//是否到达过 || 直接到达
{
if(dp[j]<=dp[j-a[i]]+)//dp[j] 表示j是由dp[j]个数组成 又因为数越多 组成的序列越小 数相同 用=号去更新
{
dp[j] = dp[j-a[i]]+;
b[j] = a[i]; //钱是j的时候 是由 (j-a[i])的时候+a[i]过来的
}
}
}
} if(!b[m])
puts("No Solution");
else dfs(m),puts("");
return ;
}

我28分的dfs

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
int a[];
bool v[];
int c[];
int k=;
int n,m;
bool f=; void display(int s)
{
for(int i=;i<=n;i++)
{
if(c[i]==) break;
if(i!=) cout<<" "; cout<<c[i];
}
} void dfs(int sum,int s)
{
if(s>n||sum>m) return;
if(sum==m)
{
display(s);
f=;
return;
} for(int i=;i<=k-;i++)
{
if(v[i]) continue;
else
{
v[i]=;
c[s]=a[i];
dfs(sum+a[i],s+);
c[s]=;
if(f) return;
v[i]=;
}
}
} int main()
{ cin>>n>>m;
memset(v,,sizeof v);
for(int i=;i<=n;i++)
{
int x;
cin>>x;
if(x>m) continue;
a[k++]=x;
}
sort(a+,a+k);
dfs(,);
if(!f) cout<<"No Solution";
}

L3-001. 凑零钱(dfs或者01背包)的更多相关文章

  1. c4 L3-001 找零钱 (简单01背包-输出最小字典序解(用vector保存当前最优解))

    #include <iostream> #include <algorithm> #include <vector> #include <cstdio> ...

  2. PAT L3-001 凑零钱(01背包dp记录路径)

    韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是 ...

  3. 天梯赛L3-001. 凑零钱(01背包记录物品)

    L3-001. 凑零钱 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现 ...

  4. 01背包记录路径 (例题 L3-001 凑零钱 (30分))

    题意: 就是找出来一个字典序最小的硬币集合,且这个硬币集合里面所有硬币的值的和等于题目中的M 题解: 01背包加一下记录路径,如果1硬币不止一个,那我们也不采用多重背包的方式,把每一个1硬币当成一个独 ...

  5. hdu3448 01背包+dfs

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 Description 0/1 bag problem should sound f ...

  6. POJ3628 Bookshelf 2(01背包+dfs)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8745   Accepted: 3974 Descr ...

  7. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

  8. [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)

    题目链接:http://acm.swust.edu.cn/problem/465/ 还有一道题只是描述不一样,方法一模一样(http://acm.swust.edu.cn/problem/644/) ...

  9. 九度OJ 1123:采药 (01背包、DP、DFS)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2705 解决:1311 题目描述: 辰辰是个很有潜能.天资聪颖的孩子,他的梦想是称为世界上最伟大的医师. 为此,他想拜附近最有威望的医师为师 ...

随机推荐

  1. MongoDB快速入门(三)- 集合

    创建集合 MongoDB 的 db.createCollection(name, options) 用于创建集合. 在命令中, name 是要创建集合的名称. Options 是一个文档,用于指定集合 ...

  2. QT 实现按住鼠标左键点击对话框空白处,拖动对话框

    定义头文件 QPoint move_point; //移动的距离     bool mouse_press; //按下鼠标左键 protected:     void mousePressEvent( ...

  3. Spring boot学习整理

    目录: Springboot结合hbase Springboot结合elasticsearch Springboot结合RestTemplate处理Http请求 Springboot的maven相关 ...

  4. bzoj 1087 [SCOI2005]互不侵犯King 状态压缩dp

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Descripti ...

  5. nova cell配置

    Configuration option = Default value Description [cells] call_timeout = 60 (IntOpt) Seconds to wait ...

  6. 分布式事务_03_2PC框架raincat源码解析-事务提交过程

    一.前言 前面两节,我们已经将raincat的demo工程启动,并简单分析了下事务协调者与事务参与者的启动过程. 这一节,我们来看下raincat的事务提交过程. 二.事务提交过程概览 1.二阶段对应 ...

  7. vmware centos 连网方式

    1.自动获取IP地址 虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分配IP地址. #dhclient 自动获取ip地址命令 #ifconfig 查询系统里网卡信息,i ...

  8. MongoDB shell基本操作

    shell命令操作语法和JavaScript很类似,其实控制台底层的查询语句都是用JavaScript脚本完成操作的.使用shell 命令,需要启动mongo.exe.mongodb百科 常用shel ...

  9. CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)

    Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common div ...

  10. CodeForces - 961D:Pair Of Lines (几何,问两条直线是否可以覆盖所有点)

    You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordin ...