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: 基础数据结构: 线性 ...
随机推荐
- 第五节 面向连接传输:TCP
第五节 面向连接传输:TCP TCP概述RFCs:793,1122,1323,2018,2581 点对点: 一个发送方,一个接收方 可靠,按序的字节流: 无“报文边界”,无结构但有 ...
- php 上传缩放图片
有时上传图片时因为图片太大了,不仅占用空间,消耗流量,而且影响浏(图片的尺寸大小不一).下面分享一种等比例不失真缩放图片的方法,这样,不管上传的图片尺有多大,都会自动压缩到我们设置尺寸值的范围之内.经 ...
- R语言笔记3--实例1
综合性例子: 模拟产生统计专业同学的名单(学号区分),记录数学分析,线性代数.概率统计三科成绩,然后进行一些统计分析 1.首先产生一个向量(100个元素,代表100位学生) 2.模拟成绩 runif: ...
- shell之路【第三篇】流程控制
if语句 if ... fi 语句: if ... else ... fi 语句: if ... elif ... else ... fi 语句. 注意: expression 和方括号([ ])之间 ...
- js css优化-- 合并和压缩
在项目框架中,首先要引用很多css和js文件,80%的用户响应时间都是浪费在前端.而这些时间主要又是因为下载图片.样式表.JavaScript脚本.flash等文件造成的.减少这些资源文件的Reque ...
- 移植Iperf到android 用来学习linux移植到安卓的例子
Iperf移植记录 1.生成arm编译需要的头文件config.h ./configure --host=arm如果需要make clean make distclean2.增加Android.mk文 ...
- 1.1 selenium 安装
1.在火狐浏览器的附加组件里搜索Selenium IDE
- 获取Excel数据(或部分数据)并导出成txt文本格式
运行代码前先导入jxl架包,以下代码仅供参考: 测试excel文件(我要获取该excel的内容为省.县.乡.村.组和PH的值): ExcelTest01类代码如下: // 读取Excel的类 impo ...
- Java获取来访者IP
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- 初识Selenium(一)
Selenium入门相关PPT参考网址:http://wenku.baidu.com/view/d1e7d90390c69ec3d5bb7565.html?from=search 内容引用网址:htt ...