PAT (Advanced Level) 1068. Find More Coins (30)
01背包路径输出。
保证字典序最小:从大到小做背包。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std; const int maxn=+;
bool dp[maxn*maxn][maxn];
int a[maxn*maxn];
int n,k;
vector<int>ans;
struct Path
{
int r,c;
}p[maxn*maxn][maxn]; bool cmp(const int &a,const int &b)
{
return a>b;
} int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a++n,cmp);
memset(dp,,sizeof dp);
for(int i=;i<=n;i++)
for(int j=;j<=k;j++)
p[i][j].r=p[i][j].c=-;
dp[][]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=k;j++)
{
if(dp[i-][j]==) continue;
if(j+a[i]>k) continue;
dp[i][j+a[i]]=;
p[i][j+a[i]].r=i-;
p[i][j+a[i]].c =j;
}
for(int j=;j<=k;j++)
{
if(dp[i-][j]==&&dp[i][j]==)
{
p[i][j].r=p[i-][j].r;
p[i][j].c=p[i-][j].c;
}
dp[i][j]=max(dp[i][j],dp[i-][j]);
}
} if(dp[n][k]==) printf("No Solution\n");
else
{
int nowr=n,nowc=k;
while()
{
ans.push_back(nowc-p[nowr][nowc].c);
int tmpr=nowr,tmpc=nowc;
nowc=p[tmpr][tmpc].c;
nowr=p[tmpr][tmpc].r;
if(nowc==) break;
}
for(int i=;i<ans.size();i++)
{
printf("%d",ans[i]);
if(i<ans.size()-) printf(" ");
else printf("\n");
}
}
return ;
}
PAT (Advanced Level) 1068. Find More Coins (30)的更多相关文章
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
- PAT (Advanced Level) 1095. Cars on Campus (30)
模拟题.仔细一些即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorit ...
- PAT (Advanced Level) 1076. Forwards on Weibo (30)
最短路. 每次询问的点当做起点,然后算一下点到其余点的最短路.然后统计一下最短路小于等于L的点有几个. #include<cstdio> #include<cstring> # ...
- PAT (Advanced Level) 1045. Favorite Color Stripe (30)
最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...
- PAT (Advanced Level) 1018. Public Bike Management (30)
先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了. #include<iostream> #include<cstring> #include&l ...
- PAT (Advanced Level) 1014. Waiting in Line (30)
简单模拟题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
- 【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 (30 分) (dp,01背包问题记录最佳选择方案)***
1068 Find More Coins (30 分) Eva loves to collect coins from all over the universe, including some ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
随机推荐
- 我的定时关机程序(MFC实现) .
原理: 利用定时器去检查,如输入的是多少分钟后关机,就根据输入的分钟数产生一个COUNT计数器,计数器一直递减,直到1,然后执行关机.如输入的是几时几分关机,那么定时器会每次都检查系统的时间和你输入的 ...
- /etc/fstab 文件解释
/etc/fstab 文件解释 文件fstab包含了你的电脑上的存储设备及其文件系统的信息.它是决定一个硬盘(分区)被怎样使用或者说整合到整个系统中的唯一文件. 这个文件的全路径是/etc/fstab ...
- 打印出最后执行的mysql 语句
db.php 文件中添加 public function getlastsql(){ return $this->sql; } 入口文件中添加,公共方法 function getlastsql( ...
- 微信接口开发1--向微信发送请求--获取access_token
//随便放置一个php文件在服务器上.执行该方法--调用模拟get提交---到微信-->获得微信返回的access_token 不建议自己编写模拟get提交方法. 建议直接导入微信框架LaneW ...
- shell之路【第一篇】shell简介与入门
shell简介 1.Shell 诞生于 Unix,Unix的第一个脚本语言,是与 Unix/Linux 交互的工具,单独地学习 Shell 是没有意义的,shell使用的熟练程度反映了用户对Unix/ ...
- Java中判断字符串中相同字符的个数
public static int countStr(String str1, String str2) { int counter=0; if (str1.indexOf(str2) == -1) ...
- js调用函数的格式
如题 onclick='alert(\""+""+"\")' onclick='alert(encodeURIComponen ...
- Asp.Net调用Office组件操作时的DCOM配置 (转)
Asp.Net调用Office组件操作时的DCOM配置 http://blog.csdn.net/gz775/article/details/6447758 在项目中将数据导出为Excel格式时出现“ ...
- 实现jsp页面显示用户登录信息,利用session保存。
这是后台代码 这是jsp代码,上面是声明,下面是获得值.
- struts2获得提交是get还是post方法提交
String method=ServletActionContext.getRequest().getMethod(); System.out.println(method); 如果是get 会打印 ...