当前位置:网站首页>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]
// 说明选不选都可以得到最优解,但是为了考虑字典序最小,我们也需要选取该物品边栏推荐
- Acwing game 61 [End]
- Seat / safety configuration upgrade is the administrative experience of the new Volvo S90 in place
- 1311_硬件设计_ICT概念、应用以及优缺点学习小结
- Dracoo master
- HelloWorld case analysis
- 红星美凯龙高负债之下,盯上新能源了?
- Worked overtime for a week to develop a reporting system. This low code free it reporting artifact is very easy to use
- Find my technology | the Internet of things asset tracking market has reached US $6.6 billion, and find my helps the market develop
- operator new、operator delete补充讲义
- 在 Istio 服务网格内连接外部 MySQL 数据库
猜你喜欢

Acwing第 61 场周赛【完结】

Communication protocol and message format between microservices

Opencv learning notes - remapping

Find my technology | the Internet of things asset tracking market has reached US $6.6 billion, and find my helps the market develop
![[project chapter - how to write and map the business model? (3000 word graphic summary suggestions)] project plan of innovation and entrepreneurship competition and application form of national Entrep](/img/e8/b115b85e2e0547545e85b2058a9bb0.png)
[project chapter - how to write and map the business model? (3000 word graphic summary suggestions)] project plan of innovation and entrepreneurship competition and application form of national Entrep
![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](/img/84/e5cb5199fe4602440b50dfc4afe963.gif)
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

Introduction to UFS CLK gate

《opencv学习笔记》-- 霍夫变换

Seat / safety configuration upgrade is the administrative experience of the new Volvo S90 in place

Dracoo master
随机推荐
php 实现从1累加到100的算法
【读书笔记->数据分析】BDA教材《数据分析》书籍介绍
redux
PHP 对象转换数组
(translation) timing of website flow chart and user flow chart
【二叉树】二叉树中的最长交错路径
Wechat applet to realize music player (4) (use pubsubjs to realize inter page communication)
STM32状态机编程实例——全自动洗衣机(下)
2.9.4 Ext JS的布尔对象类型处理及便捷方法
文献|关系研究能得出因果性结论吗
Tactile intelligent sharing-rk3568 application in scenic spot navigation robot
5 years, 1.4W times, NFT og's road to immortality Web3 column
[in depth study of 4g/5g/6g topic-42]: urllc-13 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -7-low delay technology-1-subcarrier spacing expansio
《opencv学习笔记》-- 重映射
Introduction to UFS CLK gate
Find my technology | the Internet of things asset tracking market has reached US $6.6 billion, and find my helps the market develop
5年1.4W倍,NFT OG 的封神之路|Web3专栏
在 Istio 服务网格内连接外部 MySQL 数据库
Constructing verb sources for relation extraction
Sentinel fusing and current limiting