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: 基础数据结构: 线性 ...
随机推荐
- @Value 注解获取properties值
转自:使用Spring 3的@value简化配置文件的读取 Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码. 1.在application ...
- Fine Uploader 简单配置方法
由于jquery.uploadify是基于flash的jquery上传控件,客户老是说出问题,所以今天换成了一个纯js的异步上传控件. 这方面的资料很少,故此记下来分享一下. 项目地址:Fine Up ...
- hdu_2838_Cow Sorting(树状数组求逆序对)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 题意:给你一串数,让你排序,只能交换相邻的数,每次交换花费交换的两个树的和,问最小交换的价值 题 ...
- hdu_4824_Disk Schedule(dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4824 题意:中文,不解释 题解:双调欧几里德旅行商问题,具体可看dp双调欧几里德旅行商,这里注意的是起 ...
- HDU2535:Vote
Problem Description 美国大选是按各州的投票结果来确定最终的结果的,如果得到超过一半的州的支持就可以当选,而每个州的投票结果又是由该州选民投票产生的,如果某个州超过一半的选民支持希拉 ...
- IBM Mq Spring JMS 的xml配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- webstrom 快捷键(Idea可用)
在File-->setting可查看和配置功能快捷键,以下列出常用的快捷键 1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录的文件. 2. ctrl + ...
- 缩放系列(三):一个可以手势缩放、拖拽、旋转的layout
弄了一个下午,终于搞出来了,PowerfulLayout 下面是一个功能强大的改造的例子: 可以实现以下需求: 1.两个手指进行缩放布局 2.所有子控件也随着缩放, 3.子控件该有的功能不能丢失(像b ...
- unknown filesystem type ‘iso9660’类型问题--Ubuntu
unknown filesystem type ‘iso9660’是指系统不支持这种类型的文件, 用以下命令更新内核即可: sudo aptitude update sudo aptitude upg ...
- ckediter
ckediter ##<link rel='stylesheet' href='/css/index.css' /> <script type="text/javascri ...