当前位置:网站首页>Acwing_12. 背包问题求具体方案_dp
Acwing_12. 背包问题求具体方案_dp
2022-07-26 04:09:00 【这题AC再睡.】

//
#include<bits/stdc++.h>
using namespace std;
const int N=1111;
int x[N],y[N],dp[N][N],ans[N];
int n,v,pos;
void f( int id,int v )
{
if( id==n+1 ) return ; // 判越界
// 判越界
if( v>=x[id] && dp[id][v]==dp[id+1][v-x[id]]+y[id] )
{
ans[++pos]=id; v-=x[id]; // 可存 也可输出
}
f( id+1,v );
}
int main()
{
int i,j;
while( cin>>n>>v )
{
for( i=1;i<=n;i++ ) cin>>x[i]>>y[i];
memset( dp,0,sizeof( dp ) );
for( i=n;i;i-- )
for( j=0;j<=v;j++ ) // +
dp[i][j]=( j>=x[i] )?( max( dp[i+1][j],dp[i+1][j-x[i]]+y[i] ) ):( dp[i+1][j] );
// {
// dp[i][j]=dp[i+1][j];
// if( j>=x[i] )
// dp[i][j]=max( dp[i][j],dp[i+1][j-x[i]]+y[i] );
// }
// cout<<dp[1][v]<<endl;
pos=0;
f( 1,v );
for( i=1;i<=pos;i++ )
{
if( i!=1 ) cout<<" ";
cout<<ans[i];
}
cout<<endl;
}
return 0;
}
// 题目要求输出字典序最小的解,
// 假设存在一个包含第1个物品的最优解,
// 为了确保字典序最小那么我们必然要选第一个。
// 那么问题就转化成从2-N这些物品中找到最优解。
// 之前的 dp[i][j] 记录的都是前i个物品总容量为j的最优解,
// 那么我们现在将 dp[i][j] 定义为从第i个元素到最后一个元素总容量为j的最优解
// dp[i][j]=max( dp[i+1][j],dp[i+1][j-x[i]]+y[i] )
// 两种情况
// 01 不选第i个物品,那么最优解等于 从第i+1个物品到最后一个元素 总容量为j的最优解
// 02 选了第i个物品,那么最优解等于 当前物品的价值y[i] 加上从第i+1个物品到最后一个元素 总容量为j-x[i]的最优解
// 计算完状态表示后 考虑如何的到最小字典序的解
// 首先 dp[1][v] 肯定是最大价值 那么我们便开始考虑能否选取第1个物品呢
// 01 如果 dp[1][v] == dp[2][v-x[1]]+y[1] 说明选取了第1个物品可以得到最优解
// 02 如果 dp[1][v] == dp[2][v] 说明不选取第一个物品才能得到最优解
// 03 如果 dp[1][v] == dp[2][v] == dp[2][v-x[1]]+y[1]
// 说明选不选都可以得到最优解,但是为了考虑字典序最小,我们也需要选取该物品边栏推荐
- Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130
- Advanced content of MySQL -- three MySQL logs that must be understood binlog, redo log and undo log
- [binary tree] the longest interleaved path in a binary tree
- Chapter 18: explore the wonders of the mean in the 2-bit a~b system, specify the 3x+1 conversion process of integers, specify an interval to verify the angular Valley conjecture, explore the number of
- Connect external MySQL databases in istio Service Grid
- php 查找 session 存储文件位置的方法
- Pits encountered by sdl2 OpenGL
- Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
- 【SVN】一直出现 Please execute the ‘Cleanup‘ command,cleanup以后没有反应的解决办法
- How mantium uses deepspeed to implement low latency gpt-j reasoning on Amazon sagemaker
猜你喜欢

Opencv learning notes -- Hough transform

2021 CIKM |GF-VAE: A Flow-based Variational Autoencoder for Molecule Generation

STM32 state machine programming example - full automatic washing machine (Part 2)

(翻译)网站流程图和用户流程图的使用时机

firewall 命令简单操作

How mantium uses deepspeed to implement low latency gpt-j reasoning on Amazon sagemaker

Luoda Development -- the context of sidetone configuration

E-commerce operator Xiaobai, how to get started quickly and learn data analysis?

文献|关系研究能得出因果性结论吗

Advanced content of MySQL -- three MySQL logs that must be understood binlog, redo log and undo log
随机推荐
PHP method to find the location of session storage file
How engineers treat open source -- the heartfelt words of an old engineer
LeetCode. 6115 count the number of ideal arrays
Pat class a 1039 course list for student
2022杭电多校 Bowcraft
(translation) the button position convention can strengthen the user's usage habits
How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
Leetcode:1184. Distance between bus stops -- simple
荐书丨《教育心理学》:送给明日教师的一本书~
[深入研究4G/5G/6G专题-42]: URLLC-13-《3GPP URLLC相关协议、规范、技术原理深度解读》-7-低延时技术-1-子载波间隔扩展
php eval() 函数可以将一个字符串当做 php 代码来运行
PHP 对象转换数组
Implementation of distributed lock
构建关系抽取的动词源
PHP < => spacecraft operator (combined comparator)
[oi knowledge] dichotomy, dichotomy concept, integer dichotomy, floating point dichotomy
[Reading Notes - > data analysis] Introduction to BDA textbook data analysis
CPU and GPU are out of date, and the era of NPU and APU begins
Apple removed the last Intel chip from its products
LeetCode:1184. 公交站间的距离————简单