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)的更多相关文章

  1. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

  2. PAT (Advanced Level) 1095. Cars on Campus (30)

    模拟题.仔细一些即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorit ...

  3. PAT (Advanced Level) 1076. Forwards on Weibo (30)

    最短路. 每次询问的点当做起点,然后算一下点到其余点的最短路.然后统计一下最短路小于等于L的点有几个. #include<cstdio> #include<cstring> # ...

  4. PAT (Advanced Level) 1045. Favorite Color Stripe (30)

    最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...

  5. PAT (Advanced Level) 1018. Public Bike Management (30)

    先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了. #include<iostream> #include<cstring> #include&l ...

  6. PAT (Advanced Level) 1014. Waiting in Line (30)

    简单模拟题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  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 (30 分) (dp,01背包问题记录最佳选择方案)***

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

  9. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

随机推荐

  1. 用sqlyog远程连接LINUX系统的MYSQL出现错解决方法

    无法给远程连接的用户权限问题.结果这样子操作mysql库,即可解决.在本机登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”localhost”改称'%' ...

  2. Java类和类成员的访问权限修饰符

    一:访问修饰符: 1.省略访问修饰符 具有默认的访问特性,即具有包访问特性,只能被同一个包中的类使用. 2.public访问修饰符 用于说明类和类的成员的访问权限.这种类叫公有类.在一个文件中只能有一 ...

  3. The List ADT

    1.Definiation A list is a sequence.  a0, a1,a2,..., aN (N>0) 2.Character For any list except the ...

  4. Mac下截图快捷键

    Cmd+Shift+3:全屏截图:截取整个屏幕,保存截图至桌面文件夹.Cmd+Shift+4:区域截图:鼠标光标变成带坐标的小十字,通过拖拽截取特定区域,保存截图至桌面文件夹.Cmd+Shift+4 ...

  5. Shell:进程的层级关系

    [luwenwei@appdev115 ~]$ ps -ef | grep initroot 1 0 0 Apr24 ? 00:08:25 init [3] [luwenwei@appdev115 ~ ...

  6. .htaccess文件url重写小记

    .htaccess文件url重写 当上一条规则匹配 并转换后 符合下一条规则的 继续下一条的匹配转换 RewriteRule ^shangpin-([0-9a-zA-Z]+)/category-([0 ...

  7. Thinkphp与Ucenter整合笔记

    ucenter手册:http://www.phpddt.com/manual/ucenter/html/index.htm 参考:http://www.thinkphp.cn/topic/1557.h ...

  8. PHP扩展开发-简单类扩展

    今天来学习简单类扩展开发 实现目标为如下php的类 <?php class classext(){ private $username; CONST URL="http://www.g ...

  9. hibernate ——helloWorld程序(annotation配置)

    在  <hibernate  ——helloWorld程序(XML配置)>基础上,修改.添加部分文件: 1.Teacher类和Teacher表 package com.pt.hiberna ...

  10. 关于spring的注解方式注入默认值(转) -- 首字母小写

    1.是首字母小写 比如 UserAction对应的id是userAction 可以通过ApplicationContext 对象的act.getBean("userAction") ...