当前位置:网站首页>[NOIP2001 普及组]装箱问题
[NOIP2001 普及组]装箱问题
2022-07-26 03:01:00 【ceshyong】
题目链接
[NOIP2001 普及组] 装箱问题 - 洛谷https://www.luogu.com.cn/problem/P1049
题目描述
有一个箱子容量为V,同时有N个物品,每个物品有一个体积。
现在从N个物品中,任取若干个装入箱内(也可以不取),使箱子的剩余空间最小。输出这个最小值。
输入
第一行共一个整数V,表示箱子容量。
第二行共一个整数N,表示物品总数。
接下来N行,每行有一个正整数,表示第i个物品的体积。
输出
共一行一个整数,表示箱子最小剩余空间。
样例组
输入
24
6
8
3
12
7
9
7
输出
1数据范围
对于100%的数据,满足n≤30,1≤V≤20000。
解题思路
这道题目是一道动态规划的题目,我们可以直接用采药的程序来写。
由于这道题目价格与重量都相等,也就是说我们可以输入两个数组中的一个,再把另一个的值赋为它的值。这样就可以在输入一个数组的情况下完成两个数组的赋值。读入代码如下:
void dr(int n,int &w[],int &c[])
{
cin>>n;
for(int i=1;i<=n;i++) cin>>w[i],c[i]=w[i];
}然后直接套01背包的主程序。
然后,我们还需要在最后输出的环节将原来的价值改为背包的剩余空间(背包容量V减去背包使用量)。把输出改成这样就行了。
至于递归?那实在是太慢了!题目只能拿60分!(AC不了题目)
题目标程
#include<bits/stdc++.h>
using namespace std;
const int maxn=35,maxv=20005;//用define写要两行
int c,n,v[maxn],f[maxv];
int main()
{
scanf("%d%d",&c,&n);
for(int i=1;i<=n;++i) scanf("%d",&v[i]);
for(int i=1;i<=n;++i)
for(int j=c;j>=v[i];--j) f[j]=max(f[j],f[j-v[i]]+v[i]);
printf("%d",c-f[c]);
return 0;
} 这道题目就这么多。
小调查
边栏推荐
- Neo4j 导入csv数据报错:Neo4j load csv error : Couldn‘t load the external resource
- .net serialize enumeration as string
- How to effectively prevent others from wearing the homepage snapshot of the website
- Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered
- ES6高级-利用构造函数继承父类属性
- [steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (reconstruction)
- GoLang 抽奖系统 设计
- High score technical document sharing of ink Sky Wheel - Database Security (48 in total)
- STM32——PWM学习笔记
- From the annual reports of major apps, we can see that user portraits - labels know you better than you do
猜你喜欢
![[C language] deeply understand integer lifting and arithmetic conversion](/img/5c/21d0df424c034721c64b0653edc483.png)
[C language] deeply understand integer lifting and arithmetic conversion

VR panoramic shooting and production of business center helps businesses effectively attract people

Eslint common error reporting set

这种动态规划你见过吗——状态机动态规划之股票问题(上)

Vofa+ serial port debugging assistant

Study notes of pytorch deep learning practice: convolutional neural network (Advanced)

JVM内存模型解析
![[sql] case expression](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[sql] case expression

Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered

ShardingSphere数据分片
随机推荐
Is it safe to open galaxy securities account by mobile phone?
Detailed explanation of extended physics informedneural networks paper
(9) Attribute introspection
VR panoramic shooting and production of business center helps businesses effectively attract people
[introduction to C language] zzulioj 1006-1010
Method of manually cloning virtual machine in esxi6.7
Code dynamically controls textview to move right (not XML)
JS get the time composition array of two time periods
Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
这种动态规划你见过吗——状态机动态规划之股票问题(上)
FPGA_ Initial use process of vivado software_ Ultra detailed
Basics - network and server
Neo4j import CSV data error: neo4j load CSV error: couldn't load the external resource
How can users create data tables on Web pages and store them in the database
持续交付和DevOps是一对好基友
STM32 - PWM learning notes
Case: using kept+haproxy to build a Web Cluster
FPGA_Vivado软件初次使用流程_超详细
万维网、因特网和互联网的区别
如何用U盘进行装机?